-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathUser.php
More file actions
384 lines (332 loc) · 11 KB
/
User.php
File metadata and controls
384 lines (332 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* ProcessWire UserPage
*
* A type of Page used for storing an individual User
*
* ProcessWire 2.x
* Copyright (C) 2013 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*
* @link http://processwire.com/api/variables/user/ Offical $user API variable Documentation
*
* @property string email Get or set email address for this user.
* @property string pass Set the user's password. Note that when getting, this returns a hashed version of the password, so it is not typically useful to get this property. However, it is useful to set this property if you want to change the password. When you change a password, it is assumed to be the non-hashed/non-encrypted version. ProcessWire will hash it automatically when the user is saved.
* @property PageArray roles Get roles this user has. Returns PageArray.
* @property Language $language User language, applicable only if LanguageSupport installed.
*
*/
class User extends Page {
/**
* Create a new User page in memory.
*
* @param Template $tpl Template object this page should use.
*
*/
public function __construct(Template $tpl = null) {
if(is_null($tpl)) $tpl = $this->wire('templates')->get('user');
if(!$this->parent_id) $this->set('parent_id', $this->wire('config')->usersPageID);
parent::__construct($tpl);
}
/**
* Does this user have the given role? (object, name or id)
*
* @param string|Role|int
* @return bool
*
*/
public function hasRole($role) {
$roles = $this->get('roles');
$has = false;
if(empty($roles)) {
// do nothing
} else if(is_object($role) && $role instanceof Page) {
$has = $roles->has($role);
} else if(is_object($role) && $role instanceof PageArray) {
foreach ($role as $page) {
if ($this->hasRole($page)) {
$has = true;
break;
}
}
} else if(strpos($role, '|') !== false) {
$multiroles = explode('|', $role);
foreach ($multiroles as $singlerole) {
if ($this->hasRole($singlerole)) {
$has = true;
break;
}
}
} else if(ctype_digit("$role")) {
$role = (int) $role;
foreach($roles as $r) {
if(((int) $r->id) === $role) {
$has = true;
break;
}
}
} else if(is_string($role)) {
foreach($roles as $r) {
if($r->name === $role) {
$has = true;
break;
}
}
}
return $has;
}
/**
* Add the given role string, id or object
*
* This is the same as $user->roles->add($role) except this one will accept ID or name.
*
* @param string|int|Role
* @return bool false if role not recognized, true otherwise
*
*/
public function addRole($role) {
if(is_string($role) || is_int($role)) $role = $this->fuel('roles')->get($role);
if(is_object($role) && $role instanceof Role) {
$this->get('roles')->add($role);
return true;
}
return false;
}
/**
* Remove the given role string, id or object
*
* This is the same as $user->roles->remove($role) except this one will accept ID or name.
*
* @param string|int|Role
* @return bool false if role not recognized, true otherwise
*
*/
public function removeRole($role) {
if(is_string($role) || is_int($role)) $role = $this->fuel('roles')->get($role);
if(is_object($role) && $role instanceof Role) {
$this->get('roles')->remove($role);
return true;
}
return false;
}
/**
* Does the user have the given permission, OR the given permission in the given context?
*
* Context may be a Page or a Template.
* This method serves as the public interface to the hasPagePermission and hasTemplatePermission methods.
*
* @param string|Permission $name Permission name
* @param Page|Template $context Page or Template
* @return bool
*
*/
public function hasPermission($name, $context = null) {
if(is_null($context) || $context instanceof Page) {
return $this->isHooked('hasPagePermission()') ? $this->hasPagePermission($name, $context) : $this->___hasPagePermission($name, $context);
}
if($context instanceof Template) {
return $this->isHooked('hasTemplatePermission()') ? $this->hasTemplatePermission($name, $context) : $this->___hasTemplatePermission($name, $context);
}
return false;
}
/**
* Does this user have the given permission name?
*
* This is a basic permission check and it is recommended that you use those from the PagePermissions module instead.
* You use the PagePermissions module by calling the editable(), addable(), etc., functions on a page object.
* The PagePermissions does use this function for some of it's checking.
*
* @param string|Permission
* @param Page $page Optional page to check against
* @return bool
*
*/
protected function ___hasPagePermission($name, Page $page = null) {
if($this->isSuperuser()) return true;
if($name instanceof Page) {
$permission = $name;
} else {
$p = $name;
// page-add and page-create don't actually exist in the DB, so we substitute page-edit for them
// code later on will make sure they exist in the template's addRoles/createRoles
if(in_array($name, array('page-add', 'page-create'))) $p = 'page-edit';
$permission = $this->fuel('permissions')->get("name=$p");
}
if(!$permission || !$permission->id) return false;
$roles = $this->get('roles');
if(empty($roles)) return false;
$has = false;
$accessTemplate = is_null($page) ? null : $page->getAccessTemplate();
foreach($roles as $key => $role) {
if(!$role || !$role->id) continue;
if(!is_null($page)) {
if(!$page->id) continue;
// if page doesn't have the 'view' role, then no access
if(!$page->hasAccessRole($role)) continue;
// if permission is page-edit, we also check against the template's editRoles
// if($name == 'page-edit' && !in_array($role->id, $page->getAccessTemplate()->editRoles)) continue;
// all page- permissions except page-view and page-add require page-edit access on $page, so check against that
if(strpos($name, 'page-') === 0 && $name != 'page-view' && $name != 'page-add' && !in_array($role->id, $accessTemplate->editRoles)) continue;
// check against addRoles, createRoles if the permission requires it
if($name == 'page-add' && !in_array($role->id, $accessTemplate->addRoles)) continue;
else if($name == 'page-create' && !in_array($role->id, $accessTemplate->createRoles)) continue;
//else if($name == 'page-delete' && !in_array($role->id, $accessTemplate->deleteRoles)) continue;
}
if($role->hasPermission($permission)) {
$has = true;
break;
}
}
return $has;
}
/**
* Does this user have the given permission on the given template?
*
* @param string $name Permission name
* @param Template|int|string $template Template object, name or ID
* @return bool
* @throws WireException
*
*/
protected function ___hasTemplatePermission($name, $template) {
if($name instanceof Template) $name = $name->name;
if(is_object($name)) throw new WireException("Invalid type");
if($this->isSuperuser()) return true;
if($template instanceof Template) {
// fantastic then
} else if(is_string($template) || is_int($template)) {
$template = $this->templates->get($this->sanitizer->name($template));
if(!$template) return false;
} else {
return false;
}
// if the template is not defining roles, we have to say 'no' to permission
// because we don't have any page context to inherit from at this point
// if(!$template->useRoles) return false;
$roles = $this->get('roles');
if(empty($roles)) return false;
$has = false;
foreach($roles as $role) {
if(!$template->hasRole($role)) continue;
if($name == 'page-create') {
if(!in_array($role->id, $template->createRoles)) continue;
$name = 'page-edit'; // swap permission to page-edit since create managed at template and requires page-edit
}
if($name == 'page-edit' && !in_array($role->id, $template->editRoles)) continue;
if($name == 'page-add') {
if(!in_array($role->id, $template->addRoles)) continue;
$name = 'page-edit';
}
if($role->hasPermission($name)) {
$has = true;
break;
}
}
return $has;
}
/**
* Get this user's permissions, optionally within the context of a Page
*
* Does not currently include page-add or page-create permissions.
*
* @param Page $page Optional page to check against
* @return bool
*
*/
public function getPermissions(Page $page = null) {
if($this->isSuperuser()) return $this->fuel('permissions');
$permissions = new PageArray();
$roles = $this->get('roles');
if(empty($roles)) return $permissions;
foreach($roles as $key => $role) {
if($page && !$page->hasAccessRole($role)) continue;
foreach($role->permissions as $permission) {
if($page && $permission->name == 'page-edit' && !in_array($role->id, $page->getAccessTemplate()->editRoles)) continue;
$permissions->add($permission);
}
}
return $permissions;
}
/**
* Does this user have the superuser role?
*
* Same as $user->roles->has('name=superuser');
*
* @return bool
*
*/
public function isSuperuser() {
$config = $this->wire('config');
if($this->id === $config->superUserPageID) return true;
if($this->id === $config->guestUserPageID) return false;
$superuserRoleID = (int) $config->superUserRolePageID;
$roles = $this->get('roles');
if(empty($roles)) return false;
$is = false;
foreach($roles as $role) if(((int) $role->id) === $superuserRoleID) {
$is = true;
break;
}
return $is;
}
/**
* Is this the non-logged in guest user?
*
* @return bool
*
*/
public function isGuest() {
return $this->id === $this->wire('config')->guestUserPageID;
}
/**
* Is the current user logged in?
*
* @return bool
*
*/
public function isLoggedin() {
return !$this->isGuest();
}
/**
* Get the value for a non-native User field
*
* @param string $key
* @return null|mixed
*
*/
protected function getFieldValue($key) {
$value = parent::getFieldValue($key);
if(!$value && $key == 'language') {
$languages = $this->wire('languages');
if($languages) $value = $languages->getDefault();
}
return $value;
}
/**
* Returns the URL where this page can be edited
*
* In this case we adjust the default page editor URL to ensure users are edited
* only from the Access section.
*
* @return string
*
*/
public function editUrl() {
return str_replace('/page/edit/', '/access/users/edit/', parent::editUrl());
}
/**
* Set the Process module (WirePageEditor) that is editing this User
*
* We use this to detect when the User is being edited somewhere outside of /access/users/
*
* @param WirePageEditor $editor
*
*/
public function ___setEditor(WirePageEditor $editor) {
parent::___setEditor($editor);
if(!$editor instanceof ProcessUser) $this->wire('session')->redirect($this->editUrl());
}
}