Passed
Push — main ( 6cee10...25dcc1 )
by Jean-Christophe
02:23
created

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

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

259
	public static function registerAnnotations(/** @scrutinizer ignore-unused */ &$config) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
260 2
		CacheManager::registerAnnotations([
261 2
			'allow' => AllowAnnotation::class,
262
			'resource' => ResourceAnnotation::class,
263
			'permission' => PermissionAnnotation::class
264
		]);
265 2
	}
266
267
	/**
268
	 *
269
	 * @return \Ubiquity\security\acl\cache\PermissionsMap
270
	 */
271 1
	public static function getPermissionMap() {
272 1
		if (! isset(self::$permissionMap)) {
273 1
			self::$permissionMap = new PermissionsMap();
274 1
			self::$permissionMap->load();
275
		}
276 1
		return self::$permissionMap;
277
	}
278
279
	/**
280
	 *
281
	 * @param string $controller
282
	 * @param string $action
283
	 * @param string $resource
284
	 * @param string $permission
285
	 */
286 1
	public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL') {
287 1
		self::$aclList->getResourceByName($resource);
288 1
		self::$aclList->getPermissionByName($permission);
289 1
		self::$permissionMap->addAction($controller, $action, $resource, $permission);
290 1
	}
291
292
	/**
293
	 *
294
	 * @param AbstractAclPart $part
295
	 * @param string $providerClass
296
	 * @return boolean
297
	 */
298 2
	public static function existPartIn(AbstractAclPart $part, string $providerClass) {
299 2
		return self::$aclList->existPartIn($part, $providerClass);
300
	}
301
302
	/**
303
	 *
304
	 * @param AclElement $elm
305
	 * @param string $providerClass
306
	 * @return boolean
307
	 */
308 2
	public static function existAclIn(AclElement $elm, string $providerClass) {
309 2
		return self::$aclList->existAclIn($elm, $providerClass);
310
	}
311
312
	/**
313
	 *
314
	 * @param string $providerClass
315
	 * @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL
316
	 */
317 2
	public static function getProvider(string $providerClass) {
318 2
		return self::$aclList->getProvider($providerClass);
319
	}
320
321
	public static function getModelClassesSwap(): array {
322
		$result = [];
323
		$aclList = self::getAclList();
324
		if (isset($aclList)) {
325
			foreach ($aclList->getProviders() as $prov) {
326
				$result += $prov->getModelClassesSwap();
327
			}
328
		}
329
		return $result;
330
	}
331
332 2
	public static function filterProviders(string $providerClass) {
333 2
		$providers = self::$aclList->getProviders();
334 2
		$filter = [];
335 2
		foreach ($providers as $prov) {
336 2
			if ($prov instanceof $providerClass) {
337 2
				$filter[] = $prov;
338
			}
339
		}
340 2
		self::$aclList->setProviders($filter);
341 2
		self::$providersPersistence = $providers;
342 2
	}
343
344 2
	public static function removefilterProviders() {
345 2
		self::$aclList->setProviders(self::$providersPersistence);
346 2
	}
347
}
348
349