Passed
Push — main ( 0ede48...ff177d )
by Jean-Christophe
02:14
created

AclManager   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 318
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 90
c 4
b 0
f 1
dl 0
loc 318
ccs 124
cts 140
cp 0.8857
rs 6.96
wmc 53

34 Methods

Rating   Name   Duplication   Size   Complexity  
A addRole() 0 2 1
A getPermissions() 0 2 1
A addResources() 0 3 2
A addPermissions() 0 3 2
A getAcls() 0 2 1
A addRoles() 0 3 2
A getRoles() 0 2 1
A getResources() 0 2 1
A initFromProviders() 0 7 2
A removeResource() 0 2 1
A removeRole() 0 2 1
A getAclList() 0 2 1
A setPermissionLevel() 0 2 1
A removePermission() 0 2 1
A start() 0 3 1
A allow() 0 2 1
A addAndAllow() 0 2 1
A addResource() 0 2 1
A removeAcl() 0 2 1
A isAllowed() 0 2 1
A addPermission() 0 2 1
A saveAll() 0 2 1
A reloadFromSelectedProviders() 0 11 5
A existPartIn() 0 2 1
A getProvider() 0 2 1
A filterProviders() 0 10 3
A removefilterProviders() 0 2 1
B initCache() 0 28 6
A getModelClassesSwap() 0 9 3
A isStarted() 0 2 2
A existAclIn() 0 2 1
A getPermissionMap() 0 6 2
A registerAnnotations() 0 2 1
A associate() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like AclManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AclManager, and based on these observations, apply Extract Interface, too.

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