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