@@ -35,7 +35,7 @@ |
||
35 | 35 | { |
36 | 36 | return ((is_scalar($expected) xor null === $expected) && |
37 | 37 | (is_scalar($actual) xor null === $actual)) |
38 | - // allow comparison between strings and objects featuring __toString() |
|
38 | + // allow comparison between strings and objects featuring __toString() |
|
39 | 39 | || (is_string($expected) && is_object($actual) && method_exists($actual, '__toString')) |
40 | 40 | || (is_object($expected) && method_exists($expected, '__toString') && is_string($actual)); |
41 | 41 | } |
@@ -9,778 +9,778 @@ |
||
9 | 9 | |
10 | 10 | class FlatAuthorization implements AuthorizeInterface |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array|string|null |
|
14 | - */ |
|
15 | - protected $error; |
|
16 | - |
|
17 | - /** |
|
18 | - * The group model to use. Usually the class noted |
|
19 | - * below (or an extension thereof) but can be any |
|
20 | - * compatible CodeIgniter Model. |
|
21 | - * |
|
22 | - * @var GroupModel |
|
23 | - */ |
|
24 | - protected $groupModel; |
|
25 | - |
|
26 | - /** |
|
27 | - * The group model to use. Usually the class noted |
|
28 | - * below (or an extension thereof) but can be any |
|
29 | - * compatible CodeIgniter Model. |
|
30 | - * |
|
31 | - * @var PermissionModel |
|
32 | - */ |
|
33 | - protected $permissionModel; |
|
34 | - |
|
35 | - /** |
|
36 | - * The group model to use. Usually the class noted |
|
37 | - * below (or an extension thereof) but can be any |
|
38 | - * compatible CodeIgniter Model. |
|
39 | - * |
|
40 | - * @var UserModel |
|
41 | - */ |
|
42 | - protected $userModel = null; |
|
43 | - |
|
44 | - /** |
|
45 | - * Stores the models. |
|
46 | - * |
|
47 | - * @param GroupModel $groupModel |
|
48 | - * @param PermissionModel $permissionModel |
|
49 | - * |
|
50 | - * @return array|string|null |
|
51 | - */ |
|
52 | - public function __construct(Model $groupModel, Model $permissionModel) |
|
53 | - { |
|
54 | - $this->groupModel = $groupModel; |
|
55 | - $this->permissionModel = $permissionModel; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Returns any error(s) from the last call. |
|
60 | - * |
|
61 | - * @return array|string|null |
|
62 | - */ |
|
63 | - public function error() |
|
64 | - { |
|
65 | - return $this->error; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Allows the consuming application to pass in a reference to the |
|
70 | - * model that should be used. |
|
71 | - * |
|
72 | - * @param UserModel $model |
|
73 | - * |
|
74 | - * @return mixed |
|
75 | - */ |
|
76 | - public function setUserModel(Model $model) |
|
77 | - { |
|
78 | - $this->userModel = $model; |
|
79 | - |
|
80 | - return $this; |
|
81 | - } |
|
82 | - |
|
83 | - //-------------------------------------------------------------------- |
|
84 | - // Actions |
|
85 | - //-------------------------------------------------------------------- |
|
86 | - |
|
87 | - /** |
|
88 | - * Checks to see if a user is in a group. |
|
89 | - * |
|
90 | - * Groups can be either a string, with the name of the group, an INT |
|
91 | - * with the ID of the group, or an array of strings/ids that the |
|
92 | - * user must belong to ONE of. (It's an OR check not an AND check) |
|
93 | - * |
|
94 | - * @param mixed $groups |
|
95 | - * @param int $userId |
|
96 | - * |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - public function inGroup($groups, int $userId) |
|
100 | - { |
|
101 | - if ($userId === 0) |
|
102 | - { |
|
103 | - return false; |
|
104 | - } |
|
105 | - |
|
106 | - if (! is_array($groups)) |
|
107 | - { |
|
108 | - $groups = [$groups]; |
|
109 | - } |
|
110 | - |
|
111 | - $userGroups = $this->groupModel->getGroupsForUser((int) $userId); |
|
112 | - |
|
113 | - if (empty($userGroups)) |
|
114 | - { |
|
115 | - return false; |
|
116 | - } |
|
117 | - |
|
118 | - foreach ($groups as $group) |
|
119 | - { |
|
120 | - if (is_numeric($group)) |
|
121 | - { |
|
122 | - $ids = array_column($userGroups, 'group_id'); |
|
123 | - if (in_array($group, $ids)) |
|
124 | - { |
|
125 | - return true; |
|
126 | - } |
|
127 | - } |
|
128 | - else if (is_string($group)) |
|
129 | - { |
|
130 | - $names = array_column($userGroups, 'name'); |
|
131 | - |
|
132 | - if (in_array($group, $names)) |
|
133 | - { |
|
134 | - return true; |
|
135 | - } |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - return false; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Checks a user's groups to see if they have the specified permission. |
|
144 | - * |
|
145 | - * @param int|string $permission Permission ID or name |
|
146 | - * @param int $userId |
|
147 | - * |
|
148 | - * @return mixed |
|
149 | - */ |
|
150 | - public function hasPermission($permission, int $userId) |
|
151 | - { |
|
152 | - // @phpstan-ignore-next-line |
|
153 | - if (empty($permission) || (! is_string($permission) && ! is_numeric($permission))) |
|
154 | - { |
|
155 | - return null; |
|
156 | - } |
|
157 | - |
|
158 | - if (empty($userId) || ! is_numeric($userId)) |
|
159 | - { |
|
160 | - return null; |
|
161 | - } |
|
162 | - |
|
163 | - // Get the Permission ID |
|
164 | - $permissionId = $this->getPermissionID($permission); |
|
165 | - |
|
166 | - if (! is_numeric($permissionId)) |
|
167 | - { |
|
168 | - return false; |
|
169 | - } |
|
170 | - |
|
171 | - // First check the permission model. If that exists, then we're golden. |
|
172 | - if ($this->permissionModel->doesUserHavePermission($userId, (int)$permissionId)) |
|
173 | - { |
|
174 | - return true; |
|
175 | - } |
|
176 | - |
|
177 | - // Still here? Then we have one last check to make - any user private permissions. |
|
178 | - return $this->doesUserHavePermission($userId, (int)$permissionId); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Makes a member a part of a group. |
|
183 | - * |
|
184 | - * @param int $userid |
|
185 | - * @param mixed $group Either ID or name, fails on anything else |
|
186 | - * |
|
187 | - * @return bool|null |
|
188 | - */ |
|
189 | - public function addUserToGroup(int $userid, $group) |
|
190 | - { |
|
191 | - if (empty($userid) || ! is_numeric($userid)) |
|
192 | - { |
|
193 | - return null; |
|
194 | - } |
|
195 | - |
|
196 | - if (empty($group) || (! is_numeric($group) && ! is_string($group))) |
|
197 | - { |
|
198 | - return null; |
|
199 | - } |
|
200 | - |
|
201 | - $groupId = $this->getGroupID($group); |
|
202 | - |
|
203 | - if (! Events::trigger('beforeAddUserToGroup', $userid, $groupId)) |
|
204 | - { |
|
205 | - return false; |
|
206 | - } |
|
207 | - |
|
208 | - // Group ID |
|
209 | - if (! is_numeric($groupId)) |
|
210 | - { |
|
211 | - return null; |
|
212 | - } |
|
213 | - |
|
214 | - if (! $this->groupModel->addUserToGroup($userid, (int) $groupId)) |
|
215 | - { |
|
216 | - $this->error = $this->groupModel->errors(); |
|
217 | - |
|
218 | - return false; |
|
219 | - } |
|
220 | - |
|
221 | - Events::trigger('didAddUserToGroup', $userid, $groupId); |
|
222 | - |
|
223 | - return true; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Removes a single user from a group. |
|
228 | - * |
|
229 | - * @param int $userId |
|
230 | - * @param mixed $group |
|
231 | - * |
|
232 | - * @return mixed |
|
233 | - */ |
|
234 | - public function removeUserFromGroup(int $userId, $group) |
|
235 | - { |
|
236 | - if (empty($userId) || ! is_numeric($userId)) |
|
237 | - { |
|
238 | - return null; |
|
239 | - } |
|
240 | - |
|
241 | - if (empty($group) || (! is_numeric($group) && ! is_string($group))) |
|
242 | - { |
|
243 | - return null; |
|
244 | - } |
|
245 | - |
|
246 | - $groupId = $this->getGroupID($group); |
|
247 | - |
|
248 | - if (! Events::trigger('beforeRemoveUserFromGroup', $userId, $groupId)) |
|
249 | - { |
|
250 | - return false; |
|
251 | - } |
|
252 | - |
|
253 | - // Group ID |
|
254 | - if (! is_numeric($groupId)) |
|
255 | - { |
|
256 | - return false; |
|
257 | - } |
|
258 | - |
|
259 | - if (! $this->groupModel->removeUserFromGroup($userId, $groupId)) |
|
260 | - { |
|
261 | - $this->error = $this->groupModel->errors(); |
|
262 | - |
|
263 | - return false; |
|
264 | - } |
|
265 | - |
|
266 | - Events::trigger('didRemoveUserFromGroup', $userId, $groupId); |
|
267 | - |
|
268 | - return true; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Adds a single permission to a single group. |
|
273 | - * |
|
274 | - * @param int|string $permission |
|
275 | - * @param int|string $group |
|
276 | - * |
|
277 | - * @return mixed |
|
278 | - */ |
|
279 | - public function addPermissionToGroup($permission, $group) |
|
280 | - { |
|
281 | - $permissionId = $this->getPermissionID($permission); |
|
282 | - $groupId = $this->getGroupID($group); |
|
283 | - |
|
284 | - // Permission ID |
|
285 | - if (! is_numeric($permissionId)) |
|
286 | - { |
|
287 | - return false; |
|
288 | - } |
|
289 | - |
|
290 | - // Group ID |
|
291 | - if (! is_numeric($groupId)) |
|
292 | - { |
|
293 | - return false; |
|
294 | - } |
|
295 | - |
|
296 | - // Remove it! |
|
297 | - if (! $this->groupModel->addPermissionToGroup($permissionId, $groupId)) |
|
298 | - { |
|
299 | - $this->error = $this->groupModel->errors(); |
|
300 | - |
|
301 | - return false; |
|
302 | - } |
|
303 | - |
|
304 | - return true; |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Removes a single permission from a group. |
|
309 | - * |
|
310 | - * @param int|string $permission |
|
311 | - * @param int|string $group |
|
312 | - * |
|
313 | - * @return mixed |
|
314 | - */ |
|
315 | - public function removePermissionFromGroup($permission, $group) |
|
316 | - { |
|
317 | - $permissionId = $this->getPermissionID($permission); |
|
318 | - $groupId = $this->getGroupID($group); |
|
319 | - |
|
320 | - // Permission ID |
|
321 | - if (! is_numeric($permissionId)) |
|
322 | - { |
|
323 | - return false; |
|
324 | - } |
|
325 | - |
|
326 | - // Group ID |
|
327 | - if (! is_numeric($groupId)) |
|
328 | - { |
|
329 | - return false; |
|
330 | - } |
|
331 | - |
|
332 | - // Remove it! |
|
333 | - if (! $this->groupModel->removePermissionFromGroup($permissionId, $groupId)) |
|
334 | - { |
|
335 | - $this->error = $this->groupModel->errors(); |
|
336 | - |
|
337 | - return false; |
|
338 | - } |
|
339 | - |
|
340 | - return true; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Assigns a single permission to a user, irregardless of permissions |
|
345 | - * assigned by roles. This is saved to the user's meta information. |
|
346 | - * |
|
347 | - * @param int|string $permission |
|
348 | - * @param int $userId |
|
349 | - * |
|
350 | - * @return bool|null |
|
351 | - */ |
|
352 | - public function addPermissionToUser($permission, int $userId) |
|
353 | - { |
|
354 | - $permissionId = $this->getPermissionID($permission); |
|
355 | - |
|
356 | - if (! is_numeric($permissionId)) |
|
357 | - { |
|
358 | - return null; |
|
359 | - } |
|
360 | - |
|
361 | - if (! Events::trigger('beforeAddPermissionToUser', $userId, $permissionId)) |
|
362 | - { |
|
363 | - return false; |
|
364 | - } |
|
365 | - |
|
366 | - $user = $this->userModel->find($userId); |
|
367 | - |
|
368 | - if (! $user) |
|
369 | - { |
|
370 | - $this->error = lang('Auth.userNotFound', [$userId]); |
|
371 | - return false; |
|
372 | - } |
|
373 | - |
|
374 | - /** @var User $user */ |
|
375 | - $permissions = $user->getPermissions(); |
|
376 | - |
|
377 | - if (! in_array($permissionId, $permissions)) |
|
378 | - { |
|
379 | - $this->permissionModel->addPermissionToUser($permissionId, $user->id); |
|
380 | - } |
|
381 | - |
|
382 | - Events::trigger('didAddPermissionToUser', $userId, $permissionId); |
|
383 | - |
|
384 | - return true; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Removes a single permission from a user. Only applies to permissions |
|
389 | - * that have been assigned with addPermissionToUser, not to permissions |
|
390 | - * inherited based on groups they belong to. |
|
391 | - * |
|
392 | - * @param int|string $permission |
|
393 | - * @param int $userId |
|
394 | - * |
|
395 | - * @return bool|mixed|null |
|
396 | - */ |
|
397 | - public function removePermissionFromUser($permission, int $userId) |
|
398 | - { |
|
399 | - $permissionId = $this->getPermissionID($permission); |
|
400 | - |
|
401 | - if (! is_numeric($permissionId)) |
|
402 | - { |
|
403 | - return false; |
|
404 | - } |
|
405 | - |
|
406 | - if (empty($userId) || ! is_numeric($userId)) |
|
407 | - { |
|
408 | - return null; |
|
409 | - } |
|
410 | - |
|
411 | - $userId = (int)$userId; |
|
412 | - |
|
413 | - if (! Events::trigger('beforeRemovePermissionFromUser', $userId, $permissionId)) |
|
414 | - { |
|
415 | - return false; |
|
416 | - } |
|
417 | - |
|
418 | - return $this->permissionModel->removePermissionFromUser($permissionId, $userId); |
|
419 | - } |
|
420 | - |
|
421 | - /** |
|
422 | - * Checks to see if a user has private permission assigned to it. |
|
423 | - * |
|
424 | - * @param int|string $userId |
|
425 | - * @param int|string $permission |
|
426 | - * |
|
427 | - * @return bool|null |
|
428 | - */ |
|
429 | - public function doesUserHavePermission($userId, $permission) |
|
430 | - { |
|
431 | - $permissionId = $this->getPermissionID($permission); |
|
432 | - |
|
433 | - if (! is_numeric($permissionId)) |
|
434 | - { |
|
435 | - return false; |
|
436 | - } |
|
437 | - |
|
438 | - if (empty($userId) || ! is_numeric($userId)) |
|
439 | - { |
|
440 | - return null; |
|
441 | - } |
|
442 | - |
|
443 | - return $this->permissionModel->doesUserHavePermission($userId, $permissionId); |
|
444 | - } |
|
445 | - |
|
446 | - //-------------------------------------------------------------------- |
|
447 | - // Groups |
|
448 | - //-------------------------------------------------------------------- |
|
449 | - |
|
450 | - /** |
|
451 | - * Grabs the details about a single group. |
|
452 | - * |
|
453 | - * @param int|string $group |
|
454 | - * |
|
455 | - * @return object|null |
|
456 | - */ |
|
457 | - public function group($group) |
|
458 | - { |
|
459 | - if (is_numeric($group)) |
|
460 | - { |
|
461 | - return $this->groupModel->find((int) $group); |
|
462 | - } |
|
463 | - |
|
464 | - return $this->groupModel->where('name', $group)->first(); |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Grabs an array of all groups. |
|
469 | - * |
|
470 | - * @return array of objects |
|
471 | - */ |
|
472 | - public function groups() |
|
473 | - { |
|
474 | - return $this->groupModel |
|
475 | - ->orderBy('name', 'asc') |
|
476 | - ->findAll(); |
|
477 | - } |
|
478 | - |
|
479 | - |
|
480 | - /** |
|
481 | - * @param string $name |
|
482 | - * @param string $description |
|
483 | - * |
|
484 | - * @return mixed |
|
485 | - */ |
|
486 | - public function createGroup(string $name, string $description = '') |
|
487 | - { |
|
488 | - $data = [ |
|
489 | - 'name' => $name, |
|
490 | - 'description' => $description, |
|
491 | - ]; |
|
492 | - |
|
493 | - $validation = service('validation', null, false); |
|
494 | - $validation->setRules([ |
|
495 | - 'name' => 'required|max_length[255]|is_unique[auth_groups.name]', |
|
496 | - 'description' => 'max_length[255]', |
|
497 | - ]); |
|
498 | - |
|
499 | - if (! $validation->run($data)) |
|
500 | - { |
|
501 | - $this->error = $validation->getErrors(); |
|
502 | - return false; |
|
503 | - } |
|
504 | - |
|
505 | - $id = $this->groupModel->skipValidation()->insert($data); |
|
506 | - |
|
507 | - if (is_numeric($id)) |
|
508 | - { |
|
509 | - return (int)$id; |
|
510 | - } |
|
511 | - |
|
512 | - $this->error = $this->groupModel->errors(); |
|
513 | - |
|
514 | - return false; |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Deletes a single group. |
|
519 | - * |
|
520 | - * @param int $groupId |
|
521 | - * |
|
522 | - * @return bool |
|
523 | - */ |
|
524 | - public function deleteGroup(int $groupId) |
|
525 | - { |
|
526 | - if (! $this->groupModel->delete($groupId)) |
|
527 | - { |
|
528 | - $this->error = $this->groupModel->errors(); |
|
529 | - |
|
530 | - return false; |
|
531 | - } |
|
532 | - |
|
533 | - return true; |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * Updates a single group's information. |
|
538 | - * |
|
539 | - * @param int $id |
|
540 | - * @param string $name |
|
541 | - * @param string $description |
|
542 | - * |
|
543 | - * @return mixed |
|
544 | - */ |
|
545 | - public function updateGroup(int $id, string $name, string $description = '') |
|
546 | - { |
|
547 | - $data = [ |
|
548 | - 'name' => $name, |
|
549 | - ]; |
|
550 | - |
|
551 | - if (! empty($description)) |
|
552 | - { |
|
553 | - $data['description'] = $description; |
|
554 | - } |
|
555 | - |
|
556 | - if (! $this->groupModel->update($id, $data)) |
|
557 | - { |
|
558 | - $this->error = $this->groupModel->errors(); |
|
559 | - |
|
560 | - return false; |
|
561 | - } |
|
562 | - |
|
563 | - return true; |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Given a group, will return the group ID. The group can be either |
|
568 | - * the ID or the name of the group. |
|
569 | - * |
|
570 | - * @param int|string $group |
|
571 | - * |
|
572 | - * @return int|false |
|
573 | - */ |
|
574 | - protected function getGroupID($group) |
|
575 | - { |
|
576 | - if (is_numeric($group)) |
|
577 | - { |
|
578 | - return (int)$group; |
|
579 | - } |
|
580 | - |
|
581 | - $g = $this->groupModel->where('name', $group)->first(); |
|
582 | - |
|
583 | - if (! $g) |
|
584 | - { |
|
585 | - $this->error = lang('Auth.groupNotFound', [$group]); |
|
586 | - |
|
587 | - return false; |
|
588 | - } |
|
589 | - |
|
590 | - return (int)$g->id; |
|
591 | - } |
|
592 | - |
|
593 | - //-------------------------------------------------------------------- |
|
594 | - // Permissions |
|
595 | - //-------------------------------------------------------------------- |
|
596 | - |
|
597 | - /** |
|
598 | - * Returns the details about a single permission. |
|
599 | - * |
|
600 | - * @param int|string $permission |
|
601 | - * |
|
602 | - * @return object|null |
|
603 | - */ |
|
604 | - public function permission($permission) |
|
605 | - { |
|
606 | - if (is_numeric($permission)) |
|
607 | - { |
|
608 | - return $this->permissionModel->find((int)$permission); |
|
609 | - } |
|
610 | - |
|
611 | - return $this->permissionModel->like('name', $permission, 'none', null, true)->first(); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * Returns an array of all permissions in the system. |
|
616 | - * |
|
617 | - * @return mixed |
|
618 | - */ |
|
619 | - public function permissions() |
|
620 | - { |
|
621 | - return $this->permissionModel->findAll(); |
|
622 | - } |
|
623 | - |
|
624 | - /** |
|
625 | - * Creates a single permission. |
|
626 | - * |
|
627 | - * @param string $name |
|
628 | - * @param string $description |
|
629 | - * |
|
630 | - * @return mixed |
|
631 | - */ |
|
632 | - public function createPermission(string $name, string $description = '') |
|
633 | - { |
|
634 | - $data = [ |
|
635 | - 'name' => $name, |
|
636 | - 'description' => $description, |
|
637 | - ]; |
|
638 | - |
|
639 | - $validation = service('validation', null, false); |
|
640 | - $validation->setRules([ |
|
641 | - 'name' => 'required|max_length[255]|is_unique[auth_permissions.name]', |
|
642 | - 'description' => 'max_length[255]', |
|
643 | - ]); |
|
644 | - |
|
645 | - if (! $validation->run($data)) |
|
646 | - { |
|
647 | - $this->error = $validation->getErrors(); |
|
648 | - return false; |
|
649 | - } |
|
650 | - |
|
651 | - $id = $this->permissionModel->skipValidation()->insert($data); |
|
652 | - |
|
653 | - if (is_numeric($id)) |
|
654 | - { |
|
655 | - return (int)$id; |
|
656 | - } |
|
657 | - |
|
658 | - $this->error = $this->permissionModel->errors(); |
|
659 | - |
|
660 | - return false; |
|
661 | - } |
|
662 | - |
|
663 | - /** |
|
664 | - * Deletes a single permission and removes that permission from all groups. |
|
665 | - * |
|
666 | - * @param int $permissionIdId |
|
667 | - * |
|
668 | - * @return mixed |
|
669 | - */ |
|
670 | - public function deletePermission(int $permissionIdId) |
|
671 | - { |
|
672 | - if (! $this->permissionModel->delete($permissionIdId)) |
|
673 | - { |
|
674 | - $this->error = $this->permissionModel->errors(); |
|
675 | - |
|
676 | - return false; |
|
677 | - } |
|
678 | - |
|
679 | - // Remove the permission from all groups |
|
680 | - $this->groupModel->removePermissionFromAllGroups($permissionIdId); |
|
681 | - |
|
682 | - return true; |
|
683 | - } |
|
684 | - |
|
685 | - /** |
|
686 | - * Updates the details for a single permission. |
|
687 | - * |
|
688 | - * @param int $id |
|
689 | - * @param string $name |
|
690 | - * @param string $description |
|
691 | - * |
|
692 | - * @return bool |
|
693 | - */ |
|
694 | - public function updatePermission(int $id, string $name, string $description = '') |
|
695 | - { |
|
696 | - $data = [ |
|
697 | - 'name' => $name, |
|
698 | - ]; |
|
699 | - |
|
700 | - if (! empty($description)) |
|
701 | - { |
|
702 | - $data['description'] = $description; |
|
703 | - } |
|
704 | - |
|
705 | - if (! $this->permissionModel->update((int)$id, $data)) |
|
706 | - { |
|
707 | - $this->error = $this->permissionModel->errors(); |
|
708 | - |
|
709 | - return false; |
|
710 | - } |
|
711 | - |
|
712 | - return true; |
|
713 | - } |
|
714 | - |
|
715 | - /** |
|
716 | - * Verifies that a permission (either ID or the name) exists and returns |
|
717 | - * the permission ID. |
|
718 | - * |
|
719 | - * @param int|string $permission |
|
720 | - * |
|
721 | - * @return int|false |
|
722 | - */ |
|
723 | - protected function getPermissionID($permission) |
|
724 | - { |
|
725 | - // If it's a number, we're done here. |
|
726 | - if (is_numeric($permission)) |
|
727 | - { |
|
728 | - return (int) $permission; |
|
729 | - } |
|
730 | - |
|
731 | - // Otherwise, pull it from the database. |
|
732 | - $p = $this->permissionModel->asObject()->where('name', $permission)->first(); |
|
733 | - |
|
734 | - if (! $p) |
|
735 | - { |
|
736 | - $this->error = lang('Auth.permissionNotFound', [$permission]); |
|
737 | - |
|
738 | - return false; |
|
739 | - } |
|
740 | - |
|
741 | - return (int) $p->id; |
|
742 | - } |
|
743 | - |
|
744 | - /** |
|
745 | - * Returns an array of all permissions in the system for a group |
|
746 | - * The group can be either the ID or the name of the group. |
|
747 | - * |
|
748 | - * @param int|string $group |
|
749 | - * |
|
750 | - * @return mixed |
|
751 | - */ |
|
752 | - public function groupPermissions($group) |
|
753 | - { |
|
754 | - if (is_numeric($group)) |
|
755 | - { |
|
756 | - return $this->groupModel->getPermissionsForGroup($group); |
|
757 | - } |
|
758 | - else |
|
759 | - { |
|
760 | - $g = $this->groupModel->where('name', $group)->first(); |
|
761 | - return $this->groupModel->getPermissionsForGroup($g->id); |
|
762 | - } |
|
763 | - } |
|
764 | - |
|
765 | - /** |
|
766 | - * Returns an array of all users in a group |
|
767 | - * The group can be either the ID or the name of the group. |
|
768 | - * |
|
769 | - * @param int|string $group |
|
770 | - * |
|
771 | - * @return mixed |
|
772 | - */ |
|
773 | - public function usersInGroup($group) |
|
774 | - { |
|
775 | - if (is_numeric($group)) |
|
776 | - { |
|
777 | - return $this->groupModel->getUsersForGroup($group); |
|
778 | - } |
|
779 | - else |
|
780 | - { |
|
781 | - $g = $this->groupModel->where('name', $group)->first(); |
|
782 | - return $this->groupModel->getUsersForGroup($g->id); |
|
783 | - } |
|
784 | - } |
|
12 | + /** |
|
13 | + * @var array|string|null |
|
14 | + */ |
|
15 | + protected $error; |
|
16 | + |
|
17 | + /** |
|
18 | + * The group model to use. Usually the class noted |
|
19 | + * below (or an extension thereof) but can be any |
|
20 | + * compatible CodeIgniter Model. |
|
21 | + * |
|
22 | + * @var GroupModel |
|
23 | + */ |
|
24 | + protected $groupModel; |
|
25 | + |
|
26 | + /** |
|
27 | + * The group model to use. Usually the class noted |
|
28 | + * below (or an extension thereof) but can be any |
|
29 | + * compatible CodeIgniter Model. |
|
30 | + * |
|
31 | + * @var PermissionModel |
|
32 | + */ |
|
33 | + protected $permissionModel; |
|
34 | + |
|
35 | + /** |
|
36 | + * The group model to use. Usually the class noted |
|
37 | + * below (or an extension thereof) but can be any |
|
38 | + * compatible CodeIgniter Model. |
|
39 | + * |
|
40 | + * @var UserModel |
|
41 | + */ |
|
42 | + protected $userModel = null; |
|
43 | + |
|
44 | + /** |
|
45 | + * Stores the models. |
|
46 | + * |
|
47 | + * @param GroupModel $groupModel |
|
48 | + * @param PermissionModel $permissionModel |
|
49 | + * |
|
50 | + * @return array|string|null |
|
51 | + */ |
|
52 | + public function __construct(Model $groupModel, Model $permissionModel) |
|
53 | + { |
|
54 | + $this->groupModel = $groupModel; |
|
55 | + $this->permissionModel = $permissionModel; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Returns any error(s) from the last call. |
|
60 | + * |
|
61 | + * @return array|string|null |
|
62 | + */ |
|
63 | + public function error() |
|
64 | + { |
|
65 | + return $this->error; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Allows the consuming application to pass in a reference to the |
|
70 | + * model that should be used. |
|
71 | + * |
|
72 | + * @param UserModel $model |
|
73 | + * |
|
74 | + * @return mixed |
|
75 | + */ |
|
76 | + public function setUserModel(Model $model) |
|
77 | + { |
|
78 | + $this->userModel = $model; |
|
79 | + |
|
80 | + return $this; |
|
81 | + } |
|
82 | + |
|
83 | + //-------------------------------------------------------------------- |
|
84 | + // Actions |
|
85 | + //-------------------------------------------------------------------- |
|
86 | + |
|
87 | + /** |
|
88 | + * Checks to see if a user is in a group. |
|
89 | + * |
|
90 | + * Groups can be either a string, with the name of the group, an INT |
|
91 | + * with the ID of the group, or an array of strings/ids that the |
|
92 | + * user must belong to ONE of. (It's an OR check not an AND check) |
|
93 | + * |
|
94 | + * @param mixed $groups |
|
95 | + * @param int $userId |
|
96 | + * |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + public function inGroup($groups, int $userId) |
|
100 | + { |
|
101 | + if ($userId === 0) |
|
102 | + { |
|
103 | + return false; |
|
104 | + } |
|
105 | + |
|
106 | + if (! is_array($groups)) |
|
107 | + { |
|
108 | + $groups = [$groups]; |
|
109 | + } |
|
110 | + |
|
111 | + $userGroups = $this->groupModel->getGroupsForUser((int) $userId); |
|
112 | + |
|
113 | + if (empty($userGroups)) |
|
114 | + { |
|
115 | + return false; |
|
116 | + } |
|
117 | + |
|
118 | + foreach ($groups as $group) |
|
119 | + { |
|
120 | + if (is_numeric($group)) |
|
121 | + { |
|
122 | + $ids = array_column($userGroups, 'group_id'); |
|
123 | + if (in_array($group, $ids)) |
|
124 | + { |
|
125 | + return true; |
|
126 | + } |
|
127 | + } |
|
128 | + else if (is_string($group)) |
|
129 | + { |
|
130 | + $names = array_column($userGroups, 'name'); |
|
131 | + |
|
132 | + if (in_array($group, $names)) |
|
133 | + { |
|
134 | + return true; |
|
135 | + } |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + return false; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Checks a user's groups to see if they have the specified permission. |
|
144 | + * |
|
145 | + * @param int|string $permission Permission ID or name |
|
146 | + * @param int $userId |
|
147 | + * |
|
148 | + * @return mixed |
|
149 | + */ |
|
150 | + public function hasPermission($permission, int $userId) |
|
151 | + { |
|
152 | + // @phpstan-ignore-next-line |
|
153 | + if (empty($permission) || (! is_string($permission) && ! is_numeric($permission))) |
|
154 | + { |
|
155 | + return null; |
|
156 | + } |
|
157 | + |
|
158 | + if (empty($userId) || ! is_numeric($userId)) |
|
159 | + { |
|
160 | + return null; |
|
161 | + } |
|
162 | + |
|
163 | + // Get the Permission ID |
|
164 | + $permissionId = $this->getPermissionID($permission); |
|
165 | + |
|
166 | + if (! is_numeric($permissionId)) |
|
167 | + { |
|
168 | + return false; |
|
169 | + } |
|
170 | + |
|
171 | + // First check the permission model. If that exists, then we're golden. |
|
172 | + if ($this->permissionModel->doesUserHavePermission($userId, (int)$permissionId)) |
|
173 | + { |
|
174 | + return true; |
|
175 | + } |
|
176 | + |
|
177 | + // Still here? Then we have one last check to make - any user private permissions. |
|
178 | + return $this->doesUserHavePermission($userId, (int)$permissionId); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Makes a member a part of a group. |
|
183 | + * |
|
184 | + * @param int $userid |
|
185 | + * @param mixed $group Either ID or name, fails on anything else |
|
186 | + * |
|
187 | + * @return bool|null |
|
188 | + */ |
|
189 | + public function addUserToGroup(int $userid, $group) |
|
190 | + { |
|
191 | + if (empty($userid) || ! is_numeric($userid)) |
|
192 | + { |
|
193 | + return null; |
|
194 | + } |
|
195 | + |
|
196 | + if (empty($group) || (! is_numeric($group) && ! is_string($group))) |
|
197 | + { |
|
198 | + return null; |
|
199 | + } |
|
200 | + |
|
201 | + $groupId = $this->getGroupID($group); |
|
202 | + |
|
203 | + if (! Events::trigger('beforeAddUserToGroup', $userid, $groupId)) |
|
204 | + { |
|
205 | + return false; |
|
206 | + } |
|
207 | + |
|
208 | + // Group ID |
|
209 | + if (! is_numeric($groupId)) |
|
210 | + { |
|
211 | + return null; |
|
212 | + } |
|
213 | + |
|
214 | + if (! $this->groupModel->addUserToGroup($userid, (int) $groupId)) |
|
215 | + { |
|
216 | + $this->error = $this->groupModel->errors(); |
|
217 | + |
|
218 | + return false; |
|
219 | + } |
|
220 | + |
|
221 | + Events::trigger('didAddUserToGroup', $userid, $groupId); |
|
222 | + |
|
223 | + return true; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Removes a single user from a group. |
|
228 | + * |
|
229 | + * @param int $userId |
|
230 | + * @param mixed $group |
|
231 | + * |
|
232 | + * @return mixed |
|
233 | + */ |
|
234 | + public function removeUserFromGroup(int $userId, $group) |
|
235 | + { |
|
236 | + if (empty($userId) || ! is_numeric($userId)) |
|
237 | + { |
|
238 | + return null; |
|
239 | + } |
|
240 | + |
|
241 | + if (empty($group) || (! is_numeric($group) && ! is_string($group))) |
|
242 | + { |
|
243 | + return null; |
|
244 | + } |
|
245 | + |
|
246 | + $groupId = $this->getGroupID($group); |
|
247 | + |
|
248 | + if (! Events::trigger('beforeRemoveUserFromGroup', $userId, $groupId)) |
|
249 | + { |
|
250 | + return false; |
|
251 | + } |
|
252 | + |
|
253 | + // Group ID |
|
254 | + if (! is_numeric($groupId)) |
|
255 | + { |
|
256 | + return false; |
|
257 | + } |
|
258 | + |
|
259 | + if (! $this->groupModel->removeUserFromGroup($userId, $groupId)) |
|
260 | + { |
|
261 | + $this->error = $this->groupModel->errors(); |
|
262 | + |
|
263 | + return false; |
|
264 | + } |
|
265 | + |
|
266 | + Events::trigger('didRemoveUserFromGroup', $userId, $groupId); |
|
267 | + |
|
268 | + return true; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Adds a single permission to a single group. |
|
273 | + * |
|
274 | + * @param int|string $permission |
|
275 | + * @param int|string $group |
|
276 | + * |
|
277 | + * @return mixed |
|
278 | + */ |
|
279 | + public function addPermissionToGroup($permission, $group) |
|
280 | + { |
|
281 | + $permissionId = $this->getPermissionID($permission); |
|
282 | + $groupId = $this->getGroupID($group); |
|
283 | + |
|
284 | + // Permission ID |
|
285 | + if (! is_numeric($permissionId)) |
|
286 | + { |
|
287 | + return false; |
|
288 | + } |
|
289 | + |
|
290 | + // Group ID |
|
291 | + if (! is_numeric($groupId)) |
|
292 | + { |
|
293 | + return false; |
|
294 | + } |
|
295 | + |
|
296 | + // Remove it! |
|
297 | + if (! $this->groupModel->addPermissionToGroup($permissionId, $groupId)) |
|
298 | + { |
|
299 | + $this->error = $this->groupModel->errors(); |
|
300 | + |
|
301 | + return false; |
|
302 | + } |
|
303 | + |
|
304 | + return true; |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Removes a single permission from a group. |
|
309 | + * |
|
310 | + * @param int|string $permission |
|
311 | + * @param int|string $group |
|
312 | + * |
|
313 | + * @return mixed |
|
314 | + */ |
|
315 | + public function removePermissionFromGroup($permission, $group) |
|
316 | + { |
|
317 | + $permissionId = $this->getPermissionID($permission); |
|
318 | + $groupId = $this->getGroupID($group); |
|
319 | + |
|
320 | + // Permission ID |
|
321 | + if (! is_numeric($permissionId)) |
|
322 | + { |
|
323 | + return false; |
|
324 | + } |
|
325 | + |
|
326 | + // Group ID |
|
327 | + if (! is_numeric($groupId)) |
|
328 | + { |
|
329 | + return false; |
|
330 | + } |
|
331 | + |
|
332 | + // Remove it! |
|
333 | + if (! $this->groupModel->removePermissionFromGroup($permissionId, $groupId)) |
|
334 | + { |
|
335 | + $this->error = $this->groupModel->errors(); |
|
336 | + |
|
337 | + return false; |
|
338 | + } |
|
339 | + |
|
340 | + return true; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Assigns a single permission to a user, irregardless of permissions |
|
345 | + * assigned by roles. This is saved to the user's meta information. |
|
346 | + * |
|
347 | + * @param int|string $permission |
|
348 | + * @param int $userId |
|
349 | + * |
|
350 | + * @return bool|null |
|
351 | + */ |
|
352 | + public function addPermissionToUser($permission, int $userId) |
|
353 | + { |
|
354 | + $permissionId = $this->getPermissionID($permission); |
|
355 | + |
|
356 | + if (! is_numeric($permissionId)) |
|
357 | + { |
|
358 | + return null; |
|
359 | + } |
|
360 | + |
|
361 | + if (! Events::trigger('beforeAddPermissionToUser', $userId, $permissionId)) |
|
362 | + { |
|
363 | + return false; |
|
364 | + } |
|
365 | + |
|
366 | + $user = $this->userModel->find($userId); |
|
367 | + |
|
368 | + if (! $user) |
|
369 | + { |
|
370 | + $this->error = lang('Auth.userNotFound', [$userId]); |
|
371 | + return false; |
|
372 | + } |
|
373 | + |
|
374 | + /** @var User $user */ |
|
375 | + $permissions = $user->getPermissions(); |
|
376 | + |
|
377 | + if (! in_array($permissionId, $permissions)) |
|
378 | + { |
|
379 | + $this->permissionModel->addPermissionToUser($permissionId, $user->id); |
|
380 | + } |
|
381 | + |
|
382 | + Events::trigger('didAddPermissionToUser', $userId, $permissionId); |
|
383 | + |
|
384 | + return true; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Removes a single permission from a user. Only applies to permissions |
|
389 | + * that have been assigned with addPermissionToUser, not to permissions |
|
390 | + * inherited based on groups they belong to. |
|
391 | + * |
|
392 | + * @param int|string $permission |
|
393 | + * @param int $userId |
|
394 | + * |
|
395 | + * @return bool|mixed|null |
|
396 | + */ |
|
397 | + public function removePermissionFromUser($permission, int $userId) |
|
398 | + { |
|
399 | + $permissionId = $this->getPermissionID($permission); |
|
400 | + |
|
401 | + if (! is_numeric($permissionId)) |
|
402 | + { |
|
403 | + return false; |
|
404 | + } |
|
405 | + |
|
406 | + if (empty($userId) || ! is_numeric($userId)) |
|
407 | + { |
|
408 | + return null; |
|
409 | + } |
|
410 | + |
|
411 | + $userId = (int)$userId; |
|
412 | + |
|
413 | + if (! Events::trigger('beforeRemovePermissionFromUser', $userId, $permissionId)) |
|
414 | + { |
|
415 | + return false; |
|
416 | + } |
|
417 | + |
|
418 | + return $this->permissionModel->removePermissionFromUser($permissionId, $userId); |
|
419 | + } |
|
420 | + |
|
421 | + /** |
|
422 | + * Checks to see if a user has private permission assigned to it. |
|
423 | + * |
|
424 | + * @param int|string $userId |
|
425 | + * @param int|string $permission |
|
426 | + * |
|
427 | + * @return bool|null |
|
428 | + */ |
|
429 | + public function doesUserHavePermission($userId, $permission) |
|
430 | + { |
|
431 | + $permissionId = $this->getPermissionID($permission); |
|
432 | + |
|
433 | + if (! is_numeric($permissionId)) |
|
434 | + { |
|
435 | + return false; |
|
436 | + } |
|
437 | + |
|
438 | + if (empty($userId) || ! is_numeric($userId)) |
|
439 | + { |
|
440 | + return null; |
|
441 | + } |
|
442 | + |
|
443 | + return $this->permissionModel->doesUserHavePermission($userId, $permissionId); |
|
444 | + } |
|
445 | + |
|
446 | + //-------------------------------------------------------------------- |
|
447 | + // Groups |
|
448 | + //-------------------------------------------------------------------- |
|
449 | + |
|
450 | + /** |
|
451 | + * Grabs the details about a single group. |
|
452 | + * |
|
453 | + * @param int|string $group |
|
454 | + * |
|
455 | + * @return object|null |
|
456 | + */ |
|
457 | + public function group($group) |
|
458 | + { |
|
459 | + if (is_numeric($group)) |
|
460 | + { |
|
461 | + return $this->groupModel->find((int) $group); |
|
462 | + } |
|
463 | + |
|
464 | + return $this->groupModel->where('name', $group)->first(); |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Grabs an array of all groups. |
|
469 | + * |
|
470 | + * @return array of objects |
|
471 | + */ |
|
472 | + public function groups() |
|
473 | + { |
|
474 | + return $this->groupModel |
|
475 | + ->orderBy('name', 'asc') |
|
476 | + ->findAll(); |
|
477 | + } |
|
478 | + |
|
479 | + |
|
480 | + /** |
|
481 | + * @param string $name |
|
482 | + * @param string $description |
|
483 | + * |
|
484 | + * @return mixed |
|
485 | + */ |
|
486 | + public function createGroup(string $name, string $description = '') |
|
487 | + { |
|
488 | + $data = [ |
|
489 | + 'name' => $name, |
|
490 | + 'description' => $description, |
|
491 | + ]; |
|
492 | + |
|
493 | + $validation = service('validation', null, false); |
|
494 | + $validation->setRules([ |
|
495 | + 'name' => 'required|max_length[255]|is_unique[auth_groups.name]', |
|
496 | + 'description' => 'max_length[255]', |
|
497 | + ]); |
|
498 | + |
|
499 | + if (! $validation->run($data)) |
|
500 | + { |
|
501 | + $this->error = $validation->getErrors(); |
|
502 | + return false; |
|
503 | + } |
|
504 | + |
|
505 | + $id = $this->groupModel->skipValidation()->insert($data); |
|
506 | + |
|
507 | + if (is_numeric($id)) |
|
508 | + { |
|
509 | + return (int)$id; |
|
510 | + } |
|
511 | + |
|
512 | + $this->error = $this->groupModel->errors(); |
|
513 | + |
|
514 | + return false; |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Deletes a single group. |
|
519 | + * |
|
520 | + * @param int $groupId |
|
521 | + * |
|
522 | + * @return bool |
|
523 | + */ |
|
524 | + public function deleteGroup(int $groupId) |
|
525 | + { |
|
526 | + if (! $this->groupModel->delete($groupId)) |
|
527 | + { |
|
528 | + $this->error = $this->groupModel->errors(); |
|
529 | + |
|
530 | + return false; |
|
531 | + } |
|
532 | + |
|
533 | + return true; |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * Updates a single group's information. |
|
538 | + * |
|
539 | + * @param int $id |
|
540 | + * @param string $name |
|
541 | + * @param string $description |
|
542 | + * |
|
543 | + * @return mixed |
|
544 | + */ |
|
545 | + public function updateGroup(int $id, string $name, string $description = '') |
|
546 | + { |
|
547 | + $data = [ |
|
548 | + 'name' => $name, |
|
549 | + ]; |
|
550 | + |
|
551 | + if (! empty($description)) |
|
552 | + { |
|
553 | + $data['description'] = $description; |
|
554 | + } |
|
555 | + |
|
556 | + if (! $this->groupModel->update($id, $data)) |
|
557 | + { |
|
558 | + $this->error = $this->groupModel->errors(); |
|
559 | + |
|
560 | + return false; |
|
561 | + } |
|
562 | + |
|
563 | + return true; |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Given a group, will return the group ID. The group can be either |
|
568 | + * the ID or the name of the group. |
|
569 | + * |
|
570 | + * @param int|string $group |
|
571 | + * |
|
572 | + * @return int|false |
|
573 | + */ |
|
574 | + protected function getGroupID($group) |
|
575 | + { |
|
576 | + if (is_numeric($group)) |
|
577 | + { |
|
578 | + return (int)$group; |
|
579 | + } |
|
580 | + |
|
581 | + $g = $this->groupModel->where('name', $group)->first(); |
|
582 | + |
|
583 | + if (! $g) |
|
584 | + { |
|
585 | + $this->error = lang('Auth.groupNotFound', [$group]); |
|
586 | + |
|
587 | + return false; |
|
588 | + } |
|
589 | + |
|
590 | + return (int)$g->id; |
|
591 | + } |
|
592 | + |
|
593 | + //-------------------------------------------------------------------- |
|
594 | + // Permissions |
|
595 | + //-------------------------------------------------------------------- |
|
596 | + |
|
597 | + /** |
|
598 | + * Returns the details about a single permission. |
|
599 | + * |
|
600 | + * @param int|string $permission |
|
601 | + * |
|
602 | + * @return object|null |
|
603 | + */ |
|
604 | + public function permission($permission) |
|
605 | + { |
|
606 | + if (is_numeric($permission)) |
|
607 | + { |
|
608 | + return $this->permissionModel->find((int)$permission); |
|
609 | + } |
|
610 | + |
|
611 | + return $this->permissionModel->like('name', $permission, 'none', null, true)->first(); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * Returns an array of all permissions in the system. |
|
616 | + * |
|
617 | + * @return mixed |
|
618 | + */ |
|
619 | + public function permissions() |
|
620 | + { |
|
621 | + return $this->permissionModel->findAll(); |
|
622 | + } |
|
623 | + |
|
624 | + /** |
|
625 | + * Creates a single permission. |
|
626 | + * |
|
627 | + * @param string $name |
|
628 | + * @param string $description |
|
629 | + * |
|
630 | + * @return mixed |
|
631 | + */ |
|
632 | + public function createPermission(string $name, string $description = '') |
|
633 | + { |
|
634 | + $data = [ |
|
635 | + 'name' => $name, |
|
636 | + 'description' => $description, |
|
637 | + ]; |
|
638 | + |
|
639 | + $validation = service('validation', null, false); |
|
640 | + $validation->setRules([ |
|
641 | + 'name' => 'required|max_length[255]|is_unique[auth_permissions.name]', |
|
642 | + 'description' => 'max_length[255]', |
|
643 | + ]); |
|
644 | + |
|
645 | + if (! $validation->run($data)) |
|
646 | + { |
|
647 | + $this->error = $validation->getErrors(); |
|
648 | + return false; |
|
649 | + } |
|
650 | + |
|
651 | + $id = $this->permissionModel->skipValidation()->insert($data); |
|
652 | + |
|
653 | + if (is_numeric($id)) |
|
654 | + { |
|
655 | + return (int)$id; |
|
656 | + } |
|
657 | + |
|
658 | + $this->error = $this->permissionModel->errors(); |
|
659 | + |
|
660 | + return false; |
|
661 | + } |
|
662 | + |
|
663 | + /** |
|
664 | + * Deletes a single permission and removes that permission from all groups. |
|
665 | + * |
|
666 | + * @param int $permissionIdId |
|
667 | + * |
|
668 | + * @return mixed |
|
669 | + */ |
|
670 | + public function deletePermission(int $permissionIdId) |
|
671 | + { |
|
672 | + if (! $this->permissionModel->delete($permissionIdId)) |
|
673 | + { |
|
674 | + $this->error = $this->permissionModel->errors(); |
|
675 | + |
|
676 | + return false; |
|
677 | + } |
|
678 | + |
|
679 | + // Remove the permission from all groups |
|
680 | + $this->groupModel->removePermissionFromAllGroups($permissionIdId); |
|
681 | + |
|
682 | + return true; |
|
683 | + } |
|
684 | + |
|
685 | + /** |
|
686 | + * Updates the details for a single permission. |
|
687 | + * |
|
688 | + * @param int $id |
|
689 | + * @param string $name |
|
690 | + * @param string $description |
|
691 | + * |
|
692 | + * @return bool |
|
693 | + */ |
|
694 | + public function updatePermission(int $id, string $name, string $description = '') |
|
695 | + { |
|
696 | + $data = [ |
|
697 | + 'name' => $name, |
|
698 | + ]; |
|
699 | + |
|
700 | + if (! empty($description)) |
|
701 | + { |
|
702 | + $data['description'] = $description; |
|
703 | + } |
|
704 | + |
|
705 | + if (! $this->permissionModel->update((int)$id, $data)) |
|
706 | + { |
|
707 | + $this->error = $this->permissionModel->errors(); |
|
708 | + |
|
709 | + return false; |
|
710 | + } |
|
711 | + |
|
712 | + return true; |
|
713 | + } |
|
714 | + |
|
715 | + /** |
|
716 | + * Verifies that a permission (either ID or the name) exists and returns |
|
717 | + * the permission ID. |
|
718 | + * |
|
719 | + * @param int|string $permission |
|
720 | + * |
|
721 | + * @return int|false |
|
722 | + */ |
|
723 | + protected function getPermissionID($permission) |
|
724 | + { |
|
725 | + // If it's a number, we're done here. |
|
726 | + if (is_numeric($permission)) |
|
727 | + { |
|
728 | + return (int) $permission; |
|
729 | + } |
|
730 | + |
|
731 | + // Otherwise, pull it from the database. |
|
732 | + $p = $this->permissionModel->asObject()->where('name', $permission)->first(); |
|
733 | + |
|
734 | + if (! $p) |
|
735 | + { |
|
736 | + $this->error = lang('Auth.permissionNotFound', [$permission]); |
|
737 | + |
|
738 | + return false; |
|
739 | + } |
|
740 | + |
|
741 | + return (int) $p->id; |
|
742 | + } |
|
743 | + |
|
744 | + /** |
|
745 | + * Returns an array of all permissions in the system for a group |
|
746 | + * The group can be either the ID or the name of the group. |
|
747 | + * |
|
748 | + * @param int|string $group |
|
749 | + * |
|
750 | + * @return mixed |
|
751 | + */ |
|
752 | + public function groupPermissions($group) |
|
753 | + { |
|
754 | + if (is_numeric($group)) |
|
755 | + { |
|
756 | + return $this->groupModel->getPermissionsForGroup($group); |
|
757 | + } |
|
758 | + else |
|
759 | + { |
|
760 | + $g = $this->groupModel->where('name', $group)->first(); |
|
761 | + return $this->groupModel->getPermissionsForGroup($g->id); |
|
762 | + } |
|
763 | + } |
|
764 | + |
|
765 | + /** |
|
766 | + * Returns an array of all users in a group |
|
767 | + * The group can be either the ID or the name of the group. |
|
768 | + * |
|
769 | + * @param int|string $group |
|
770 | + * |
|
771 | + * @return mixed |
|
772 | + */ |
|
773 | + public function usersInGroup($group) |
|
774 | + { |
|
775 | + if (is_numeric($group)) |
|
776 | + { |
|
777 | + return $this->groupModel->getUsersForGroup($group); |
|
778 | + } |
|
779 | + else |
|
780 | + { |
|
781 | + $g = $this->groupModel->where('name', $group)->first(); |
|
782 | + return $this->groupModel->getUsersForGroup($g->id); |
|
783 | + } |
|
784 | + } |
|
785 | 785 | |
786 | 786 | } |
@@ -17,99 +17,99 @@ |
||
17 | 17 | |
18 | 18 | class Services extends BaseService |
19 | 19 | { |
20 | - public static function authentication(string $lib = 'local', Model $userModel = null, Model $loginModel = null, bool $getShared = true) |
|
21 | - { |
|
22 | - if ($getShared) |
|
23 | - { |
|
24 | - return self::getSharedInstance('authentication', $lib, $userModel, $loginModel); |
|
25 | - } |
|
26 | - |
|
27 | - $userModel = $userModel ?? model(UserModel::class); |
|
28 | - $loginModel = $loginModel ?? model(LoginModel::class); |
|
29 | - |
|
30 | - /** @var AuthConfig $config */ |
|
31 | - $config = config('Auth'); |
|
32 | - $class = $config->authenticationLibs[$lib]; |
|
33 | - $instance = new $class($config); |
|
34 | - |
|
35 | - return $instance |
|
36 | - ->setUserModel($userModel) |
|
37 | - ->setLoginModel($loginModel); |
|
38 | - } |
|
39 | - |
|
40 | - public static function authorization(Model $groupModel = null, Model $permissionModel = null, Model $userModel = null, bool $getShared = true) |
|
41 | - { |
|
42 | - if ($getShared) |
|
43 | - { |
|
44 | - return self::getSharedInstance('authorization', $groupModel, $permissionModel, $userModel); |
|
45 | - } |
|
46 | - |
|
47 | - $groupModel = $groupModel ?? model(GroupModel::class); |
|
48 | - $permissionModel = $permissionModel ?? model(PermissionModel::class); |
|
49 | - $userModel = $userModel ?? model(UserModel::class); |
|
50 | - |
|
51 | - $instance = new FlatAuthorization($groupModel, $permissionModel); |
|
52 | - |
|
53 | - return $instance->setUserModel($userModel); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Returns an instance of the PasswordValidator. |
|
58 | - * |
|
59 | - * @param AuthConfig|null $config |
|
60 | - * @param bool $getShared |
|
61 | - * |
|
62 | - * @return PasswordValidator |
|
63 | - */ |
|
64 | - public static function passwords(AuthConfig $config = null, bool $getShared = true): PasswordValidator |
|
65 | - { |
|
66 | - if ($getShared) |
|
67 | - { |
|
68 | - return self::getSharedInstance('passwords', $config); |
|
69 | - } |
|
70 | - |
|
71 | - return new PasswordValidator($config ?? config(AuthConfig::class)); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Returns an instance of the Activator. |
|
76 | - * |
|
77 | - * @param AuthConfig|null $config |
|
78 | - * @param bool $getShared |
|
79 | - * |
|
80 | - * @return ActivatorInterface |
|
81 | - */ |
|
82 | - public static function activator(AuthConfig $config = null, bool $getShared = true): ActivatorInterface |
|
83 | - { |
|
84 | - if ($getShared) |
|
85 | - { |
|
86 | - return self::getSharedInstance('activator', $config); |
|
87 | - } |
|
88 | - |
|
89 | - $config = $config ?? config(AuthConfig::class); |
|
90 | - $class = $config->requireActivation ?? UserActivator::class; |
|
91 | - |
|
92 | - return new $class($config); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Returns an instance of the Resetter. |
|
97 | - * |
|
98 | - * @param AuthConfig|null $config |
|
99 | - * @param bool $getShared |
|
100 | - * |
|
101 | - * @return ResetterInterface |
|
102 | - */ |
|
103 | - public static function resetter(AuthConfig $config = null, bool $getShared = true): ResetterInterface |
|
104 | - { |
|
105 | - if ($getShared) |
|
106 | - { |
|
107 | - return self::getSharedInstance('resetter', $config); |
|
108 | - } |
|
109 | - |
|
110 | - $config = $config ?? config(AuthConfig::class); |
|
111 | - $class = $config->activeResetter ?? EmailResetter::class; |
|
112 | - |
|
113 | - return new $class($config); |
|
114 | - } |
|
20 | + public static function authentication(string $lib = 'local', Model $userModel = null, Model $loginModel = null, bool $getShared = true) |
|
21 | + { |
|
22 | + if ($getShared) |
|
23 | + { |
|
24 | + return self::getSharedInstance('authentication', $lib, $userModel, $loginModel); |
|
25 | + } |
|
26 | + |
|
27 | + $userModel = $userModel ?? model(UserModel::class); |
|
28 | + $loginModel = $loginModel ?? model(LoginModel::class); |
|
29 | + |
|
30 | + /** @var AuthConfig $config */ |
|
31 | + $config = config('Auth'); |
|
32 | + $class = $config->authenticationLibs[$lib]; |
|
33 | + $instance = new $class($config); |
|
34 | + |
|
35 | + return $instance |
|
36 | + ->setUserModel($userModel) |
|
37 | + ->setLoginModel($loginModel); |
|
38 | + } |
|
39 | + |
|
40 | + public static function authorization(Model $groupModel = null, Model $permissionModel = null, Model $userModel = null, bool $getShared = true) |
|
41 | + { |
|
42 | + if ($getShared) |
|
43 | + { |
|
44 | + return self::getSharedInstance('authorization', $groupModel, $permissionModel, $userModel); |
|
45 | + } |
|
46 | + |
|
47 | + $groupModel = $groupModel ?? model(GroupModel::class); |
|
48 | + $permissionModel = $permissionModel ?? model(PermissionModel::class); |
|
49 | + $userModel = $userModel ?? model(UserModel::class); |
|
50 | + |
|
51 | + $instance = new FlatAuthorization($groupModel, $permissionModel); |
|
52 | + |
|
53 | + return $instance->setUserModel($userModel); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Returns an instance of the PasswordValidator. |
|
58 | + * |
|
59 | + * @param AuthConfig|null $config |
|
60 | + * @param bool $getShared |
|
61 | + * |
|
62 | + * @return PasswordValidator |
|
63 | + */ |
|
64 | + public static function passwords(AuthConfig $config = null, bool $getShared = true): PasswordValidator |
|
65 | + { |
|
66 | + if ($getShared) |
|
67 | + { |
|
68 | + return self::getSharedInstance('passwords', $config); |
|
69 | + } |
|
70 | + |
|
71 | + return new PasswordValidator($config ?? config(AuthConfig::class)); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Returns an instance of the Activator. |
|
76 | + * |
|
77 | + * @param AuthConfig|null $config |
|
78 | + * @param bool $getShared |
|
79 | + * |
|
80 | + * @return ActivatorInterface |
|
81 | + */ |
|
82 | + public static function activator(AuthConfig $config = null, bool $getShared = true): ActivatorInterface |
|
83 | + { |
|
84 | + if ($getShared) |
|
85 | + { |
|
86 | + return self::getSharedInstance('activator', $config); |
|
87 | + } |
|
88 | + |
|
89 | + $config = $config ?? config(AuthConfig::class); |
|
90 | + $class = $config->requireActivation ?? UserActivator::class; |
|
91 | + |
|
92 | + return new $class($config); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Returns an instance of the Resetter. |
|
97 | + * |
|
98 | + * @param AuthConfig|null $config |
|
99 | + * @param bool $getShared |
|
100 | + * |
|
101 | + * @return ResetterInterface |
|
102 | + */ |
|
103 | + public static function resetter(AuthConfig $config = null, bool $getShared = true): ResetterInterface |
|
104 | + { |
|
105 | + if ($getShared) |
|
106 | + { |
|
107 | + return self::getSharedInstance('resetter', $config); |
|
108 | + } |
|
109 | + |
|
110 | + $config = $config ?? config(AuthConfig::class); |
|
111 | + $class = $config->activeResetter ?? EmailResetter::class; |
|
112 | + |
|
113 | + return new $class($config); |
|
114 | + } |
|
115 | 115 | } |
@@ -4,220 +4,220 @@ discard block |
||
4 | 4 | |
5 | 5 | class Auth extends BaseConfig |
6 | 6 | { |
7 | - /** |
|
8 | - * -------------------------------------------------------------------- |
|
9 | - * Default User Group |
|
10 | - * -------------------------------------------------------------------- |
|
11 | - * |
|
12 | - * The name of a group a user will be added to when they register, |
|
13 | - * i.e. $defaultUserGroup = 'guests'. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - public $defaultUserGroup; |
|
7 | + /** |
|
8 | + * -------------------------------------------------------------------- |
|
9 | + * Default User Group |
|
10 | + * -------------------------------------------------------------------- |
|
11 | + * |
|
12 | + * The name of a group a user will be added to when they register, |
|
13 | + * i.e. $defaultUserGroup = 'guests'. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + public $defaultUserGroup; |
|
18 | 18 | |
19 | - /** |
|
20 | - * -------------------------------------------------------------------- |
|
21 | - * Libraries |
|
22 | - * -------------------------------------------------------------------- |
|
23 | - * |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - public $authenticationLibs = [ |
|
27 | - 'local' => 'Myth\Auth\Authentication\LocalAuthenticator', |
|
28 | - ]; |
|
19 | + /** |
|
20 | + * -------------------------------------------------------------------- |
|
21 | + * Libraries |
|
22 | + * -------------------------------------------------------------------- |
|
23 | + * |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + public $authenticationLibs = [ |
|
27 | + 'local' => 'Myth\Auth\Authentication\LocalAuthenticator', |
|
28 | + ]; |
|
29 | 29 | |
30 | - /** |
|
31 | - * -------------------------------------------------------------------- |
|
32 | - * Views used by Auth Controllers |
|
33 | - * -------------------------------------------------------------------- |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - public $views = [ |
|
38 | - 'login' => 'Myth\Auth\Views\login', |
|
39 | - 'register' => 'Myth\Auth\Views\register', |
|
40 | - 'forgot' => 'Myth\Auth\Views\forgot', |
|
41 | - 'reset' => 'Myth\Auth\Views\reset', |
|
42 | - 'emailForgot' => 'Myth\Auth\Views\emails\forgot', |
|
43 | - 'emailActivation' => 'Myth\Auth\Views\emails\activation', |
|
44 | - ]; |
|
30 | + /** |
|
31 | + * -------------------------------------------------------------------- |
|
32 | + * Views used by Auth Controllers |
|
33 | + * -------------------------------------------------------------------- |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + public $views = [ |
|
38 | + 'login' => 'Myth\Auth\Views\login', |
|
39 | + 'register' => 'Myth\Auth\Views\register', |
|
40 | + 'forgot' => 'Myth\Auth\Views\forgot', |
|
41 | + 'reset' => 'Myth\Auth\Views\reset', |
|
42 | + 'emailForgot' => 'Myth\Auth\Views\emails\forgot', |
|
43 | + 'emailActivation' => 'Myth\Auth\Views\emails\activation', |
|
44 | + ]; |
|
45 | 45 | |
46 | - /** |
|
47 | - * -------------------------------------------------------------------- |
|
48 | - * Layout for the views to extend |
|
49 | - * -------------------------------------------------------------------- |
|
50 | - * |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - public $viewLayout = 'Myth\Auth\Views\layout'; |
|
46 | + /** |
|
47 | + * -------------------------------------------------------------------- |
|
48 | + * Layout for the views to extend |
|
49 | + * -------------------------------------------------------------------- |
|
50 | + * |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + public $viewLayout = 'Myth\Auth\Views\layout'; |
|
54 | 54 | |
55 | - /** |
|
56 | - * -------------------------------------------------------------------- |
|
57 | - * Authentication |
|
58 | - * -------------------------------------------------------------------- |
|
59 | - * |
|
60 | - * Fields that are available to be used as credentials for login. |
|
61 | - * |
|
62 | - * @var string[] |
|
63 | - */ |
|
64 | - public $validFields = [ |
|
65 | - 'email', |
|
66 | - 'username', |
|
67 | - ]; |
|
55 | + /** |
|
56 | + * -------------------------------------------------------------------- |
|
57 | + * Authentication |
|
58 | + * -------------------------------------------------------------------- |
|
59 | + * |
|
60 | + * Fields that are available to be used as credentials for login. |
|
61 | + * |
|
62 | + * @var string[] |
|
63 | + */ |
|
64 | + public $validFields = [ |
|
65 | + 'email', |
|
66 | + 'username', |
|
67 | + ]; |
|
68 | 68 | |
69 | - /** |
|
70 | - * -------------------------------------------------------------------- |
|
71 | - * Additional Fields for "Nothing Personal" |
|
72 | - * -------------------------------------------------------------------- |
|
73 | - * |
|
74 | - * The `NothingPersonalValidator` prevents personal information from |
|
75 | - * being used in passwords. The email and username fields are always |
|
76 | - * considered by the validator. Do not enter those field names here. |
|
77 | - * |
|
78 | - * An extend User Entity might include other personal info such as |
|
79 | - * first and/or last names. `$personalFields` is where you can add |
|
80 | - * fields to be considered as "personal" by the NothingPersonalValidator. |
|
81 | - * |
|
82 | - * For example: |
|
83 | - * $personalFields = ['firstname', 'lastname']; |
|
84 | - * |
|
85 | - * @var string[] |
|
86 | - */ |
|
87 | - public $personalFields = []; |
|
69 | + /** |
|
70 | + * -------------------------------------------------------------------- |
|
71 | + * Additional Fields for "Nothing Personal" |
|
72 | + * -------------------------------------------------------------------- |
|
73 | + * |
|
74 | + * The `NothingPersonalValidator` prevents personal information from |
|
75 | + * being used in passwords. The email and username fields are always |
|
76 | + * considered by the validator. Do not enter those field names here. |
|
77 | + * |
|
78 | + * An extend User Entity might include other personal info such as |
|
79 | + * first and/or last names. `$personalFields` is where you can add |
|
80 | + * fields to be considered as "personal" by the NothingPersonalValidator. |
|
81 | + * |
|
82 | + * For example: |
|
83 | + * $personalFields = ['firstname', 'lastname']; |
|
84 | + * |
|
85 | + * @var string[] |
|
86 | + */ |
|
87 | + public $personalFields = []; |
|
88 | 88 | |
89 | - /** |
|
90 | - * -------------------------------------------------------------------- |
|
91 | - * Password / Username Similarity |
|
92 | - * -------------------------------------------------------------------- |
|
93 | - * |
|
94 | - * Among other things, the NothingPersonalValidator checks the |
|
95 | - * amount of sameness between the password and username. |
|
96 | - * Passwords that are too much like the username are invalid. |
|
97 | - * |
|
98 | - * The value set for $maxSimilarity represents the maximum percentage |
|
99 | - * of similarity at which the password will be accepted. In other words, any |
|
100 | - * calculated similarity equal to, or greater than $maxSimilarity |
|
101 | - * is rejected. |
|
102 | - * |
|
103 | - * The accepted range is 0-100, with 0 (zero) meaning don't check similarity. |
|
104 | - * Using values at either extreme of the *working range* (1-100) is |
|
105 | - * not advised. The low end is too restrictive and the high end is too permissive. |
|
106 | - * The suggested value for $maxSimilarity is 50. |
|
107 | - * |
|
108 | - * You may be thinking that a value of 100 should have the effect of accepting |
|
109 | - * everything like a value of 0 does. That's logical and probably true, |
|
110 | - * but is unproven and untested. Besides, 0 skips the work involved |
|
111 | - * making the calculation unlike when using 100. |
|
112 | - * |
|
113 | - * The (admittedly limited) testing that's been done suggests a useful working range |
|
114 | - * of 50 to 60. You can set it lower than 50, but site users will probably start |
|
115 | - * to complain about the large number of proposed passwords getting rejected. |
|
116 | - * At around 60 or more it starts to see pairs like 'captain joe' and 'joe*captain' as |
|
117 | - * perfectly acceptable which clearly they are not. |
|
118 | - * |
|
119 | - * |
|
120 | - * To disable similarity checking set the value to 0. |
|
121 | - * public $maxSimilarity = 0; |
|
122 | - * |
|
123 | - * @var int |
|
124 | - */ |
|
125 | - public $maxSimilarity = 50; |
|
89 | + /** |
|
90 | + * -------------------------------------------------------------------- |
|
91 | + * Password / Username Similarity |
|
92 | + * -------------------------------------------------------------------- |
|
93 | + * |
|
94 | + * Among other things, the NothingPersonalValidator checks the |
|
95 | + * amount of sameness between the password and username. |
|
96 | + * Passwords that are too much like the username are invalid. |
|
97 | + * |
|
98 | + * The value set for $maxSimilarity represents the maximum percentage |
|
99 | + * of similarity at which the password will be accepted. In other words, any |
|
100 | + * calculated similarity equal to, or greater than $maxSimilarity |
|
101 | + * is rejected. |
|
102 | + * |
|
103 | + * The accepted range is 0-100, with 0 (zero) meaning don't check similarity. |
|
104 | + * Using values at either extreme of the *working range* (1-100) is |
|
105 | + * not advised. The low end is too restrictive and the high end is too permissive. |
|
106 | + * The suggested value for $maxSimilarity is 50. |
|
107 | + * |
|
108 | + * You may be thinking that a value of 100 should have the effect of accepting |
|
109 | + * everything like a value of 0 does. That's logical and probably true, |
|
110 | + * but is unproven and untested. Besides, 0 skips the work involved |
|
111 | + * making the calculation unlike when using 100. |
|
112 | + * |
|
113 | + * The (admittedly limited) testing that's been done suggests a useful working range |
|
114 | + * of 50 to 60. You can set it lower than 50, but site users will probably start |
|
115 | + * to complain about the large number of proposed passwords getting rejected. |
|
116 | + * At around 60 or more it starts to see pairs like 'captain joe' and 'joe*captain' as |
|
117 | + * perfectly acceptable which clearly they are not. |
|
118 | + * |
|
119 | + * |
|
120 | + * To disable similarity checking set the value to 0. |
|
121 | + * public $maxSimilarity = 0; |
|
122 | + * |
|
123 | + * @var int |
|
124 | + */ |
|
125 | + public $maxSimilarity = 50; |
|
126 | 126 | |
127 | - /** |
|
128 | - * -------------------------------------------------------------------- |
|
129 | - * Allow User Registration |
|
130 | - * -------------------------------------------------------------------- |
|
131 | - * |
|
132 | - * When enabled (default) any unregistered user may apply for a new |
|
133 | - * account. If you disable registration you may need to ensure your |
|
134 | - * controllers and views know not to offer registration. |
|
135 | - * |
|
136 | - * @var bool |
|
137 | - */ |
|
138 | - public $allowRegistration = true; |
|
127 | + /** |
|
128 | + * -------------------------------------------------------------------- |
|
129 | + * Allow User Registration |
|
130 | + * -------------------------------------------------------------------- |
|
131 | + * |
|
132 | + * When enabled (default) any unregistered user may apply for a new |
|
133 | + * account. If you disable registration you may need to ensure your |
|
134 | + * controllers and views know not to offer registration. |
|
135 | + * |
|
136 | + * @var bool |
|
137 | + */ |
|
138 | + public $allowRegistration = true; |
|
139 | 139 | |
140 | - /** |
|
141 | - * -------------------------------------------------------------------- |
|
142 | - * Require Confirmation Registration via Email |
|
143 | - * -------------------------------------------------------------------- |
|
144 | - * |
|
145 | - * When enabled, every registered user will receive an email message |
|
146 | - * with an activation link to confirm the account. |
|
147 | - * |
|
148 | - * @var string|null Name of the ActivatorInterface class |
|
149 | - */ |
|
150 | - public $requireActivation = 'Myth\Auth\Authentication\Activators\EmailActivator'; |
|
140 | + /** |
|
141 | + * -------------------------------------------------------------------- |
|
142 | + * Require Confirmation Registration via Email |
|
143 | + * -------------------------------------------------------------------- |
|
144 | + * |
|
145 | + * When enabled, every registered user will receive an email message |
|
146 | + * with an activation link to confirm the account. |
|
147 | + * |
|
148 | + * @var string|null Name of the ActivatorInterface class |
|
149 | + */ |
|
150 | + public $requireActivation = 'Myth\Auth\Authentication\Activators\EmailActivator'; |
|
151 | 151 | |
152 | - /** |
|
153 | - * -------------------------------------------------------------------- |
|
154 | - * Allow Password Reset via Email |
|
155 | - * -------------------------------------------------------------------- |
|
156 | - * |
|
157 | - * When enabled, users will have the option to reset their password |
|
158 | - * via the specified Resetter. Default setting is email. |
|
159 | - * |
|
160 | - * @var string|null Name of the ResetterInterface class |
|
161 | - */ |
|
162 | - public $activeResetter = 'Myth\Auth\Authentication\Resetters\EmailResetter'; |
|
152 | + /** |
|
153 | + * -------------------------------------------------------------------- |
|
154 | + * Allow Password Reset via Email |
|
155 | + * -------------------------------------------------------------------- |
|
156 | + * |
|
157 | + * When enabled, users will have the option to reset their password |
|
158 | + * via the specified Resetter. Default setting is email. |
|
159 | + * |
|
160 | + * @var string|null Name of the ResetterInterface class |
|
161 | + */ |
|
162 | + public $activeResetter = 'Myth\Auth\Authentication\Resetters\EmailResetter'; |
|
163 | 163 | |
164 | - /** |
|
165 | - * -------------------------------------------------------------------- |
|
166 | - * Allow Persistent Login Cookies (Remember me) |
|
167 | - * -------------------------------------------------------------------- |
|
168 | - * |
|
169 | - * While every attempt has been made to create a very strong protection |
|
170 | - * with the remember me system, there are some cases (like when you |
|
171 | - * need extreme protection, like dealing with users financials) that |
|
172 | - * you might not want the extra risk associated with this cookie-based |
|
173 | - * solution. |
|
174 | - * |
|
175 | - * @var bool |
|
176 | - */ |
|
177 | - public $allowRemembering = true; |
|
164 | + /** |
|
165 | + * -------------------------------------------------------------------- |
|
166 | + * Allow Persistent Login Cookies (Remember me) |
|
167 | + * -------------------------------------------------------------------- |
|
168 | + * |
|
169 | + * While every attempt has been made to create a very strong protection |
|
170 | + * with the remember me system, there are some cases (like when you |
|
171 | + * need extreme protection, like dealing with users financials) that |
|
172 | + * you might not want the extra risk associated with this cookie-based |
|
173 | + * solution. |
|
174 | + * |
|
175 | + * @var bool |
|
176 | + */ |
|
177 | + public $allowRemembering = true; |
|
178 | 178 | |
179 | - /** |
|
180 | - * -------------------------------------------------------------------- |
|
181 | - * Remember Length |
|
182 | - * -------------------------------------------------------------------- |
|
183 | - * |
|
184 | - * The amount of time, in seconds, that you want a login to last for. |
|
185 | - * Defaults to 30 days. |
|
186 | - * |
|
187 | - * @var int |
|
188 | - */ |
|
189 | - public $rememberLength = 30 * DAY; |
|
179 | + /** |
|
180 | + * -------------------------------------------------------------------- |
|
181 | + * Remember Length |
|
182 | + * -------------------------------------------------------------------- |
|
183 | + * |
|
184 | + * The amount of time, in seconds, that you want a login to last for. |
|
185 | + * Defaults to 30 days. |
|
186 | + * |
|
187 | + * @var int |
|
188 | + */ |
|
189 | + public $rememberLength = 30 * DAY; |
|
190 | 190 | |
191 | - /** |
|
192 | - * -------------------------------------------------------------------- |
|
193 | - * Error handling |
|
194 | - * -------------------------------------------------------------------- |
|
195 | - * |
|
196 | - * If true, will continue instead of throwing exceptions. |
|
197 | - * |
|
198 | - * @var bool |
|
199 | - */ |
|
200 | - public $silent = false; |
|
191 | + /** |
|
192 | + * -------------------------------------------------------------------- |
|
193 | + * Error handling |
|
194 | + * -------------------------------------------------------------------- |
|
195 | + * |
|
196 | + * If true, will continue instead of throwing exceptions. |
|
197 | + * |
|
198 | + * @var bool |
|
199 | + */ |
|
200 | + public $silent = false; |
|
201 | 201 | |
202 | - /** |
|
203 | - * -------------------------------------------------------------------- |
|
204 | - * Encryption Algorithm to Use |
|
205 | - * -------------------------------------------------------------------- |
|
206 | - * |
|
207 | - * Valid values are |
|
208 | - * - PASSWORD_DEFAULT (default) |
|
209 | - * - PASSWORD_BCRYPT |
|
210 | - * - PASSWORD_ARGON2I - As of PHP 7.2 only if compiled with support for it |
|
211 | - * - PASSWORD_ARGON2ID - As of PHP 7.3 only if compiled with support for it |
|
212 | - * |
|
213 | - * If you choose to use any ARGON algorithm, then you might want to |
|
214 | - * uncomment the "ARGON2i/D Algorithm" options to suit your needs |
|
215 | - * |
|
216 | - * @var string|int |
|
217 | - */ |
|
218 | - public $hashAlgorithm = PASSWORD_DEFAULT; |
|
202 | + /** |
|
203 | + * -------------------------------------------------------------------- |
|
204 | + * Encryption Algorithm to Use |
|
205 | + * -------------------------------------------------------------------- |
|
206 | + * |
|
207 | + * Valid values are |
|
208 | + * - PASSWORD_DEFAULT (default) |
|
209 | + * - PASSWORD_BCRYPT |
|
210 | + * - PASSWORD_ARGON2I - As of PHP 7.2 only if compiled with support for it |
|
211 | + * - PASSWORD_ARGON2ID - As of PHP 7.3 only if compiled with support for it |
|
212 | + * |
|
213 | + * If you choose to use any ARGON algorithm, then you might want to |
|
214 | + * uncomment the "ARGON2i/D Algorithm" options to suit your needs |
|
215 | + * |
|
216 | + * @var string|int |
|
217 | + */ |
|
218 | + public $hashAlgorithm = PASSWORD_DEFAULT; |
|
219 | 219 | |
220 | - /* |
|
220 | + /* |
|
221 | 221 | * -------------------------------------------------------------------- |
222 | 222 | * ARGON2i/D Algorithm options |
223 | 223 | * -------------------------------------------------------------------- |
@@ -232,106 +232,106 @@ discard block |
||
232 | 232 | * cost. This makes the hashing process takes longer. |
233 | 233 | */ |
234 | 234 | |
235 | - /** @var int */ |
|
236 | - public $hashMemoryCost = 2048; // PASSWORD_ARGON2_DEFAULT_MEMORY_COST; |
|
235 | + /** @var int */ |
|
236 | + public $hashMemoryCost = 2048; // PASSWORD_ARGON2_DEFAULT_MEMORY_COST; |
|
237 | 237 | |
238 | - /** @var int */ |
|
239 | - public $hashTimeCost = 4; // PASSWORD_ARGON2_DEFAULT_TIME_COST; |
|
238 | + /** @var int */ |
|
239 | + public $hashTimeCost = 4; // PASSWORD_ARGON2_DEFAULT_TIME_COST; |
|
240 | 240 | |
241 | - /** @var int */ |
|
242 | - public $hashThreads = 4; // PASSWORD_ARGON2_DEFAULT_THREADS; |
|
241 | + /** @var int */ |
|
242 | + public $hashThreads = 4; // PASSWORD_ARGON2_DEFAULT_THREADS; |
|
243 | 243 | |
244 | - /** |
|
245 | - * -------------------------------------------------------------------- |
|
246 | - * Password Hashing Cost |
|
247 | - * -------------------------------------------------------------------- |
|
248 | - * |
|
249 | - * The BCRYPT method of encryption allows you to define the "cost" |
|
250 | - * or number of iterations made, whenever a password hash is created. |
|
251 | - * This defaults to a value of 10 which is an acceptable number. |
|
252 | - * However, depending on the security needs of your application |
|
253 | - * and the power of your hardware, you might want to increase the |
|
254 | - * cost. This makes the hashing process takes longer. |
|
255 | - * |
|
256 | - * Valid range is between 4 - 31. |
|
257 | - * |
|
258 | - * @var int |
|
259 | - */ |
|
260 | - public $hashCost = 10; |
|
244 | + /** |
|
245 | + * -------------------------------------------------------------------- |
|
246 | + * Password Hashing Cost |
|
247 | + * -------------------------------------------------------------------- |
|
248 | + * |
|
249 | + * The BCRYPT method of encryption allows you to define the "cost" |
|
250 | + * or number of iterations made, whenever a password hash is created. |
|
251 | + * This defaults to a value of 10 which is an acceptable number. |
|
252 | + * However, depending on the security needs of your application |
|
253 | + * and the power of your hardware, you might want to increase the |
|
254 | + * cost. This makes the hashing process takes longer. |
|
255 | + * |
|
256 | + * Valid range is between 4 - 31. |
|
257 | + * |
|
258 | + * @var int |
|
259 | + */ |
|
260 | + public $hashCost = 10; |
|
261 | 261 | |
262 | - /** |
|
263 | - * -------------------------------------------------------------------- |
|
264 | - * Minimum Password Length |
|
265 | - * -------------------------------------------------------------------- |
|
266 | - * |
|
267 | - * The minimum length that a password must be to be accepted. |
|
268 | - * Recommended minimum value by NIST = 8 characters. |
|
269 | - * |
|
270 | - * @var int |
|
271 | - */ |
|
272 | - public $minimumPasswordLength = 8; |
|
262 | + /** |
|
263 | + * -------------------------------------------------------------------- |
|
264 | + * Minimum Password Length |
|
265 | + * -------------------------------------------------------------------- |
|
266 | + * |
|
267 | + * The minimum length that a password must be to be accepted. |
|
268 | + * Recommended minimum value by NIST = 8 characters. |
|
269 | + * |
|
270 | + * @var int |
|
271 | + */ |
|
272 | + public $minimumPasswordLength = 8; |
|
273 | 273 | |
274 | - /** |
|
275 | - * -------------------------------------------------------------------- |
|
276 | - * Password Check Helpers |
|
277 | - * -------------------------------------------------------------------- |
|
278 | - * |
|
279 | - * The PasswordValidater class runs the password through all of these |
|
280 | - * classes, each getting the opportunity to pass/fail the password. |
|
281 | - * |
|
282 | - * You can add custom classes as long as they adhere to the |
|
283 | - * Password\ValidatorInterface. |
|
284 | - * |
|
285 | - * @var string[] |
|
286 | - */ |
|
287 | - public $passwordValidators = [ |
|
288 | - 'Myth\Auth\Authentication\Passwords\CompositionValidator', |
|
289 | - 'Myth\Auth\Authentication\Passwords\NothingPersonalValidator', |
|
290 | - 'Myth\Auth\Authentication\Passwords\DictionaryValidator', |
|
291 | - // 'Myth\Auth\Authentication\Passwords\PwnedValidator', |
|
292 | - ]; |
|
274 | + /** |
|
275 | + * -------------------------------------------------------------------- |
|
276 | + * Password Check Helpers |
|
277 | + * -------------------------------------------------------------------- |
|
278 | + * |
|
279 | + * The PasswordValidater class runs the password through all of these |
|
280 | + * classes, each getting the opportunity to pass/fail the password. |
|
281 | + * |
|
282 | + * You can add custom classes as long as they adhere to the |
|
283 | + * Password\ValidatorInterface. |
|
284 | + * |
|
285 | + * @var string[] |
|
286 | + */ |
|
287 | + public $passwordValidators = [ |
|
288 | + 'Myth\Auth\Authentication\Passwords\CompositionValidator', |
|
289 | + 'Myth\Auth\Authentication\Passwords\NothingPersonalValidator', |
|
290 | + 'Myth\Auth\Authentication\Passwords\DictionaryValidator', |
|
291 | + // 'Myth\Auth\Authentication\Passwords\PwnedValidator', |
|
292 | + ]; |
|
293 | 293 | |
294 | - /** |
|
295 | - * -------------------------------------------------------------------- |
|
296 | - * Activator classes |
|
297 | - * -------------------------------------------------------------------- |
|
298 | - * |
|
299 | - * Available activators with config settings |
|
300 | - * |
|
301 | - * @var array |
|
302 | - */ |
|
303 | - public $userActivators = [ |
|
304 | - 'Myth\Auth\Authentication\Activators\EmailActivator' => [ |
|
305 | - 'fromEmail' => null, |
|
306 | - 'fromName' => null, |
|
307 | - ], |
|
308 | - ]; |
|
294 | + /** |
|
295 | + * -------------------------------------------------------------------- |
|
296 | + * Activator classes |
|
297 | + * -------------------------------------------------------------------- |
|
298 | + * |
|
299 | + * Available activators with config settings |
|
300 | + * |
|
301 | + * @var array |
|
302 | + */ |
|
303 | + public $userActivators = [ |
|
304 | + 'Myth\Auth\Authentication\Activators\EmailActivator' => [ |
|
305 | + 'fromEmail' => null, |
|
306 | + 'fromName' => null, |
|
307 | + ], |
|
308 | + ]; |
|
309 | 309 | |
310 | - /** |
|
311 | - * -------------------------------------------------------------------- |
|
312 | - * Resetter Classes |
|
313 | - * -------------------------------------------------------------------- |
|
314 | - * |
|
315 | - * Available resetters with config settings |
|
316 | - * |
|
317 | - * @var array |
|
318 | - */ |
|
319 | - public $userResetters = [ |
|
320 | - 'Myth\Auth\Authentication\Resetters\EmailResetter' => [ |
|
321 | - 'fromEmail' => null, |
|
322 | - 'fromName' => null, |
|
323 | - ], |
|
324 | - ]; |
|
310 | + /** |
|
311 | + * -------------------------------------------------------------------- |
|
312 | + * Resetter Classes |
|
313 | + * -------------------------------------------------------------------- |
|
314 | + * |
|
315 | + * Available resetters with config settings |
|
316 | + * |
|
317 | + * @var array |
|
318 | + */ |
|
319 | + public $userResetters = [ |
|
320 | + 'Myth\Auth\Authentication\Resetters\EmailResetter' => [ |
|
321 | + 'fromEmail' => null, |
|
322 | + 'fromName' => null, |
|
323 | + ], |
|
324 | + ]; |
|
325 | 325 | |
326 | - /** |
|
327 | - * -------------------------------------------------------------------- |
|
328 | - * Reset Time |
|
329 | - * -------------------------------------------------------------------- |
|
330 | - * |
|
331 | - * The amount of time that a password reset-token is valid for, |
|
332 | - * in seconds. |
|
333 | - * |
|
334 | - * @var int |
|
335 | - */ |
|
336 | - public $resetTime = 3600; |
|
326 | + /** |
|
327 | + * -------------------------------------------------------------------- |
|
328 | + * Reset Time |
|
329 | + * -------------------------------------------------------------------- |
|
330 | + * |
|
331 | + * The amount of time that a password reset-token is valid for, |
|
332 | + * in seconds. |
|
333 | + * |
|
334 | + * @var int |
|
335 | + */ |
|
336 | + public $resetTime = 3600; |
|
337 | 337 | } |
@@ -7,56 +7,56 @@ |
||
7 | 7 | |
8 | 8 | class LoginFilter implements FilterInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * Verifies that a user is logged in, or redirects to login. |
|
12 | - * |
|
13 | - * @param RequestInterface $request |
|
14 | - * @param array|null $params |
|
15 | - * |
|
16 | - * @return mixed |
|
17 | - */ |
|
18 | - public function before(RequestInterface $request, $params = null) |
|
19 | - { |
|
20 | - if (! function_exists('logged_in')) |
|
21 | - { |
|
22 | - helper('auth'); |
|
23 | - } |
|
10 | + /** |
|
11 | + * Verifies that a user is logged in, or redirects to login. |
|
12 | + * |
|
13 | + * @param RequestInterface $request |
|
14 | + * @param array|null $params |
|
15 | + * |
|
16 | + * @return mixed |
|
17 | + */ |
|
18 | + public function before(RequestInterface $request, $params = null) |
|
19 | + { |
|
20 | + if (! function_exists('logged_in')) |
|
21 | + { |
|
22 | + helper('auth'); |
|
23 | + } |
|
24 | 24 | |
25 | - $current = (string)current_url(true) |
|
26 | - ->setHost('') |
|
27 | - ->setScheme('') |
|
28 | - ->stripQuery('token'); |
|
25 | + $current = (string)current_url(true) |
|
26 | + ->setHost('') |
|
27 | + ->setScheme('') |
|
28 | + ->stripQuery('token'); |
|
29 | 29 | |
30 | - $config = config(App::class); |
|
31 | - if($config->forceGlobalSecureRequests) |
|
32 | - { |
|
33 | - # Remove "https:/" |
|
34 | - $current = substr($current, 7); |
|
35 | - } |
|
30 | + $config = config(App::class); |
|
31 | + if($config->forceGlobalSecureRequests) |
|
32 | + { |
|
33 | + # Remove "https:/" |
|
34 | + $current = substr($current, 7); |
|
35 | + } |
|
36 | 36 | |
37 | - // Make sure this isn't already a login route |
|
38 | - if (in_array((string)$current, [route_to('login'), route_to('forgot'), route_to('reset-password'), route_to('register'), route_to('activate-account')])) |
|
39 | - { |
|
40 | - return; |
|
41 | - } |
|
37 | + // Make sure this isn't already a login route |
|
38 | + if (in_array((string)$current, [route_to('login'), route_to('forgot'), route_to('reset-password'), route_to('register'), route_to('activate-account')])) |
|
39 | + { |
|
40 | + return; |
|
41 | + } |
|
42 | 42 | |
43 | - // if no user is logged in then send to the login form |
|
44 | - $authenticate = service('authentication'); |
|
45 | - if (! $authenticate->check()) |
|
46 | - { |
|
47 | - session()->set('redirect_url', current_url()); |
|
48 | - return redirect('login'); |
|
49 | - } |
|
50 | - } |
|
43 | + // if no user is logged in then send to the login form |
|
44 | + $authenticate = service('authentication'); |
|
45 | + if (! $authenticate->check()) |
|
46 | + { |
|
47 | + session()->set('redirect_url', current_url()); |
|
48 | + return redirect('login'); |
|
49 | + } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param RequestInterface $request |
|
54 | - * @param ResponseInterface $response |
|
55 | - * @param array|null $arguments |
|
56 | - * |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
|
60 | - { |
|
61 | - } |
|
52 | + /** |
|
53 | + * @param RequestInterface $request |
|
54 | + * @param ResponseInterface $response |
|
55 | + * @param array|null $arguments |
|
56 | + * |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
|
60 | + { |
|
61 | + } |
|
62 | 62 | } |
@@ -7,82 +7,82 @@ |
||
7 | 7 | |
8 | 8 | class RoleFilter implements FilterInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * Do whatever processing this filter needs to do. |
|
12 | - * By default it should not return anything during |
|
13 | - * normal execution. However, when an abnormal state |
|
14 | - * is found, it should return an instance of |
|
15 | - * CodeIgniter\HTTP\Response. If it does, script |
|
16 | - * execution will end and that Response will be |
|
17 | - * sent back to the client, allowing for error pages, |
|
18 | - * redirects, etc. |
|
19 | - * |
|
20 | - * @param RequestInterface $request |
|
21 | - * @param array|null $params |
|
22 | - * |
|
23 | - * @return mixed |
|
24 | - */ |
|
25 | - public function before(RequestInterface $request, $params = null) |
|
26 | - { |
|
27 | - if (! function_exists('logged_in')) |
|
28 | - { |
|
29 | - helper('auth'); |
|
30 | - } |
|
10 | + /** |
|
11 | + * Do whatever processing this filter needs to do. |
|
12 | + * By default it should not return anything during |
|
13 | + * normal execution. However, when an abnormal state |
|
14 | + * is found, it should return an instance of |
|
15 | + * CodeIgniter\HTTP\Response. If it does, script |
|
16 | + * execution will end and that Response will be |
|
17 | + * sent back to the client, allowing for error pages, |
|
18 | + * redirects, etc. |
|
19 | + * |
|
20 | + * @param RequestInterface $request |
|
21 | + * @param array|null $params |
|
22 | + * |
|
23 | + * @return mixed |
|
24 | + */ |
|
25 | + public function before(RequestInterface $request, $params = null) |
|
26 | + { |
|
27 | + if (! function_exists('logged_in')) |
|
28 | + { |
|
29 | + helper('auth'); |
|
30 | + } |
|
31 | 31 | |
32 | - if (empty($params)) |
|
33 | - { |
|
34 | - return; |
|
35 | - } |
|
32 | + if (empty($params)) |
|
33 | + { |
|
34 | + return; |
|
35 | + } |
|
36 | 36 | |
37 | - $authenticate = service('authentication'); |
|
37 | + $authenticate = service('authentication'); |
|
38 | 38 | |
39 | - // if no user is logged in then send to the login form |
|
40 | - if (! $authenticate->check()) |
|
41 | - { |
|
42 | - session()->set('redirect_url', current_url()); |
|
43 | - return redirect('login'); |
|
44 | - } |
|
39 | + // if no user is logged in then send to the login form |
|
40 | + if (! $authenticate->check()) |
|
41 | + { |
|
42 | + session()->set('redirect_url', current_url()); |
|
43 | + return redirect('login'); |
|
44 | + } |
|
45 | 45 | |
46 | - $authorize = service('authorization'); |
|
46 | + $authorize = service('authorization'); |
|
47 | 47 | |
48 | - // Check each requested permission |
|
49 | - foreach ($params as $group) |
|
50 | - { |
|
51 | - if($authorize->inGroup($group, $authenticate->id())) |
|
52 | - { |
|
53 | - return; |
|
54 | - } |
|
55 | - } |
|
48 | + // Check each requested permission |
|
49 | + foreach ($params as $group) |
|
50 | + { |
|
51 | + if($authorize->inGroup($group, $authenticate->id())) |
|
52 | + { |
|
53 | + return; |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | - if ($authenticate->silent()) |
|
58 | - { |
|
59 | - $redirectURL = session('redirect_url') ?? '/'; |
|
60 | - unset($_SESSION['redirect_url']); |
|
61 | - return redirect()->to($redirectURL)->with('error', lang('Auth.notEnoughPrivilege')); |
|
62 | - } |
|
63 | - else { |
|
64 | - throw new PermissionException(lang('Auth.notEnoughPrivilege')); |
|
65 | - } |
|
66 | - } |
|
57 | + if ($authenticate->silent()) |
|
58 | + { |
|
59 | + $redirectURL = session('redirect_url') ?? '/'; |
|
60 | + unset($_SESSION['redirect_url']); |
|
61 | + return redirect()->to($redirectURL)->with('error', lang('Auth.notEnoughPrivilege')); |
|
62 | + } |
|
63 | + else { |
|
64 | + throw new PermissionException(lang('Auth.notEnoughPrivilege')); |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - //-------------------------------------------------------------------- |
|
68 | + //-------------------------------------------------------------------- |
|
69 | 69 | |
70 | - /** |
|
71 | - * Allows After filters to inspect and modify the response |
|
72 | - * object as needed. This method does not allow any way |
|
73 | - * to stop execution of other after filters, short of |
|
74 | - * throwing an Exception or Error. |
|
75 | - * |
|
76 | - * @param RequestInterface $request |
|
77 | - * @param ResponseInterface $response |
|
78 | - * @param array|null $arguments |
|
79 | - * |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
|
83 | - { |
|
70 | + /** |
|
71 | + * Allows After filters to inspect and modify the response |
|
72 | + * object as needed. This method does not allow any way |
|
73 | + * to stop execution of other after filters, short of |
|
74 | + * throwing an Exception or Error. |
|
75 | + * |
|
76 | + * @param RequestInterface $request |
|
77 | + * @param ResponseInterface $response |
|
78 | + * @param array|null $arguments |
|
79 | + * |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
|
83 | + { |
|
84 | 84 | |
85 | - } |
|
85 | + } |
|
86 | 86 | |
87 | - //-------------------------------------------------------------------- |
|
87 | + //-------------------------------------------------------------------- |
|
88 | 88 | } |
@@ -7,83 +7,83 @@ |
||
7 | 7 | |
8 | 8 | class PermissionFilter implements FilterInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * Do whatever processing this filter needs to do. |
|
12 | - * By default it should not return anything during |
|
13 | - * normal execution. However, when an abnormal state |
|
14 | - * is found, it should return an instance of |
|
15 | - * CodeIgniter\HTTP\Response. If it does, script |
|
16 | - * execution will end and that Response will be |
|
17 | - * sent back to the client, allowing for error pages, |
|
18 | - * redirects, etc. |
|
19 | - * |
|
20 | - * @param RequestInterface $request |
|
21 | - * @param array|null $params |
|
22 | - * |
|
23 | - * @return mixed |
|
24 | - */ |
|
25 | - public function before(RequestInterface $request, $params = null) |
|
26 | - { |
|
27 | - if (! function_exists('logged_in')) |
|
28 | - { |
|
29 | - helper('auth'); |
|
30 | - } |
|
10 | + /** |
|
11 | + * Do whatever processing this filter needs to do. |
|
12 | + * By default it should not return anything during |
|
13 | + * normal execution. However, when an abnormal state |
|
14 | + * is found, it should return an instance of |
|
15 | + * CodeIgniter\HTTP\Response. If it does, script |
|
16 | + * execution will end and that Response will be |
|
17 | + * sent back to the client, allowing for error pages, |
|
18 | + * redirects, etc. |
|
19 | + * |
|
20 | + * @param RequestInterface $request |
|
21 | + * @param array|null $params |
|
22 | + * |
|
23 | + * @return mixed |
|
24 | + */ |
|
25 | + public function before(RequestInterface $request, $params = null) |
|
26 | + { |
|
27 | + if (! function_exists('logged_in')) |
|
28 | + { |
|
29 | + helper('auth'); |
|
30 | + } |
|
31 | 31 | |
32 | - if (empty($params)) |
|
33 | - { |
|
34 | - return; |
|
35 | - } |
|
32 | + if (empty($params)) |
|
33 | + { |
|
34 | + return; |
|
35 | + } |
|
36 | 36 | |
37 | - $authenticate = service('authentication'); |
|
37 | + $authenticate = service('authentication'); |
|
38 | 38 | |
39 | - // if no user is logged in then send to the login form |
|
40 | - if (! $authenticate->check()) |
|
41 | - { |
|
42 | - session()->set('redirect_url', current_url()); |
|
43 | - return redirect('login'); |
|
44 | - } |
|
39 | + // if no user is logged in then send to the login form |
|
40 | + if (! $authenticate->check()) |
|
41 | + { |
|
42 | + session()->set('redirect_url', current_url()); |
|
43 | + return redirect('login'); |
|
44 | + } |
|
45 | 45 | |
46 | - $authorize = service('authorization'); |
|
47 | - $result = true; |
|
46 | + $authorize = service('authorization'); |
|
47 | + $result = true; |
|
48 | 48 | |
49 | - // Check each requested permission |
|
50 | - foreach ($params as $permission) |
|
51 | - { |
|
52 | - $result = $result && $authorize->hasPermission($permission, $authenticate->id()); |
|
53 | - } |
|
49 | + // Check each requested permission |
|
50 | + foreach ($params as $permission) |
|
51 | + { |
|
52 | + $result = $result && $authorize->hasPermission($permission, $authenticate->id()); |
|
53 | + } |
|
54 | 54 | |
55 | - if (! $result) |
|
56 | - { |
|
57 | - if ($authenticate->silent()) |
|
58 | - { |
|
59 | - $redirectURL = session('redirect_url') ?? '/'; |
|
60 | - unset($_SESSION['redirect_url']); |
|
61 | - return redirect()->to($redirectURL)->with('error', lang('Auth.notEnoughPrivilege')); |
|
62 | - } |
|
63 | - else { |
|
64 | - throw new PermissionException(lang('Auth.notEnoughPrivilege')); |
|
65 | - } |
|
66 | - } |
|
67 | - } |
|
55 | + if (! $result) |
|
56 | + { |
|
57 | + if ($authenticate->silent()) |
|
58 | + { |
|
59 | + $redirectURL = session('redirect_url') ?? '/'; |
|
60 | + unset($_SESSION['redirect_url']); |
|
61 | + return redirect()->to($redirectURL)->with('error', lang('Auth.notEnoughPrivilege')); |
|
62 | + } |
|
63 | + else { |
|
64 | + throw new PermissionException(lang('Auth.notEnoughPrivilege')); |
|
65 | + } |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | - //-------------------------------------------------------------------- |
|
69 | + //-------------------------------------------------------------------- |
|
70 | 70 | |
71 | - /** |
|
72 | - * Allows After filters to inspect and modify the response |
|
73 | - * object as needed. This method does not allow any way |
|
74 | - * to stop execution of other after filters, short of |
|
75 | - * throwing an Exception or Error. |
|
76 | - * |
|
77 | - * @param RequestInterface $request |
|
78 | - * @param ResponseInterface $response |
|
79 | - * @param array|null $arguments |
|
80 | - * |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
|
84 | - { |
|
71 | + /** |
|
72 | + * Allows After filters to inspect and modify the response |
|
73 | + * object as needed. This method does not allow any way |
|
74 | + * to stop execution of other after filters, short of |
|
75 | + * throwing an Exception or Error. |
|
76 | + * |
|
77 | + * @param RequestInterface $request |
|
78 | + * @param ResponseInterface $response |
|
79 | + * @param array|null $arguments |
|
80 | + * |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
|
84 | + { |
|
85 | 85 | |
86 | - } |
|
86 | + } |
|
87 | 87 | |
88 | - //-------------------------------------------------------------------- |
|
88 | + //-------------------------------------------------------------------- |
|
89 | 89 | } |
@@ -5,66 +5,66 @@ |
||
5 | 5 | |
6 | 6 | abstract class BaseResetter |
7 | 7 | { |
8 | - /** |
|
9 | - * @var AuthConfig |
|
10 | - */ |
|
11 | - protected $config; |
|
8 | + /** |
|
9 | + * @var AuthConfig |
|
10 | + */ |
|
11 | + protected $config; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $error = ''; |
|
13 | + /** |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $error = ''; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Sends a reset message to user |
|
20 | - * |
|
21 | - * @param User $user |
|
22 | - * |
|
23 | - * @return bool |
|
24 | - */ |
|
25 | - abstract public function send(User $user = null): bool; |
|
18 | + /** |
|
19 | + * Sends a reset message to user |
|
20 | + * |
|
21 | + * @param User $user |
|
22 | + * |
|
23 | + * @return bool |
|
24 | + */ |
|
25 | + abstract public function send(User $user = null): bool; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Sets the initial config file. |
|
29 | - * |
|
30 | - * @param AuthConfig|null $config |
|
31 | - */ |
|
32 | - public function __construct(AuthConfig $config = null) |
|
33 | - { |
|
34 | - $this->config = $config ?? config('Auth'); |
|
35 | - } |
|
27 | + /** |
|
28 | + * Sets the initial config file. |
|
29 | + * |
|
30 | + * @param AuthConfig|null $config |
|
31 | + */ |
|
32 | + public function __construct(AuthConfig $config = null) |
|
33 | + { |
|
34 | + $this->config = $config ?? config('Auth'); |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Allows for changing the config file on the Resetter. |
|
39 | - * |
|
40 | - * @param AuthConfig $config |
|
41 | - * |
|
42 | - * @return $this |
|
43 | - */ |
|
44 | - public function setConfig(AuthConfig $config) |
|
45 | - { |
|
46 | - $this->config = $config; |
|
37 | + /** |
|
38 | + * Allows for changing the config file on the Resetter. |
|
39 | + * |
|
40 | + * @param AuthConfig $config |
|
41 | + * |
|
42 | + * @return $this |
|
43 | + */ |
|
44 | + public function setConfig(AuthConfig $config) |
|
45 | + { |
|
46 | + $this->config = $config; |
|
47 | 47 | |
48 | - return $this; |
|
49 | - } |
|
48 | + return $this; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Gets a config settings for current Resetter. |
|
53 | - * |
|
54 | - * @return object |
|
55 | - */ |
|
56 | - public function getResetterSettings() |
|
57 | - { |
|
58 | - return (object) $this->config->userResetters[static::class]; |
|
59 | - } |
|
51 | + /** |
|
52 | + * Gets a config settings for current Resetter. |
|
53 | + * |
|
54 | + * @return object |
|
55 | + */ |
|
56 | + public function getResetterSettings() |
|
57 | + { |
|
58 | + return (object) $this->config->userResetters[static::class]; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Returns the current error. |
|
63 | - * |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - public function error(): string |
|
67 | - { |
|
68 | - return $this->error; |
|
69 | - } |
|
61 | + /** |
|
62 | + * Returns the current error. |
|
63 | + * |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + public function error(): string |
|
67 | + { |
|
68 | + return $this->error; |
|
69 | + } |
|
70 | 70 | } |
@@ -27,11 +27,11 @@ |
||
27 | 27 | $settings = $this->getResetterSettings(); |
28 | 28 | |
29 | 29 | $sent = $email->setFrom($settings->fromEmail ?? $config->fromEmail, $settings->fromName ?? $config->fromName) |
30 | - ->setTo($user->email) |
|
31 | - ->setSubject(lang('Auth.forgotSubject')) |
|
32 | - ->setMessage(view($this->config->views['emailForgot'], ['hash' => $user->reset_hash])) |
|
33 | - ->setMailType('html') |
|
34 | - ->send(); |
|
30 | + ->setTo($user->email) |
|
31 | + ->setSubject(lang('Auth.forgotSubject')) |
|
32 | + ->setMessage(view($this->config->views['emailForgot'], ['hash' => $user->reset_hash])) |
|
33 | + ->setMailType('html') |
|
34 | + ->send(); |
|
35 | 35 | |
36 | 36 | if (! $sent) |
37 | 37 | { |