Passed
Push — main ( 778532...d55b52 )
by Jean-Christophe
02:11
created

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