Passed
Push — main ( e591c1...2428da )
by Jean-Christophe
02:42
created

AclList::addResource()   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
7
/**
8
 * Ubiquity\security\acl\models$AclList
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.0
13
 *
14
 */
15
class AclList {
16
17
	/**
18
	 *
19
	 * @var AclElement[]
20
	 */
21
	protected $acls;
22
23
	/**
24
	 *
25
	 * @var Role[]
26
	 */
27
	protected $roles;
28
29
	/**
30
	 *
31
	 * @var \Ubiquity\security\acl\models\Resource[]
32
	 */
33
	protected $resources;
34
35
	/**
36
	 *
37
	 * @var Permission[]
38
	 */
39
	protected $permissions;
40
41
	/**
42
	 *
43
	 * @var AclLoaderInterface[]
44
	 */
45
	protected $providers;
46
47
	protected $elementsCache = [];
48
49 7
	protected function getElementByName(string $name, array $inArray, string $type) {
50 7
		foreach ($inArray as $elm) {
51 7
			if ($elm->getName() == $name) {
52 6
				return $elm;
53
			}
54
		}
55 2
		throw new AclException("$name does not exist in $type ACL");
56
	}
57
58
	protected function elementExistByName(string $name, array $inArray): bool {
59
		foreach ($inArray as $elm) {
60
			if ($elm->getName() == $name) {
61
				return true;
62
			}
63
		}
64
		return false;
65
	}
66
67 7
	public function init() {
68 7
		$this->roles[] = new Role('@ALL');
69 7
		$this->resources[] = new Resource('*');
70 7
		$this->permissions[] = new Permission('ALL', 1000);
71 7
		$this->acls = [];
72 7
	}
73
74 7
	public function getRoleByName(string $name) {
75 7
		return $this->elementsCache["role_$name"] ??= $this->getElementByName($name, $this->roles, 'roles');
76
	}
77
78 3
	public function getResourceByName(string $name) {
79 3
		return $this->elementsCache["res_$name"] ??= $this->getElementByName($name, $this->resources, 'resources');
80
	}
81
82 3
	public function getPermissionByName(string $name) {
83 3
		return $this->elementsCache["perm_$name"] ??= $this->getElementByName($name, $this->permissions, 'permissions');
84
	}
85
86 1
	public function loadAcls(): array {
87 1
		foreach ($this->providers as $provider) {
88
			$this->acls += $provider->loadAllAcls();
89
		}
90 1
		return $this->acls;
91
	}
92
93 1
	public function loadRoles(): array {
94 1
		foreach ($this->providers as $provider) {
95
			$this->roles += $provider->loadAllRoles();
96
		}
97 1
		return $this->roles;
98
	}
99
100 1
	public function loadResources(): array {
101 1
		foreach ($this->providers as $provider) {
102
			$this->resources += $provider->loadAllResources();
103
		}
104 1
		return $this->resources;
105
	}
106
107 1
	public function loadPermissions(): array {
108 1
		foreach ($this->providers as $provider) {
109
			$this->permissions += $provider->loadAllPermissions();
110
		}
111 1
		return $this->permissions;
112
	}
113
114
	public function addProvider(AclProviderInterface $provider) {
115
		$this->providers[] = $provider;
116
	}
117
118
	/**
119
	 *
120
	 * @return AclElement[]
121
	 */
122
	public function getAcls() {
123
		return $this->acls;
124
	}
125
126
	/**
127
	 *
128
	 * @return Role[]
129
	 */
130
	public function getRoles() {
131
		return $this->roles;
132
	}
133
134
	/**
135
	 *
136
	 * @return \Ubiquity\security\acl\models\Resource[]
137
	 */
138
	public function getResources() {
139
		return $this->resources;
140
	}
141
142
	/**
143
	 *
144
	 * @return Permission[]
145
	 */
146
	public function getPermissions() {
147
		return $this->permissions;
148
	}
149
150
	/**
151
	 *
152
	 * @return AclLoaderInterface[]
153
	 */
154
	public function getProviders() {
155
		return $this->providers;
156
	}
157
158
	/**
159
	 *
160
	 * @param AclLoaderInterface[] $providers
161
	 */
162 1
	public function setProviders($providers) {
163 1
		$this->providers = $providers;
164 1
	}
165
166 2
	public function addRole(Role $role) {
167 2
		$this->roles[$role->getName()] = $role;
168 2
	}
169
170 3
	public function addResource(Resource $resource) {
171 3
		$this->resources[$resource->getName()] = $resource;
172 3
	}
173
174 2
	public function addPermission(Permission $permission) {
175 2
		$this->permissions[$permission->getName()] = $permission;
176 2
	}
177
178 3
	public function allow(string $roleName, string $resourceName, string $permissionName) {
179 3
		$aclElm = new AclElement();
180 3
		$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
181 3
		$this->acls[] = $aclElm;
182 3
	}
183
184 7
	public function getRolePermissionsOn(string $roleName, $resourceName = '*'): array {
185 7
		$role = $this->getRoleByName($roleName);
186 6
		$parents = $role->getParents();
187 6
		$result = [];
188 6
		foreach ($this->acls as $aclElement) {
189 3
			$aclRoleName = $aclElement->getRole()->getName();
190 3
			if ($aclRoleName === '@ALL' || $aclRoleName === $roleName) {
191 3
				$aclResourceName = $aclElement->getResource()->getName();
192 3
				if ($aclResourceName === '*' || $aclResourceName === $resourceName) {
193 3
					$result[] = $aclElement;
194
				}
195
			}
196
		}
197 6
		foreach ($parents as $parentElm) {
198 1
			$result += $this->getRolePermissionsOn($parentElm, $resourceName);
199
		}
200 6
		return $result;
201
	}
202
203 7
	public function isAllowed(string $roleName, string $resourceName, string $permissionName) {
204 7
		$acls = $this->getRolePermissionsOn($roleName, $resourceName);
205 6
		if (\count($acls) > 0) {
206 3
			$permissionLevel = $this->getPermissionByName($permissionName)->getLevel();
207 2
			foreach ($acls as $aclElm) {
208 2
				$level = $aclElm->getPermission()->getLevel();
209 2
				if ($level >= $permissionLevel) {
210 2
					return true;
211
				}
212
			}
213
		}
214 5
		return false;
215
	}
216
}
217
218