-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathPermissionCalculator.php
More file actions
75 lines (63 loc) · 2.34 KB
/
PermissionCalculator.php
File metadata and controls
75 lines (63 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Allows modules to define permissions.
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.0.0-pr8
* @license MIT
*/
class PermissionCalculator
{
private PermissionCache $_permission_cache;
public function __construct(PermissionCache $permission_cache)
{
$this->_permission_cache = $permission_cache;
}
public function userHasPermission(User $user, string $permission): bool
{
$user_permissions = $this->_permission_cache->getOrLoad(User::class, $user->data()->id);
$result = $user_permissions[$permission] ?? PermissionTristate::INHERIT;
if ($result === PermissionTristate::TRUE) {
return true;
}
if ($result === PermissionTristate::FALSE) {
return false;
}
if ($result === PermissionTristate::INHERIT) {
foreach ($user->getGroups() as $group) {
if ($this->groupHasPermission($group, $permission)) {
return true;
}
}
}
return false;
}
public function groupHasPermission(Group $group, string $permission): bool
{
$result = $this->_permission_cache->getOrLoad(Group::class, $group->id)[$permission] ?? PermissionTristate::INHERIT;
if ($result === PermissionTristate::TRUE) {
return true;
}
if ($result === PermissionTristate::FALSE) {
return false;
}
if ($result === PermissionTristate::INHERIT) {
// if any of the groups with a lower order have the permission set to true or false, then this group inherits that value
$inherit = false;
$lower_order_groups = DB::getInstance()->query('SELECT id FROM nl2_groups WHERE `order` < ? ORDER BY `order`', [$group->order]);
foreach ($lower_order_groups as $lower_order_group) {
$result2 = $this->_permission_cache->getOrLoad(Group::class, $lower_order_group->id)[$permission] ?? PermissionTristate::INHERIT;
if ($result2 === PermissionTristate::TRUE) {
$inherit = true;
break;
} elseif ($result2 === PermissionTristate::FALSE) {
$inherit = false;
break;
}
}
return $inherit;
}
return false;
}
}