Passed
Push — main ( 70119b...59b401 )
by Jean-Christophe
02:05
created

AclManager::addPermission()   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;
0 ignored issues
show
Bug introduced by
The type Ubiquity\cache\CacheManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Ubiquity\cache\ClassUtils;
0 ignored issues
show
Bug introduced by
The type Ubiquity\cache\ClassUtils was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	public static function isStarted(): bool {
55
		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 === '*' || \array_search(\get_class($prov), $selectedProviders) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $selectedProviders can also be of type string; however, parameter $haystack of array_search() does only seem to accept array, 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

82
			if ($selectedProviders === '*' || \array_search(\get_class($prov), /** @scrutinizer ignore-type */ $selectedProviders) !== false) {
Loading history...
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
		self::filterProviders(AclCacheProvider::class);
232 2
		self::reloadFromSelectedProviders([]);
233 2
		self::registerAnnotations($config);
234 2
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
235 2
		$parser = new AclControllerParser();
236 2
		$parser->init();
237 2
		foreach ($files as $file) {
238 2
			if (\is_file($file)) {
239 2
				$controller = ClassUtils::getClassFullNameFromFile($file);
240
				try {
241 2
					$parser->parse($controller);
242
				} catch (\Exception $e) {
243
					if ($e instanceof AclException) {
244
						throw $e;
245
					}
246
				}
247
			}
248
		}
249 2
		$parser->save();
250 2
		self::removefilterProviders();
251 2
		self::reloadFromSelectedProviders();
252 2
	}
253
254 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

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