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