|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\security\acl; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\security\acl\models\AclList; |
|
5
|
|
|
use Ubiquity\security\acl\models\Role; |
|
6
|
|
|
use Ubiquity\security\acl\models\Resource; |
|
7
|
|
|
use Ubiquity\security\acl\models\Permission; |
|
8
|
|
|
use Ubiquity\security\acl\models\AclElement; |
|
9
|
|
|
use Ubiquity\security\acl\persistence\AclProviderInterface; |
|
10
|
|
|
use Ubiquity\security\acl\models\AbstractAclPart; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Ubiquity\security\acl$AclManager |
|
14
|
|
|
* This class is part of Ubiquity |
|
15
|
|
|
* |
|
16
|
|
|
* @author jc |
|
17
|
|
|
* @version 1.0.0 |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class AclManager { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var AclList |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $aclList; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Create AclList with default roles and resources. |
|
30
|
|
|
*/ |
|
31
|
15 |
|
public static function start(): void { |
|
32
|
15 |
|
self::$aclList = new AclList(); |
|
33
|
15 |
|
self::$aclList->init(); |
|
34
|
15 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Load acls, roles, resources and permissions from providers. |
|
38
|
|
|
* |
|
39
|
|
|
* @param AclProviderInterface[] $providers |
|
40
|
|
|
*/ |
|
41
|
7 |
|
public static function initFromProviders(?array $providers = []): void { |
|
42
|
7 |
|
self::$aclList->setProviders($providers); |
|
43
|
7 |
|
if (\count($providers) > 0) { |
|
44
|
6 |
|
self::$aclList->loadAcls(); |
|
45
|
6 |
|
self::$aclList->loadRoles(); |
|
46
|
6 |
|
self::$aclList->loadResources(); |
|
47
|
6 |
|
self::$aclList->loadPermissions(); |
|
48
|
|
|
} |
|
49
|
7 |
|
} |
|
50
|
|
|
|
|
51
|
5 |
|
public static function addRole(string $name, ?array $parents = []) { |
|
52
|
5 |
|
self::$aclList->addRole(new Role($name, $parents)); |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function addRoles(array $nameParents) { |
|
56
|
|
|
foreach ($nameParents as $name => $parents) { |
|
57
|
|
|
self::$aclList->addRole(new Role($name, $parents)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
public static function addResource(string $name, ?string $value = null) { |
|
62
|
4 |
|
self::$aclList->addResource(new Resource($name, $value)); |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
public static function addResources(array $nameValue) { |
|
66
|
|
|
foreach ($nameValue as $name => $value) { |
|
67
|
|
|
self::$aclList->addResource(new Resource($name, $value)); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
public static function addPermission(string $name, int $level = 0) { |
|
72
|
5 |
|
self::$aclList->addPermission(new Permission($name, $level)); |
|
73
|
5 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static function addPermissions(array $nameLevel) { |
|
76
|
|
|
foreach ($nameLevel as $name => $level) { |
|
77
|
|
|
self::$aclList->addPermission(new Permission($name, $level)); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
public static function setPermissionLevel(string $name, int $level) { |
|
82
|
3 |
|
self::$aclList->setPermissionLevel($name, $level); |
|
83
|
2 |
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
public static function getRoles() { |
|
86
|
4 |
|
return self::$aclList->getRoles(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
public static function getResources() { |
|
90
|
3 |
|
return self::$aclList->getResources(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
6 |
|
public static function getPermissions() { |
|
94
|
6 |
|
return self::$aclList->getPermissions(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
public static function getAcls() { |
|
98
|
3 |
|
return self::$aclList->getAcls(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Allow role to access to resource with the permission. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $role |
|
105
|
|
|
* @param string $resource |
|
106
|
|
|
* @param string $permission |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
|
109
|
6 |
|
self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
|
110
|
6 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Add role, resource and permission and allow this role to access to resource with the permission. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $role |
|
116
|
|
|
* @param string $resource |
|
117
|
|
|
* @param string $permission |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
|
120
|
1 |
|
self::$aclList->addRole(new Role($role, [])); |
|
121
|
1 |
|
if ($resource !== '*') { |
|
122
|
1 |
|
self::$aclList->addResource(new Resource($resource)); |
|
123
|
|
|
} |
|
124
|
1 |
|
if ($permission !== 'ALL') { |
|
125
|
1 |
|
self::$aclList->addPermission(new Permission($permission)); |
|
126
|
|
|
} |
|
127
|
1 |
|
self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
|
128
|
1 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Check if access to resource is allowed for role with the permission. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $role |
|
134
|
|
|
* @param string $resource |
|
135
|
|
|
* @param string $permission |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
15 |
|
public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool { |
|
139
|
15 |
|
return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
public static function saveAll() { |
|
143
|
2 |
|
self::$aclList->saveAll(); |
|
144
|
2 |
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
public static function removeRole(string $role) { |
|
147
|
2 |
|
self::$aclList->removeRole($role); |
|
148
|
2 |
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
public static function removePermission(string $permission) { |
|
151
|
2 |
|
self::$aclList->removePermission($permission); |
|
152
|
2 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
public static function removeResource(string $resource) { |
|
155
|
|
|
self::$aclList->removeResource($resource); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
public static function removeAcl(string $role, string $resource, string $permission = null) { |
|
159
|
2 |
|
self::$aclList->removeAcl($role, $resource, $permission); |
|
160
|
2 |
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|