Passed
Push — main ( 8815d3...d06024 )
by Jean-Christophe
02:38
created

AclManager::addRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
namespace Ubiquity\security\acl;
3
4
use Ubiquity\cache\CacheManager;
5
use Ubiquity\cache\ClassUtils;
6
use Ubiquity\exceptions\AclException;
7
use Ubiquity\security\acl\cache\AclControllerParser;
8
use Ubiquity\security\acl\cache\PermissionsMap;
9
use Ubiquity\security\acl\models\AbstractAclPart;
10
use Ubiquity\security\acl\models\AclElement;
11
use Ubiquity\security\acl\models\AclList;
12
use Ubiquity\security\acl\models\Permission;
13
use Ubiquity\security\acl\models\Resource;
14
use Ubiquity\security\acl\models\Role;
15
use Ubiquity\security\acl\persistence\AclCacheProvider;
16
use Ubiquity\controllers\Router;
17
use Ubiquity\security\acl\persistence\AclProviderInterface;
18
19
/**
20
 * Ubiquity\security\acl$AclManager
21
 * This class is part of Ubiquity
22
 *
23
 * @author jc
24
 * @version 1.0.1
25
 *
26
 */
27
class AclManager {
28
29
	protected static AclList $aclList;
30
31
	protected static PermissionsMap $permissionMap;
32
33
	protected static array $providersPersistence;
34
35
	/**
36
	 * Create AclList with default roles and resources.
37
	 */
38 22
	public static function start(): void {
39 22
		self::$aclList = new AclList();
40 22
		self::$aclList->init();
41 22
	}
42
	
43
	/**
44
	 * Start the Acls with AclCacheProvider (for attributes or annotations).
45
	 */
46 6
	public static function startWithCacheProvider(): void {
47 6
		self::start();
48 6
		self::initFromProviders([new AclCacheProvider()]);
49 6
	}
50
51
	/**
52
	 * Check whether the Acl service is started.
53
	 *
54
	 * @return bool
55
	 */
56 2
	public static function isStarted(): bool {
57 2
		return self::$aclList !== NULL && (self::$aclList instanceof AclList);
58
	}
59
60
	/**
61
	 * Load acls, roles, resources and permissions from providers.
62
	 *
63
	 * @param AclProviderInterface[] $providers
64
	 */
65 11
	public static function initFromProviders(?array $providers = []): void {
66 11
		self::$aclList->setProviders($providers);
67 11
		if (\count($providers) > 0) {
68 10
			self::$aclList->loadAcls();
69 10
			self::$aclList->loadRoles();
70 10
			self::$aclList->loadResources();
71 10
			self::$aclList->loadPermissions();
72
		}
73 11
	}
74
75
	/**
76
	 *
77
	 * @param array|string $selectedProviders
78
	 */
79 3
	public static function reloadFromSelectedProviders($selectedProviders = '*') {
80 3
		$sProviders = self::$aclList->getProviders();
81 3
		self::$aclList->clear();
82 3
		$providers = [];
83 3
		foreach ($sProviders as $prov) {
84 3
			if ($selectedProviders === '*' || (\is_array($selectedProviders) && \array_search(\get_class($prov), $selectedProviders) !== false)) {
85 3
				$providers[] = $prov;
86
			}
87
		}
88 3
		self::initFromProviders($providers);
89 3
		self::$aclList->setProviders($sProviders);
90 3
	}
91
92 8
	public static function addRole(string $name, ?array $parents = []) {
93 8
		self::$aclList->addRole(new Role($name, $parents));
94 8
	}
95
96 1
	public static function addRoles(array $nameParents) {
97 1
		foreach ($nameParents as $name => $parents) {
98 1
			self::$aclList->addRole(new Role($name, $parents));
99
		}
100 1
	}
101
102 7
	public static function addResource(string $name, ?string $value = null) {
103 7
		self::$aclList->addResource(new Resource($name, $value));
104 7
	}
105
106 1
	public static function addResources(array $nameValue) {
107 1
		foreach ($nameValue as $name => $value) {
108 1
			self::$aclList->addResource(new Resource($name, $value));
109
		}
110 1
	}
111
112 9
	public static function addPermission(string $name, int $level = 0) {
113 9
		self::$aclList->addPermission(new Permission($name, $level));
114 9
	}
115
116 1
	public static function addPermissions(array $nameLevel) {
117 1
		foreach ($nameLevel as $name => $level) {
118 1
			self::$aclList->addPermission(new Permission($name, $level));
119
		}
120 1
	}
121
122 3
	public static function setPermissionLevel(string $name, int $level) {
123 3
		self::$aclList->setPermissionLevel($name, $level);
124 2
	}
125
126 8
	public static function getRoles() {
127 8
		return self::$aclList->getRoles();
128
	}
129
130 7
	public static function getResources() {
131 7
		return self::$aclList->getResources();
132
	}
133
134
	/**
135
	 *
136
	 * @return \Ubiquity\security\acl\models\AclList
137
	 */
138 3
	public static function getAclList() {
139 3
		return AclManager::$aclList;
140
	}
141
142 11
	public static function getPermissions():array {
143 11
		return self::$aclList->getPermissions();
144
	}
145
146 6
	public static function getAcls() {
147 6
		return self::$aclList->getAcls();
148
	}
149
150
	/**
151
	 * Allow role to access to resource with the permission.
152
	 *
153
	 * @param string $role
154
	 * @param ?string $resource
155
	 * @param ?string $permission
156
	 */
157 10
	public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
158 10
		self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL');
159 10
	}
160
161
	/**
162
	 * Add role, resource and permission and allow this role to access to resource with the permission.
163
	 *
164
	 * @param string $role
165
	 * @param ?string $resource
166
	 * @param ?string $permission
167
	 */
168 3
	public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
169 3
		self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL');
170 3
	}
171
172
	/**
173
	 * Check if access to resource is allowed for role with the permission.
174
	 *
175
	 * @param string $role
176
	 * @param ?string $resource
177
	 * @param ?string $permission
178
	 * @return bool
179
	 */
180 20
	public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool {
181 20
		return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL');
182
	}
183
	
184
	public static function isAllowedRoute(string $role,string $routeName){
185
		$routeInfo=Router::getRouteInfoByName($routeName);
186
		if (!isset ( $routeDetails ['controller'] )) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $routeDetails seems to never exist and therefore isset should always be false.
Loading history...
187
			$routeInfo=current($routeInfo);
0 ignored issues
show
Bug introduced by
It seems like $routeInfo can also be of type false; however, parameter $array of current() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

187
			$routeInfo=current(/** @scrutinizer ignore-type */ $routeInfo);
Loading history...
188
		}
189
		$controller=$routeInfo['controller']??null;
190
		$action=$routeInfo['action']??null;
191
		if(isset($controller) && isset($action)){
192
			$resourceController = self::getPermissionMap ()->getRessourcePermission ( $controller, $action );
193
			if (isset ( $resourceController )) {
194
				try{
195
					if (self::isAllowed ( $role, $resourceController ['resource'], $resourceController ['permission'] )) {
196
						return true;
197
					}
198
				}
199
				catch(AclException $e){
200
					//Nothing to do
201
				}
202
			}
203
			return false;
204
		}
205
		return false;
206
	}
207
208
	/**
209
	 * Save all acls,roles, resources and permissions for AclProviders with no autoSave.
210
	 */
211 4
	public static function saveAll() {
212 4
		self::$aclList->saveAll();
213 4
	}
214
215
	/**
216
	 *
217
	 * @param string $role
218
	 */
219 2
	public static function removeRole(string $role) {
220 2
		self::$aclList->removeRole($role);
221 2
	}
222
223
	/**
224
	 *
225
	 * @param string $permission
226
	 */
227 2
	public static function removePermission(string $permission) {
228 2
		self::$aclList->removePermission($permission);
229 2
	}
230
231
	/**
232
	 *
233
	 * @param string $resource
234
	 */
235
	public static function removeResource(string $resource) {
236
		self::$aclList->removeResource($resource);
237
	}
238
239
	/**
240
	 *
241
	 * @param string $role
242
	 * @param string $resource
243
	 * @param ?string $permission
244
	 */
245 2
	public static function removeAcl(string $role, string $resource, ?string $permission = null) {
246 2
		self::$aclList->removeAcl($role, $resource, $permission);
247 2
	}
248
249
	/**
250
	 * Initialize acls cache with controllers annotations.
251
	 * Do not execute at runtime
252
	 *
253
	 * @param array $config
254
	 * @throws \Ubiquity\exceptions\AclException
255
	 */
256 2
	public static function initCache(&$config) {
257 2
		if(!self::isStarted()){
258
			self::start();
259
			self::initFromProviders([
260
				new AclCacheProvider()
261
			]);
262
		}
263 2
		self::filterProviders(AclCacheProvider::class);
264 2
		self::reloadFromSelectedProviders([]);
265 2
		self::registerAnnotations();
266 2
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
267 2
		$parser = new AclControllerParser();
268 2
		$parser->init();
269 2
		foreach ($files as $file) {
270 2
			if (\is_file($file)) {
271 2
				$controller = ClassUtils::getClassFullNameFromFile($file);
272
				try {
273 2
					$parser->parse($controller);
274
				} catch (\Exception $e) {
275
					if ($e instanceof AclException) {
276
						throw $e;
277
					}
278
				}
279
			}
280
		}
281 2
		$parser->save();
282 2
		self::removefilterProviders();
283 2
		self::reloadFromSelectedProviders();
284 2
	}
285
286 2
	protected static function registerAnnotations() {
287 2
		CacheManager::getAnnotationsEngineInstance()->registerAcls();
288 2
	}
289
290
	/**
291
	 *
292
	 * @return \Ubiquity\security\acl\cache\PermissionsMap
293
	 */
294 1
	public static function getPermissionMap():PermissionsMap {
295 1
		if (! isset(self::$permissionMap)) {
296 1
			self::$permissionMap = new PermissionsMap();
297 1
			self::$permissionMap->load();
298
		}
299 1
		return self::$permissionMap;
300
	}
301
302
	/**
303
	 *
304
	 * @param string $controller
305
	 * @param string $action
306
	 * @param string $resource
307
	 * @param string $permission
308
	 */
309 1
	public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL'):void {
310 1
		self::$aclList->getResourceByName($resource);
311 1
		self::$aclList->getPermissionByName($permission);
312 1
		self::$permissionMap->addAction($controller, $action, $resource, $permission);
313 1
	}
314
315
	/**
316
	 *
317
	 * @param AbstractAclPart $part
318
	 * @param string $providerClass
319
	 * @return boolean
320
	 */
321 2
	public static function existPartIn(AbstractAclPart $part, string $providerClass):bool {
322 2
		return self::$aclList->existPartIn($part, $providerClass);
323
	}
324
325
	/**
326
	 *
327
	 * @param AclElement $elm
328
	 * @param string $providerClass
329
	 * @return boolean
330
	 */
331 2
	public static function existAclIn(AclElement $elm, string $providerClass):bool {
332 2
		return self::$aclList->existAclIn($elm, $providerClass);
333
	}
334
335
	/**
336
	 *
337
	 * @param string $providerClass
338
	 * @return AclProviderInterface|NULL
339
	 */
340 2
	public static function getProvider(string $providerClass):?AclProviderInterface {
341 2
		return self::$aclList->getProvider($providerClass);
342
	}
343
344
	public static function getModelClassesSwap(): array {
345
		$result = [];
346
		$aclList = self::getAclList();
347
		if (isset($aclList)) {
348
			foreach ($aclList->getProviders() as $prov) {
349
				$result += $prov->getModelClassesSwap();
350
			}
351
		}
352
		return $result;
353
	}
354
355 2
	public static function filterProviders(string $providerClass):void {
356 2
		$providers = self::$aclList->getProviders();
357 2
		$filter = [];
358 2
		foreach ($providers as $prov) {
359 2
			if ($prov instanceof $providerClass) {
360 2
				$filter[] = $prov;
361
			}
362
		}
363 2
		self::$aclList->setProviders($filter);
364 2
		self::$providersPersistence = $providers;
365 2
	}
366
367 2
	public static function removefilterProviders():void {
368 2
		self::$aclList->setProviders(self::$providersPersistence);
369 2
	}
370
}
371
372