-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
FlatAuthorization.php
714 lines (590 loc) · 16 KB
/
FlatAuthorization.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
<?php namespace Myth\Auth\Authorization;
use CodeIgniter\Model;
use CodeIgniter\Events\Events;
use Myth\Authorization\GroupModel;
use Myth\Authorization\PermissionModel;
class FlatAuthorization implements AuthorizeInterface
{
protected $error = null;
/**
* @var GroupModel
*/
protected $groupModel;
/**
* @var PermissionModel
*/
protected $permissionModel;
/**
* @var null UserModel
*/
protected $userModel = null;
public function __construct(Model $groupModel, Model $permModel)
{
$this->groupModel = $groupModel;
$this->permissionModel = $permModel;
}
public function error()
{
return $this->error;
}
/**
* Allows the consuming application to pass in a reference to the
* model that should be used.
*
* @param $model
*
* @return mixed
*/
public function setUserModel($model)
{
$this->userModel = $model;
return $this;
}
//--------------------------------------------------------------------
// Actions
//--------------------------------------------------------------------
/**
* Checks to see if a user is in a group.
*
* Groups can be either a string, with the name of the group, an INT
* with the ID of the group, or an array of strings/ids that the
* user must belong to ONE of. (It's an OR check not an AND check)
*
* @param $groups
*
* @return bool
*/
public function inGroup($groups, int $userId)
{
if (! is_array($groups))
{
$groups = [$groups];
}
if (empty($userId))
{
return null;
}
$userGroups = $this->groupModel->getGroupsForUser((int)$userId);
if (! $userGroups)
{
return false;
}
foreach ($groups as $group)
{
if (is_numeric($group))
{
$ids = array_column($userGroups, 'id');
if (in_array($group, $ids))
{
return true;
}
}
else if (is_string($group))
{
$ids = array_column($userGroups, 'name');
if (in_array($group, $ids))
{
return true;
}
}
}
return false;
}
/**
* Checks a user's groups to see if they have the specified permission.
*
* @param int|string $permission
* @param int $userId
*
* @return mixed
*/
public function hasPermission($permission, int $userId)
{
if (empty($permission) || (! is_string($permission) && ! is_numeric($permission)))
{
return null;
}
if (empty($userId) || ! is_numeric($userId))
{
return null;
}
// Get the Permission ID
$permissionId = $this->getPermissionID($permission);
if (! is_numeric($permissionId))
{
return false;
}
// First check the permission model. If that exists, then we're golden.
if ($this->permissionModel->doesUserHavePermission($userId, (int)$permissionId))
{
return true;
}
// Still here? Then we have one last check to make - any user private permissions.
return $this->doesUserHavePermission($userId, (int)$permissionId);
}
/**
* Makes a member a part of a group.
*
* @param $userid
* @param $group // Either ID or name
*
* @return bool
*/
public function addUserToGroup(int $userid, $group)
{
if (empty($userid) || ! is_numeric($userid))
{
return null;
}
if (empty($group) || (! is_numeric($group) && ! is_string($group)))
{
return null;
}
if (! Events::trigger('beforeAddUserToGroup', $userid, $group))
{
return false;
}
$groupId = $this->getGroupID($group);
// Group ID
if (! is_numeric($groupId))
{
return null;
}
if (! $this->groupModel->addUserToGroup($userid, (int)$groupId))
{
$this->error = $this->groupModel->error();
return false;
}
Events::trigger('didAddUserToGroup', $userid, $group);
return true;
}
/**
* Removes a single user from a group.
*
* @param $userId
* @param $group
*
* @return mixed
*/
public function removeUserFromGroup(int $userId, $group)
{
if (empty($userId) || ! is_numeric($userId))
{
return null;
}
if (empty($group) || (! is_numeric($group) && ! is_string($group)))
{
return null;
}
if (! Events::trigger('beforeRemoveUserFromGroup', $userId, $group))
{
return false;
}
$groupId = $this->getGroupID($group);
// Group ID
if (! is_numeric($groupId))
{
return false;
}
if (! $this->groupModel->removeUserFromGroup($userId, $groupId))
{
$this->error = $this->groupModel->error();
return false;
}
Events::trigger('didRemoveUserFromGroup', $userId, $group);
return true;
}
/**
* Adds a single permission to a single group.
*
* @param int|string $permission
* @param int|string $group
*
* @return mixed
*/
public function addPermissionToGroup($permission, $group)
{
$permissionId = $this->getPermissionID($permission);
$groupId = $this->getGroupID($group);
// Permission ID
if (! is_numeric($permissionId))
{
return false;
}
// Group ID
if (! is_numeric($groupId))
{
return false;
}
// Remove it!
if (! $this->groupModel->addPermissionToGroup($permissionId, $groupId))
{
$this->error = $this->groupModel->error();
return false;
}
return true;
}
/**
* Removes a single permission from a group.
*
* @param int|string $permission
* @param int|string $group
*
* @return mixed
*/
public function removePermissionFromGroup($permission, $group)
{
$permissionId = $this->getPermissionID($permission);
$groupId = $this->getGroupID($group);
// Permission ID
if (! is_numeric($permissionId))
{
return false;
}
// Group ID
if (! is_numeric($groupId))
{
return false;
}
// Remove it!
if (! $this->groupModel->removePermissionFromGroup($permissionId, $groupId))
{
$this->error = $this->groupModel->error();
return false;
}
return true;
}
/**
* Assigns a single permission to a user, irregardless of permissions
* assigned by roles. This is saved to the user's meta information.
*
* @param int|string $permission
* @param int $userId
*
* @return int|bool
*/
public function addPermissionToUser($permission, int $userId)
{
$permissionId = $this->getPermissionID($permission);
if (! is_numeric($permissionId))
{
return null;
}
if (! Events::trigger('beforeAddPermissionToUser', $userId, $permission))
{
return false;
}
$user = $this->userModel->find($userId);
if (! $user)
{
$this->error = lang('Auth.userNotFound', [$userId]);
return false;
}
$permissions = $user->getPermissions();
if (! in_array($permissionId, $permissions))
{
$permissions[] = $permissionId;
$user->setPermissions($permissions);
$this->userModel->save($user);
}
return true;
}
/**
* Removes a single permission from a user. Only applies to permissions
* that have been assigned with addPermissionToUser, not to permissions
* inherited based on groups they belong to.
*
* @param int/string $permission
* @param int $userId
*/
public function removePermissionFromUser($permission, int $userId)
{
$permissionId = $this->getPermissionID($permission);
if (! is_numeric($permissionId))
{
return false;
}
if (empty($userId) || ! is_numeric($userId))
{
return null;
}
$userId = (int)$userId;
if (! Events::trigger('beforeRemovePermissionFromUser', $userId, $permission))
{
return false;
}
$user = $this->userModel->find($userId);
if (! $user)
{
$this->error = lang('Auth.userNotFound', [$userId]);
return false;
}
// Grab the existing permissions for this user, and remove
// the permission id from the list.
$permissions = $user->getPermissions();
if (in_array($permissionId, $permissions))
{
unset($permissions[array_search($permissionId, $permissions)]);
$user->setPermissions($permissions);
$this->userModel->save($user);
}
// Save the updated permissions
return true;
}
/**
* Checks to see if a user has private permission assigned to it.
*
* @param $userId
* @param $permission
*
* @return bool|null
*/
public function doesUserHavePermission($userId, $permission)
{
$permissionId = $this->getPermissionID($permission);
if (! is_numeric($permissionId))
{
return false;
}
if (empty($userId) || ! is_numeric($userId))
{
return null;
}
$user = $this->userModel->find($userId);
if (! $user)
{
$this->error = lang('Auth.userNotFound', [$userId]);
return false;
}
$permissions = $user->getPermissions();
return in_array($permissionId, $permissions);
}
//--------------------------------------------------------------------
// Groups
//--------------------------------------------------------------------
/**
* Grabs the details about a single group.
*
* @param $group
*
* @return object|null
*/
public function group($group)
{
if (is_numeric($group))
{
return $this->groupModel->find((int)$group);
}
return $this->groupModel->findWhere('name', $group);
}
/**
* Grabs an array of all groups.
*
* @return array of objects
*/
public function groups()
{
return $this->groupModel
->orderBy('name', 'asc')
->findAll();
}
/**
* @param $name
* @param string $description
*
* @return mixed
*/
public function createGroup(string $name, string $description = '')
{
$data = [
'name' => $name,
'description' => $description,
];
$id = $this->groupModel->insert($data);
if (is_numeric($id))
{
return (int)$id;
}
$this->error = $this->groupModel->error();
return false;
}
/**
* Deletes a single group.
*
* @param int $groupId
*
* @return bool
*/
public function deleteGroup(int $groupId)
{
if (! $this->groupModel->delete($groupId))
{
$this->error = $this->groupModel->error();
return false;
}
return true;
}
/**
* Updates a single group's information.
*
* @param $id
* @param $name
* @param string $description
*
* @return mixed
*/
public function updateGroup(int $id, string $name, string $description = '')
{
$data = [
'name' => $name,
];
if (! empty($description))
{
$data['description'] = $description;
}
if (! $this->groupModel->update($id, $data))
{
$this->error = $this->groupModel->error();
return false;
}
return true;
}
/**
* Given a group, will return the group ID. The group can be either
* the ID or the name of the group.
*
* @param int|string $group
*
* @return int|false
*/
protected function getGroupID($group)
{
if (is_int($group))
{
return (int)$group;
}
$g = $this->groupModel->findWhere('name', $group);
if (! $g)
{
$this->error = lang('Auth.groupNotFound', [$group]);
return false;
}
return (int)$g->id;
}
//--------------------------------------------------------------------
// Permissions
//--------------------------------------------------------------------
/**
* Returns the details about a single permission.
*
* @param int|string $permission
*
* @return object|null
*/
public function permission($permission)
{
if (is_numeric($permission))
{
return $this->permissionModel->find((int)$permission);
}
return $this->permissionModel->findWhere('LOWER(name)', strtolower($permission));
}
/**
* Returns an array of all permissions in the system.
*
* @return mixed
*/
public function permissions()
{
return $this->permissionModel->findAll();
}
/**
* Creates a single permission.
*
* @param $name
* @param string $description
*
* @return mixed
*/
public function createPermission(string $name, string $description = '')
{
$data = [
'name' => $name,
'description' => $description,
];
$id = $this->permissionModel->insert($data);
if (is_numeric($id))
{
return (int)$id;
}
$this->error = $this->permissionModel->error();
return false;
}
/**
* Deletes a single permission and removes that permission from all groups.
*
* @param int $permissionIdId
*
* @return mixed
*/
public function deletePermission(int $permissionIdId)
{
if (! $this->permissionModel->delete($permissionIdId))
{
$this->error = $this->permissionModel->error();
return false;
}
// Remove the permission from all groups
$this->groupModel->removePermissionFromAllGroups($permissionIdId);
return true;
}
/**
* Updates the details for a single permission.
*
* @param int $id
* @param string $name
* @param string $description
*
* @return bool
*/
public function updatePermission(int $id, string $name, string $description = '')
{
$data = [
'name' => $name,
];
if (! empty($description))
{
$data['description'] = $description;
}
if (! $this->permissionModel->update((int)$id, $data))
{
$this->error = $this->permissionModel->error();
return false;
}
return true;
}
/**
* Verifies that a permission (either ID or the name) exists and returns
* the permission ID.
*
* @param int|string $permission
*
* @return int|null
*/
protected function getPermissionID($permission)
{
// If it's a number, we're done here.
if (is_numeric($permission))
{
return (int)$permission;
}
// Otherwise, pull it from the database.
$p = $this->permissionModel->findWhere('name', $permission);
if (! $p)
{
$this->error = lang('Auth.permissionNotFound', [$permission]);
return false;
}
return (int)$p->id;
}
}