Test Failed
Push — main ( d56afb...9584dd )
by Jean-Christophe
07:23
created

AclManager   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 61
c 1
b 0
f 1
dl 0
loc 254
ccs 90
cts 95
cp 0.9474
rs 9.44
wmc 37

28 Methods

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