|
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
|
8 |
|
protected function getElementByName(string $name, array $inArray, string $type) { |
|
50
|
8 |
|
foreach ($inArray as $elm) { |
|
51
|
8 |
|
if ($elm->getName() == $name) { |
|
52
|
7 |
|
return $elm; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
3 |
|
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
|
8 |
|
public function init() { |
|
68
|
8 |
|
$this->roles[] = new Role('@ALL'); |
|
69
|
8 |
|
$this->resources[] = new Resource('*'); |
|
70
|
8 |
|
$this->permissions[] = new Permission('ALL', 1000); |
|
71
|
8 |
|
$this->acls = []; |
|
72
|
8 |
|
} |
|
73
|
|
|
|
|
74
|
8 |
|
public function getRoleByName(string $name) { |
|
75
|
8 |
|
return $this->elementsCache["role_$name"] ??= $this->getElementByName($name, $this->roles, 'roles'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
public function getResourceByName(string $name) { |
|
79
|
4 |
|
return $this->elementsCache["res_$name"] ??= $this->getElementByName($name, $this->resources, 'resources'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
public function getPermissionByName(string $name) { |
|
83
|
4 |
|
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
|
3 |
|
public function addRole(Role $role) { |
|
167
|
3 |
|
$this->roles[$role->getName()] = $role; |
|
168
|
3 |
|
} |
|
169
|
|
|
|
|
170
|
4 |
|
public function addResource(Resource $resource) { |
|
171
|
4 |
|
$this->resources[$resource->getName()] = $resource; |
|
172
|
4 |
|
} |
|
173
|
|
|
|
|
174
|
3 |
|
public function addPermission(Permission $permission) { |
|
175
|
3 |
|
$this->permissions[$permission->getName()] = $permission; |
|
176
|
3 |
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
public function setPermissionLevel(string $name, int $level) { |
|
179
|
1 |
|
$perm = $this->getPermissionByName($name); |
|
180
|
|
|
$perm->setLevel($level); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
4 |
|
public function allow(string $roleName, string $resourceName, string $permissionName) { |
|
184
|
4 |
|
$aclElm = new AclElement(); |
|
185
|
4 |
|
$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName)); |
|
186
|
4 |
|
$this->acls[] = $aclElm; |
|
187
|
4 |
|
} |
|
188
|
|
|
|
|
189
|
8 |
|
public function getRolePermissionsOn(string $roleName, $resourceName = '*'): array { |
|
190
|
8 |
|
$role = $this->getRoleByName($roleName); |
|
191
|
7 |
|
$parents = $role->getParents(); |
|
192
|
7 |
|
$result = []; |
|
193
|
7 |
|
foreach ($this->acls as $aclElement) { |
|
194
|
4 |
|
$aclRoleName = $aclElement->getRole()->getName(); |
|
195
|
4 |
|
if ($aclRoleName === '@ALL' || $aclRoleName === $roleName) { |
|
196
|
4 |
|
$aclResourceName = $aclElement->getResource()->getName(); |
|
197
|
4 |
|
if ($aclResourceName === '*' || $aclResourceName === $resourceName) { |
|
198
|
4 |
|
$result[] = $aclElement; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
7 |
|
foreach ($parents as $parentElm) { |
|
203
|
1 |
|
$result += $this->getRolePermissionsOn($parentElm, $resourceName); |
|
204
|
|
|
} |
|
205
|
7 |
|
return $result; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
8 |
|
public function isAllowed(string $roleName, string $resourceName, string $permissionName) { |
|
209
|
8 |
|
$acls = $this->getRolePermissionsOn($roleName, $resourceName); |
|
210
|
7 |
|
if (\count($acls) > 0) { |
|
211
|
4 |
|
$permissionLevel = $this->getPermissionByName($permissionName)->getLevel(); |
|
212
|
3 |
|
foreach ($acls as $aclElm) { |
|
213
|
3 |
|
$level = $aclElm->getPermission()->getLevel(); |
|
214
|
3 |
|
if ($level >= $permissionLevel) { |
|
215
|
3 |
|
return true; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
6 |
|
return false; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
|