Test Failed
Push — main ( 85a925...1676fa )
by Jean-Christophe
02:18
created

AclManager::reloadFromSelectedProviders()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
ccs 7
cts 7
cp 1
crap 4
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
	/**
66 6
	 *
67 6
	 * @param array|string $selectedProviders
68
	 */
69 1
	public static function reloadFromSelectedProviders($selectedProviders = '*') {
70 1
		self::$aclList->clear();
71 1
		$providers = [];
72
		foreach (self::$aclList->getProviders() as $prov) {
73 1
			if ($selectedProviders === '*' || \array_search(\get_class($prov), $selectedProviders) !== false) {
1 ignored issue
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

73
			if ($selectedProviders === '*' || \array_search(\get_class($prov), /** @scrutinizer ignore-type */ $selectedProviders) !== false) {
Loading history...
74
				$providers[] = $prov;
75 8
			}
76 8
		}
77 8
		self::initFromProviders($providers);
78
	}
79 1
80 1
	public static function addRole(string $name, ?array $parents = []) {
81 1
		self::$aclList->addRole(new Role($name, $parents));
82
	}
83 1
84
	public static function addRoles(array $nameParents) {
85 10
		foreach ($nameParents as $name => $parents) {
86 10
			self::$aclList->addRole(new Role($name, $parents));
87 10
		}
88
	}
89 1
90 1
	public static function addResource(string $name, ?string $value = null) {
91 1
		self::$aclList->addResource(new Resource($name, $value));
92
	}
93 1
94
	public static function addResources(array $nameValue) {
95 3
		foreach ($nameValue as $name => $value) {
96 3
			self::$aclList->addResource(new Resource($name, $value));
97 2
		}
98
	}
99 9
100 9
	public static function addPermission(string $name, int $level = 0) {
101
		self::$aclList->addPermission(new Permission($name, $level));
102
	}
103 8
104 8
	public static function addPermissions(array $nameLevel) {
105
		foreach ($nameLevel as $name => $level) {
106
			self::$aclList->addPermission(new Permission($name, $level));
107
		}
108
	}
109
110
	public static function setPermissionLevel(string $name, int $level) {
111 2
		self::$aclList->setPermissionLevel($name, $level);
112 2
	}
113
114
	public static function getRoles() {
115 12
		return self::$aclList->getRoles();
116 12
	}
117
118
	public static function getResources() {
119 6
		return self::$aclList->getResources();
120 6
	}
121
122
	/**
123
	 *
124
	 * @return \Ubiquity\security\acl\models\AclList
125
	 */
126
	public static function getAclList() {
127
		return AclManager::$aclList;
128
	}
129
130 9
	public static function getPermissions() {
131 9
		return self::$aclList->getPermissions();
132 9
	}
133
134
	public static function getAcls() {
135
		return self::$aclList->getAcls();
136
	}
137
138
	/**
139
	 * Allow role to access to resource with the permission.
140
	 *
141 4
	 * @param string $role
142 4
	 * @param string $resource
143 4
	 * @param string $permission
144
	 */
145
	public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
146
		self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL');
147
	}
148
149
	/**
150
	 * Add role, resource and permission and allow this role to access to resource with the permission.
151
	 *
152
	 * @param string $role
153 20
	 * @param string $resource
154 20
	 * @param string $permission
155
	 */
156
	public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
157
		self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL');
158
	}
159
160 5
	/**
161 5
	 * Check if access to resource is allowed for role with the permission.
162 5
	 *
163
	 * @param string $role
164
	 * @param string $resource
165
	 * @param string $permission
166
	 * @return bool
167
	 */
168 4
	public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool {
169 4
		return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL');
170 4
	}
171
172
	/**
173
	 * Save all acls,roles, resources and permissions for AclProviders with no autoSave.
174
	 */
175
	public static function saveAll() {
176 4
		self::$aclList->saveAll();
177 4
	}
178 4
179
	/**
180
	 *
181
	 * @param string $role
182
	 */
183
	public static function removeRole(string $role) {
184 2
		self::$aclList->removeRole($role);
185 2
	}
186 2
187
	/**
188
	 *
189
	 * @param string $permission
190
	 */
191
	public static function removePermission(string $permission) {
192
		self::$aclList->removePermission($permission);
193
	}
194 4
195 4
	/**
196 4
	 *
197
	 * @param string $resource
198
	 */
199
	public static function removeResource(string $resource) {
200
		self::$aclList->removeResource($resource);
201
	}
202
203
	/**
204
	 *
205 3
	 * @param string $role
206 3
	 * @param string $resource
207 3
	 * @param string $permission
208 3
	 */
209
	public static function removeAcl(string $role, string $resource, string $permission = null) {
210
		self::$aclList->removeAcl($role, $resource, $permission);
211
	}
212 3
213 3
	/**
214 3
	 * Initialize acls cache with controllers annotations.
215 3
	 * Do not execute at runtime
216 3
	 *
217 3
	 * @param array $config
218
	 * @throws \Ubiquity\exceptions\AclException
219 3
	 */
220
	public static function initCache(&$config) {
221
		CacheManager::start($config);
222
		CacheManager::registerAnnotations([
223
			'allow' => AllowAnnotation::class,
224
			'resource' => ResourceAnnotation::class,
225
			'permission' => PermissionAnnotation::class
226
		]);
227 3
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
228 3
		$parser = new AclControllerParser();
229
		$parser->init();
230
		foreach ($files as $file) {
231
			if (\is_file($file)) {
232
				$controller = ClassUtils::getClassFullNameFromFile($file);
233
				try {
234 1
					$parser->parse($controller);
235 1
				} catch (\Exception $e) {
236 1
					if ($e instanceof AclException) {
237 1
						throw $e;
238
					}
239 1
				}
240
			}
241
		}
242
		$parser->save();
243
	}
244
245
	/**
246
	 *
247
	 * @return \Ubiquity\security\acl\cache\PermissionsMap
248
	 */
249 1
	public static function getPermissionMap() {
250 1
		if (! isset(self::$permissionMap)) {
251 1
			self::$permissionMap = new PermissionsMap();
252 1
			self::$permissionMap->load();
253 1
		}
254
		return self::$permissionMap;
255
	}
256
257
	/**
258
	 *
259
	 * @param string $controller
260
	 * @param string $action
261 2
	 * @param string $resource
262 2
	 * @param string $permission
263
	 */
264
	public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL') {
265
		self::$aclList->getResourceByName($resource);
266
		self::$aclList->getPermissionByName($permission);
267
		self::$permissionMap->addAction($controller, $action, $resource, $permission);
268
	}
269
270
	/**
271 2
	 *
272 2
	 * @param AbstractAclPart $part
273
	 * @param string $providerClass
274
	 * @return boolean
275
	 */
276
	public static function existPartIn(AbstractAclPart $part, string $providerClass) {
277
		return self::$aclList->existPartIn($part, $providerClass);
278
	}
279
280 2
	/**
281 2
	 *
282
	 * @param AclElement $elm
283
	 * @param string $providerClass
284
	 * @return boolean
285
	 */
286
	public static function existAclIn(AclElement $elm, string $providerClass) {
287
		return self::$aclList->existAclIn($elm, $providerClass);
288
	}
289
290
	/**
291
	 *
292
	 * @param string $providerClass
293
	 * @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL
294
	 */
295
	public static function getProvider(string $providerClass) {
296
		return self::$aclList->getProvider($providerClass);
297
	}
298
}
299
300