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\persistence\AclProviderInterface; |
9
|
|
|
use Ubiquity\cache\ClassUtils; |
10
|
|
|
use Ubiquity\security\acl\cache\AclControllerParser; |
11
|
|
|
use Ubiquity\exceptions\AclException; |
12
|
|
|
use Ubiquity\cache\CacheManager; |
13
|
|
|
use Ubiquity\annotations\acl\AllowAnnotation; |
14
|
|
|
use Ubiquity\annotations\acl\ResourceAnnotation; |
15
|
|
|
use Ubiquity\annotations\acl\PermissionAnnotation; |
16
|
|
|
use Ubiquity\security\acl\cache\PermissionsMap; |
17
|
|
|
use Ubiquity\security\acl\models\AbstractAclPart; |
18
|
|
|
use Ubiquity\security\acl\models\AclElement; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Ubiquity\security\acl$AclManager |
22
|
|
|
* This class is part of Ubiquity |
23
|
|
|
* |
24
|
|
|
* @author jc |
25
|
|
|
* @version 1.0.0 |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
class AclManager { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var AclList |
33
|
|
|
*/ |
34
|
|
|
protected static $aclList; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var PermissionsMap |
39
|
|
|
*/ |
40
|
|
|
protected static $permissionMap; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create AclList with default roles and resources. |
44
|
|
|
*/ |
45
|
21 |
|
public static function start(): void { |
46
|
21 |
|
self::$aclList = new AclList(); |
47
|
21 |
|
self::$aclList->init(); |
48
|
21 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load acls, roles, resources and permissions from providers. |
52
|
|
|
* |
53
|
|
|
* @param AclProviderInterface[] $providers |
54
|
|
|
*/ |
55
|
10 |
|
public static function initFromProviders(?array $providers = []): void { |
56
|
10 |
|
self::$aclList->setProviders($providers); |
57
|
10 |
|
if (\count($providers) > 0) { |
58
|
9 |
|
self::$aclList->loadAcls(); |
59
|
9 |
|
self::$aclList->loadRoles(); |
60
|
9 |
|
self::$aclList->loadResources(); |
61
|
9 |
|
self::$aclList->loadPermissions(); |
62
|
|
|
} |
63
|
10 |
|
} |
64
|
|
|
|
65
|
6 |
|
public static function addRole(string $name, ?array $parents = []) { |
66
|
6 |
|
self::$aclList->addRole(new Role($name, $parents)); |
67
|
6 |
|
} |
68
|
|
|
|
69
|
1 |
|
public static function addRoles(array $nameParents) { |
70
|
1 |
|
foreach ($nameParents as $name => $parents) { |
71
|
1 |
|
self::$aclList->addRole(new Role($name, $parents)); |
72
|
|
|
} |
73
|
1 |
|
} |
74
|
|
|
|
75
|
8 |
|
public static function addResource(string $name, ?string $value = null) { |
76
|
8 |
|
self::$aclList->addResource(new Resource($name, $value)); |
77
|
8 |
|
} |
78
|
|
|
|
79
|
1 |
|
public static function addResources(array $nameValue) { |
80
|
1 |
|
foreach ($nameValue as $name => $value) { |
81
|
1 |
|
self::$aclList->addResource(new Resource($name, $value)); |
82
|
|
|
} |
83
|
1 |
|
} |
84
|
|
|
|
85
|
10 |
|
public static function addPermission(string $name, int $level = 0) { |
86
|
10 |
|
self::$aclList->addPermission(new Permission($name, $level)); |
87
|
10 |
|
} |
88
|
|
|
|
89
|
1 |
|
public static function addPermissions(array $nameLevel) { |
90
|
1 |
|
foreach ($nameLevel as $name => $level) { |
91
|
1 |
|
self::$aclList->addPermission(new Permission($name, $level)); |
92
|
|
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
3 |
|
public static function setPermissionLevel(string $name, int $level) { |
96
|
3 |
|
self::$aclList->setPermissionLevel($name, $level); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
9 |
|
public static function getRoles() { |
100
|
9 |
|
return self::$aclList->getRoles(); |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
public static function getResources() { |
104
|
8 |
|
return self::$aclList->getResources(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @return \Ubiquity\security\acl\models\AclList |
110
|
|
|
*/ |
111
|
2 |
|
public static function getAclList() { |
112
|
2 |
|
return AclManager::$aclList; |
113
|
|
|
} |
114
|
|
|
|
115
|
12 |
|
public static function getPermissions() { |
116
|
12 |
|
return self::$aclList->getPermissions(); |
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
public static function getAcls() { |
120
|
6 |
|
return self::$aclList->getAcls(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Allow role to access to resource with the permission. |
125
|
|
|
* |
126
|
|
|
* @param string $role |
127
|
|
|
* @param string $resource |
128
|
|
|
* @param string $permission |
129
|
|
|
*/ |
130
|
9 |
|
public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
131
|
9 |
|
self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
132
|
9 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Add role, resource and permission and allow this role to access to resource with the permission. |
136
|
|
|
* |
137
|
|
|
* @param string $role |
138
|
|
|
* @param string $resource |
139
|
|
|
* @param string $permission |
140
|
|
|
*/ |
141
|
4 |
|
public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
142
|
4 |
|
self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL'); |
143
|
4 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if access to resource is allowed for role with the permission. |
147
|
|
|
* |
148
|
|
|
* @param string $role |
149
|
|
|
* @param string $resource |
150
|
|
|
* @param string $permission |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
20 |
|
public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool { |
154
|
20 |
|
return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Save all acls,roles, resources and permissions for AclProviders with no autoSave. |
159
|
|
|
*/ |
160
|
5 |
|
public static function saveAll() { |
161
|
5 |
|
self::$aclList->saveAll(); |
162
|
5 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* |
166
|
|
|
* @param string $role |
167
|
|
|
*/ |
168
|
4 |
|
public static function removeRole(string $role) { |
169
|
4 |
|
self::$aclList->removeRole($role); |
170
|
4 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* |
174
|
|
|
* @param string $permission |
175
|
|
|
*/ |
176
|
4 |
|
public static function removePermission(string $permission) { |
177
|
4 |
|
self::$aclList->removePermission($permission); |
178
|
4 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* |
182
|
|
|
* @param string $resource |
183
|
|
|
*/ |
184
|
2 |
|
public static function removeResource(string $resource) { |
185
|
2 |
|
self::$aclList->removeResource($resource); |
186
|
2 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* |
190
|
|
|
* @param string $role |
191
|
|
|
* @param string $resource |
192
|
|
|
* @param string $permission |
193
|
|
|
*/ |
194
|
4 |
|
public static function removeAcl(string $role, string $resource, string $permission = null) { |
195
|
4 |
|
self::$aclList->removeAcl($role, $resource, $permission); |
196
|
4 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Initialize acls cache with controllers annotations. |
200
|
|
|
* Do not execute at runtime |
201
|
|
|
* |
202
|
|
|
* @param array $config |
203
|
|
|
* @throws \Ubiquity\exceptions\AclException |
204
|
|
|
*/ |
205
|
3 |
|
public static function initCache(&$config) { |
206
|
3 |
|
CacheManager::start($config); |
207
|
3 |
|
CacheManager::registerAnnotations([ |
208
|
3 |
|
'allow' => AllowAnnotation::class, |
209
|
|
|
'resource' => ResourceAnnotation::class, |
210
|
|
|
'permission' => PermissionAnnotation::class |
211
|
|
|
]); |
212
|
3 |
|
$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true); |
213
|
3 |
|
$parser = new AclControllerParser(); |
214
|
3 |
|
$parser->init(); |
215
|
3 |
|
foreach ($files as $file) { |
216
|
3 |
|
if (\is_file($file)) { |
217
|
3 |
|
$controller = ClassUtils::getClassFullNameFromFile($file); |
218
|
|
|
try { |
219
|
3 |
|
$parser->parse($controller); |
220
|
|
|
} catch (\Exception $e) { |
221
|
|
|
if ($e instanceof AclException) { |
222
|
|
|
throw $e; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
3 |
|
$parser->save(); |
228
|
3 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* |
232
|
|
|
* @return \Ubiquity\security\acl\cache\PermissionsMap |
233
|
|
|
*/ |
234
|
1 |
|
public static function getPermissionMap() { |
235
|
1 |
|
if (! isset(self::$permissionMap)) { |
236
|
1 |
|
self::$permissionMap = new PermissionsMap(); |
237
|
1 |
|
self::$permissionMap->load(); |
238
|
|
|
} |
239
|
1 |
|
return self::$permissionMap; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* |
244
|
|
|
* @param string $controller |
245
|
|
|
* @param string $action |
246
|
|
|
* @param string $resource |
247
|
|
|
* @param string $permission |
248
|
|
|
*/ |
249
|
1 |
|
public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL') { |
250
|
1 |
|
self::$aclList->getResourceByName($resource); |
251
|
1 |
|
self::$aclList->getPermissionByName($permission); |
252
|
1 |
|
self::$permissionMap->addAction($controller, $action, $resource, $permission); |
253
|
1 |
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* |
257
|
|
|
* @param AbstractAclPart $part |
258
|
|
|
* @param string $providerClass |
259
|
|
|
* @return boolean |
260
|
|
|
*/ |
261
|
2 |
|
public static function existPartIn(AbstractAclPart $part, string $providerClass) { |
262
|
2 |
|
return self::$aclList->existPartIn($part, $providerClass); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* |
267
|
|
|
* @param AclElement $elm |
268
|
|
|
* @param string $providerClass |
269
|
|
|
* @return boolean |
270
|
|
|
*/ |
271
|
2 |
|
public static function existAclIn(AclElement $elm, string $providerClass) { |
272
|
2 |
|
return self::$aclList->existAclIn($elm, $providerClass); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* |
277
|
|
|
* @param string $providerClass |
278
|
|
|
* @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL |
279
|
|
|
*/ |
280
|
2 |
|
public static function getProvider(string $providerClass) { |
281
|
2 |
|
return self::$aclList->getProvider($providerClass); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|