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

AclList::getAclById_()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 10
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
namespace Ubiquity\security\acl\models;
3
4
use Ubiquity\security\acl\persistence\AclProviderInterface;
5
use Ubiquity\exceptions\AclException;
6
use Ubiquity\security\acl\models\traits\AclListOperationsTrait;
7
use Ubiquity\security\acl\models\traits\AclListQueryTrait;
8
9
/**
10
 * Ubiquity\security\acl\models$AclList
11
 * This class is part of Ubiquity
12
 *
13
 * @author jc
14
 * @version 1.0.0
15
 *
16
 */
17
class AclList {
18
	use AclListOperationsTrait,AclListQueryTrait;
19
20
	/**
21
	 *
22
	 * @var AclElement[]
23
	 */
24
	protected $acls;
25
26
	/**
27
	 *
28
	 * @var Role[]
29
	 */
30
	protected $roles;
31
32
	/**
33
	 *
34
	 * @var \Ubiquity\security\acl\models\Resource[]
35
	 */
36
	protected $resources;
37
38
	/**
39
	 *
40
	 * @var Permission[]
41
	 */
42
	protected $permissions;
43
44
	/**
45
	 *
46
	 * @var AclProviderInterface[]
47
	 */
48
	protected $providers = [];
49
50
	protected $elementsCache = [];
51 21
52 21
	protected function getElementByName(string $name, array $inArray, string $type) {
53 21
		foreach ($inArray as $elm) {
54 20
			if ($elm->getName() == $name) {
55
				return $elm;
56
			}
57 7
		}
58
		throw new AclException("$name does not exist in $type ACL");
59
	}
60 4
61 4
	protected function elementExistByName(string $name, array $inArray): bool {
62 4
		foreach ($inArray as $elm) {
63 3
			if ($elm->getName() == $name) {
64
				return true;
65
			}
66 4
		}
67
		return false;
68
	}
69 21
70 21
	public function init() {
71 21
		$this->roles['role_@ALL'] = new Role('@ALL');
72 21
		$this->resources['res_*'] = new Resource('*');
73 21
		$this->permissions['perm_ALL'] = new Permission('ALL', 1000);
74 21
		$this->acls = [];
75
	}
76 21
77 21
	public function getRoleByName(string $name) {
78
		return $this->elementsCache["role_$name"] ??= $this->getElementByName($name, $this->roles, 'roles');
79
	}
80 13
81 13
	public function getResourceByName(string $name) {
82
		return $this->elementsCache["res_$name"] ??= $this->getElementByName($name, $this->resources, 'resources');
83
	}
84 17
85 17
	public function getPermissionByName(string $name) {
86
		return $this->elementsCache["perm_$name"] ??= $this->getElementByName($name, $this->permissions, 'permissions');
87
	}
88 9
89 9
	public function loadAcls(): array {
90 9
		foreach ($this->providers as $provider) {
91
			$this->acls += $provider->loadAllAcls();
92 9
		}
93
		return $this->acls;
94
	}
95 9
96 9
	public function loadRoles(): array {
97 9
		foreach ($this->providers as $provider) {
98
			$this->roles += $provider->loadAllRoles();
99 9
		}
100
		return $this->roles;
101
	}
102 9
103 9
	public function loadResources(): array {
104 9
		foreach ($this->providers as $provider) {
105
			$this->resources += $provider->loadAllResources();
106 9
		}
107
		return $this->resources;
108
	}
109 9
110 9
	public function loadPermissions(): array {
111 9
		foreach ($this->providers as $provider) {
112
			$this->permissions += $provider->loadAllPermissions();
113 9
		}
114
		return $this->permissions;
115
	}
116
117
	public function addProvider(AclProviderInterface $provider) {
118
		$this->providers[] = $provider;
119
	}
120
121
	/**
122
	 *
123
	 * @return AclElement[]
124 6
	 */
125 6
	public function getAcls() {
126
		return $this->acls;
127
	}
128
129
	/**
130
	 *
131
	 * @return Role[]
132 9
	 */
133 9
	public function getRoles() {
134
		return $this->roles;
135
	}
136
137
	/**
138
	 *
139
	 * @return \Ubiquity\security\acl\models\Resource[]
140 8
	 */
141 8
	public function getResources() {
142
		return $this->resources;
143
	}
144
145
	/**
146
	 *
147
	 * @return Permission[]
148 12
	 */
149 12
	public function getPermissions() {
150
		return $this->permissions;
151
	}
152
153
	/**
154
	 *
155
	 * @return AclProviderInterface[]
156
	 */
157
	public function getProviders() {
158
		return $this->providers;
159
	}
160
161
	/**
162
	 *
163
	 * @param AclProviderInterface[] $providers
164 10
	 */
165 10
	public function setProviders($providers) {
166 10
		$this->providers = $providers;
167
	}
168 11
169 11
	public function clear() {
170 11
		$this->roles = [];
171 11
		$this->resources = [];
172
		$this->permissions = [];
173 10
		$this->elementsCache = [];
174 10
		$this->init();
175 10
	}
176 10
177
	public function addRole(Role $role) {
178 12
		$this->roles[$role->getName()] = $role;
179 12
		$this->savePart($role);
180 12
	}
181 12
182
	public function addResource(Resource $resource) {
183 3
		$this->resources[$resource->getName()] = $resource;
184 3
		$this->savePart($resource);
185 2
	}
186 2
187 2
	public function addPermission(Permission $permission) {
188
		$this->permissions[$permission->getName()] = $permission;
189 13
		$this->savePart($permission);
190 13
	}
191 13
192 13
	public function setPermissionLevel(string $name, int $level) {
193 13
		$perm = $this->getPermissionByName($name);
194 13
		$perm->setLevel($level);
195
		$this->updatePart($perm);
196 4
	}
197 4
198 4
	public function allow(string $roleName, string $resourceName, string $permissionName) {
199
		$aclElm = new AclElement();
200 4
		$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
201 4
		$this->acls[] = $aclElm;
202
		$this->saveAclElement($aclElm);
203 4
	}
204 4
205
	public function addAndAllow(string $roleName, string $resourceName, string $permissionName) {
206 4
		if (! $this->elementExistByName($roleName, $this->roles)) {
207 4
			$this->addRole(new Role($roleName));
208
		}
209 20
		if ($resourceName !== '*' && ! $this->elementExistByName($resourceName, $this->resources)) {
210 20
			$this->addResource(new Resource($resourceName));
211 19
		}
212 19
		if ($permissionName !== 'ALL' && ! $this->elementExistByName($permissionName, $this->permissions)) {
213 19
			$this->addPermission(new Permission($permissionName));
214 16
		}
215 16
		$this->allow($roleName, $resourceName ?? '*', $permissionName ?? 'ALL');
216 16
	}
217 16
218 16
	public function getRolePermissionsOn(string $roleName, $resourceName = '*'): array {
219
		$role = $this->getRoleByName($roleName);
220
		$parents = $role->getParentsArray();
221
		$result = [];
222 19
		foreach ($this->acls as $aclElement) {
223 4
			$aclRoleName = $aclElement->getRole()->getName();
224
			if ($aclRoleName === '@ALL' || $aclRoleName === $roleName) {
225 19
				$aclResourceName = $aclElement->getResource()->getName();
226
				if ($aclResourceName === '*' || $aclResourceName === $resourceName) {
227
					$result[] = $aclElement;
228 20
				}
229 20
			}
230 19
		}
231 16
		foreach ($parents as $parentElm) {
232 15
			$result += $this->getRolePermissionsOn($parentElm, $resourceName);
233 15
		}
234 15
		return $result;
235 15
	}
236
237
	public function isAllowed(string $roleName, string $resourceName, string $permissionName) {
238
		$acls = $this->getRolePermissionsOn($roleName, $resourceName);
239 14
		if (\count($acls) > 0) {
240
			$permissionLevel = $this->getPermissionByName($permissionName)->getLevel();
241
			foreach ($acls as $aclElm) {
242
				$level = $aclElm->getPermission()->getLevel();
243
				if ($level >= $permissionLevel) {
244
					return true;
245
				}
246
			}
247 2
		}
248 2
		return false;
249 2
	}
250 2
251
	/**
252
	 *
253
	 * @param string $providerClass
254
	 * @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL
255
	 */
256
	public function getProvider(string $providerClass) {
257
		foreach ($this->providers as $prov) {
258
			if ($prov instanceof $providerClass) {
259
				return $prov;
260
			}
261
		}
262 2
		return null;
263 2
	}
264 2
265 2
	/**
266
	 *
267
	 * @param AbstractAclPart $part
268
	 * @param string $providerClass
269
	 * @return boolean
270
	 */
271
	public function existPartIn(AbstractAclPart $part, string $providerClass) {
272
		$prov = $this->getProvider($providerClass);
273
		if (isset($prov)) {
274
			return $prov->existPart($part);
275
		}
276 2
		return false;
277 2
	}
278 2
279 2
	/**
280
	 *
281
	 * @param AclElement $elm
282
	 * @param string $providerClass
283
	 * @return boolean
284
	 */
285
	public function existAclIn(AclElement $elm, string $providerClass) {
286
		$prov = $this->getProvider($providerClass);
287
		if (isset($prov)) {
288
			return $prov->existAcl($elm);
289
		}
290
		return false;
291
	}
292
293
	/**
294
	 *
295
	 * @param string $id_
296
	 * @return ?AclElement
297
	 */
298
	public function getAclById_(string $id_): ?AclElement {
299
		foreach ($this->acls as $acl) {
300
			if ($acl->getId_() === $id_) {
301
				return $acl;
302
			}
303
		}
304
		return null;
305
	}
306
307
	public function getProviderClasses() {
308
		$result = [];
309
		foreach ($this->providers as $prov) {
310
			$result[] = \get_class($prov);
311
		}
312
		return $result;
313
	}
314
}
315
316