Test Failed
Push — main ( 9d2fd2...0a49ac )
by Jean-Christophe
02:29
created

AclList::loadPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3.1852

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 5
ccs 1
cts 3
cp 0.3333
crap 3.1852
rs 10
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 3
		return false;
68
	}
69
70 22
	public function __construct() {
71 22
		$this->providers = [];
72 22
	}
73
74 22
	public function init() {
75 22
		$this->roles = [
76
			'role_@ALL' => new Role('@ALL')
77 22
		];
78 22
		$this->resources = [
79
			'res_*' => new Resource('*')
80 22
		];
81 22
		$this->permissions = [
82 22
			'perm_ALL' => new Permission('ALL', 1000)
83
		];
84 21
		$this->elementsCache = [];
85 21
		$this->acls = [];
86
	}
87
88 13
	public function getRoleByName(string $name) {
89 13
		return $this->elementsCache["role_$name"] ??= $this->getElementByName($name, $this->roles, 'roles');
90
	}
91
92 17
	public function getResourceByName(string $name) {
93 17
		return $this->elementsCache["res_$name"] ??= $this->getElementByName($name, $this->resources, 'resources');
94
	}
95
96 10
	public function getPermissionByName(string $name) {
97 10
		return $this->elementsCache["perm_$name"] ??= $this->getElementByName($name, $this->permissions, 'permissions');
98 10
	}
99
100 10
	public function loadAcls(): array {
101
		foreach ($this->providers as $provider) {
102
			$this->acls += $provider->loadAllAcls();
103 10
		}
104 10
		return $this->acls;
105 10
	}
106
107 10
	public function loadRoles(): array {
108
		foreach ($this->providers as $provider) {
109
			$this->roles += $provider->loadAllRoles();
110 10
		}
111 10
		return $this->roles;
112 10
	}
113
114 10
	public function loadResources(): array {
115
		foreach ($this->providers as $provider) {
116
			$this->resources += $provider->loadAllResources();
117 10
		}
118 10
		return $this->resources;
119 10
	}
120
121 10
	public function loadPermissions(): array {
122
		foreach ($this->providers as $provider) {
123
			$this->permissions += $provider->loadAllPermissions();
124
		}
125
		return $this->permissions;
126
	}
127
128
	public function addProvider(AclProviderInterface $provider) {
129
		$this->providers[] = $provider;
130
	}
131
132 6
	/**
133 6
	 *
134
	 * @return AclElement[]
135
	 */
136
	public function getAcls() {
137
		return $this->acls;
138
	}
139
140 8
	/**
141 8
	 *
142
	 * @return Role[]
143
	 */
144
	public function getRoles() {
145
		return $this->roles;
146
	}
147
148 7
	/**
149 7
	 *
150
	 * @return \Ubiquity\security\acl\models\Resource[]
151
	 */
152
	public function getResources() {
153
		return $this->resources;
154
	}
155
156 11
	/**
157 11
	 *
158
	 * @return Permission[]
159
	 */
160
	public function getPermissions() {
161
		return $this->permissions;
162
	}
163
164 1
	/**
165 1
	 *
166
	 * @return AclProviderInterface[]
167
	 */
168
	public function getProviders() {
169
		return $this->providers;
170
	}
171
172 11
	/**
173 11
	 *
174 11
	 * @param AclProviderInterface[] $providers
175
	 */
176 20
	public function setProviders($providers) {
177 20
		$this->providers = $providers;
178 19
	}
179 19
180 19
	public function getRolePermissionsOn(string $roleName, $resourceName = '*'): array {
181 16
		$role = $this->getRoleByName($roleName);
182 16
		$parents = $role->getParentsArray();
183 16
		$result = [];
184 16
		foreach ($this->acls as $aclElement) {
185 16
			$aclRoleName = $aclElement->getRole()->getName();
186
			if ($aclRoleName === '@ALL' || $aclRoleName === $roleName) {
187
				$aclResourceName = $aclElement->getResource()->getName();
188
				if ($aclResourceName === '*' || $aclResourceName === $resourceName) {
189 19
					$result[] = $aclElement;
190 4
				}
191
			}
192 19
		}
193
		foreach ($parents as $parentElm) {
194
			$result += $this->getRolePermissionsOn($parentElm, $resourceName);
195 20
		}
196 20
		return $result;
197 19
	}
198 16
199 15
	public function isAllowed(string $roleName, string $resourceName, string $permissionName) {
200 15
		$acls = $this->getRolePermissionsOn($roleName, $resourceName);
201 15
		if (\count($acls) > 0) {
202 15
			$permissionLevel = $this->getPermissionByName($permissionName)->getLevel();
203
			foreach ($acls as $aclElm) {
204
				$level = $aclElm->getPermission()->getLevel();
205
				if ($level >= $permissionLevel) {
206 14
					return true;
207
				}
208
			}
209
		}
210
		return false;
211
	}
212
213
	/**
214 2
	 *
215 2
	 * @param string $providerClass
216 2
	 * @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL
217 2
	 */
218
	public function getProvider(string $providerClass) {
219
		foreach ($this->providers as $prov) {
220
			if ($prov instanceof $providerClass) {
221
				return $prov;
222
			}
223
		}
224
		return null;
225
	}
226
227
	/**
228
	 *
229
	 * @param string $id_
230
	 * @return ?AclElement
231
	 */
232
	public function getAclById_(string $id_): ?AclElement {
233
		foreach ($this->acls as $acl) {
234
			if ($acl->getId_() === $id_) {
235
				return $acl;
236
			}
237 1
		}
238 1
		return null;
239 1
	}
240 1
241
	public function getProviderClasses() {
242 1
		$result = [];
243
		foreach ($this->providers as $prov) {
244
			$result[] = \get_class($prov);
245
		}
246
		return $result;
247
	}
248
}
249
250