@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * @param string $permission |
145 | 145 | * @param string[] $args |
146 | 146 | * @throws \Exception |
147 | - * @return void |
|
147 | + * @return boolean|null |
|
148 | 148 | */ |
149 | 149 | public function __call(string $permission, array $args) |
150 | 150 | { |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | /** |
193 | 193 | * Add a new role object to the registry |
194 | 194 | * |
195 | - * @param string $role |
|
195 | + * @param string[] $role |
|
196 | 196 | * @return void |
197 | 197 | */ |
198 | 198 | public function addRole(string ...$role) |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | /** |
207 | 207 | * Add a new resource object to the registry |
208 | 208 | * |
209 | - * @param string $resource |
|
209 | + * @param string[] $resource |
|
210 | 210 | * @return void |
211 | 211 | */ |
212 | 212 | public function addResource(string ...$resource) |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * Automatically determine the type of an object and call the appropriate |
238 | 238 | * add method on it. |
239 | 239 | * |
240 | - * @param ObjectInterface $objects |
|
240 | + * @param ObjectInterface[] $objects |
|
241 | 241 | * @throws \Exception |
242 | 242 | * @return void |
243 | 243 | */ |
@@ -9,20 +9,20 @@ discard block |
||
9 | 9 | namespace Samshal\Acl; |
10 | 10 | |
11 | 11 | use Samshal\Acl\Role\{ |
12 | - DefaultRole as Role, |
|
13 | - RoleInterface |
|
12 | + DefaultRole as Role, |
|
13 | + RoleInterface |
|
14 | 14 | }; |
15 | 15 | use Samshal\Acl\Resource\{ |
16 | - DefaultResource as Resource, |
|
17 | - ResourceInterface |
|
16 | + DefaultResource as Resource, |
|
17 | + ResourceInterface |
|
18 | 18 | }; |
19 | 19 | use Samshal\Acl\Permission\{ |
20 | - DefaultPermission as Permission, |
|
21 | - PermissionInterface |
|
20 | + DefaultPermission as Permission, |
|
21 | + PermissionInterface |
|
22 | 22 | }; |
23 | 23 | use Samshal\Acl\Registry\{ |
24 | - GlobalRegistry, |
|
25 | - Registry |
|
24 | + GlobalRegistry, |
|
25 | + Registry |
|
26 | 26 | }; |
27 | 27 | |
28 | 28 | |
@@ -35,96 +35,96 @@ discard block |
||
35 | 35 | */ |
36 | 36 | class Acl implements AclInterface |
37 | 37 | { |
38 | - /** |
|
39 | - * @var Registry\RegistryInterface $roleRegistry |
|
40 | - */ |
|
41 | - public $roleRegistry; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var Registry\RegistryInterface $resourceRegistry |
|
45 | - */ |
|
46 | - protected $resourceRegistry; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var Registry\RegistryInterface $permissionRegistry |
|
50 | - */ |
|
51 | - protected $permissionRegistry; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var Registry\RegistryInterface $globalRegistry |
|
55 | - */ |
|
56 | - public $globalRegistry; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var string[] $sesion |
|
60 | - */ |
|
61 | - protected $session = []; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var string SYN_ALLOW |
|
65 | - */ |
|
66 | - const SYN_ALLOW = "can"; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var string SYN_DENY |
|
70 | - */ |
|
71 | - const SYN_DENY = "cannot"; |
|
72 | - |
|
73 | - /** |
|
74 | - * Performs bootstrapping |
|
75 | - */ |
|
76 | - public function __construct() |
|
77 | - { |
|
78 | - self::initRegistries(); |
|
79 | - self::initSession(); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Initalizes the registries |
|
84 | - * |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - protected function initRegistries() |
|
88 | - { |
|
89 | - $this->roleRegistry = new Registry(); |
|
90 | - $this->resourceRegistry = new Registry(); |
|
91 | - $this->permissionRegistry = new Registry(); |
|
92 | - $this->globalRegistry = new GlobalRegistry(); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Initializes the global session array and sets them to the default value |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - protected function initSession() |
|
101 | - { |
|
102 | - $this->session["query"] = true; |
|
103 | - unset($this->session["role"], $this->session["status"]); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Listen for and intercept properties that're not set |
|
108 | - * |
|
109 | - * @param string $role; |
|
110 | - * @throws \Exception |
|
111 | - * @return AclInterface |
|
112 | - */ |
|
113 | - public function __get(string $role) : AclInterface |
|
114 | - { |
|
115 | - if ($role === self::SYN_ALLOW || $role === self::SYN_DENY) |
|
116 | - { |
|
117 | - $this->session["status"] = ($role === self::SYN_ALLOW); |
|
118 | - |
|
119 | - if (!empty($this->session["role"])) |
|
120 | - { |
|
121 | - $this->session["query"] = false; |
|
122 | - } |
|
123 | - |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - if (!$this->roleRegistry->exists($role)) { |
|
38 | + /** |
|
39 | + * @var Registry\RegistryInterface $roleRegistry |
|
40 | + */ |
|
41 | + public $roleRegistry; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var Registry\RegistryInterface $resourceRegistry |
|
45 | + */ |
|
46 | + protected $resourceRegistry; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var Registry\RegistryInterface $permissionRegistry |
|
50 | + */ |
|
51 | + protected $permissionRegistry; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var Registry\RegistryInterface $globalRegistry |
|
55 | + */ |
|
56 | + public $globalRegistry; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var string[] $sesion |
|
60 | + */ |
|
61 | + protected $session = []; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var string SYN_ALLOW |
|
65 | + */ |
|
66 | + const SYN_ALLOW = "can"; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var string SYN_DENY |
|
70 | + */ |
|
71 | + const SYN_DENY = "cannot"; |
|
72 | + |
|
73 | + /** |
|
74 | + * Performs bootstrapping |
|
75 | + */ |
|
76 | + public function __construct() |
|
77 | + { |
|
78 | + self::initRegistries(); |
|
79 | + self::initSession(); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Initalizes the registries |
|
84 | + * |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + protected function initRegistries() |
|
88 | + { |
|
89 | + $this->roleRegistry = new Registry(); |
|
90 | + $this->resourceRegistry = new Registry(); |
|
91 | + $this->permissionRegistry = new Registry(); |
|
92 | + $this->globalRegistry = new GlobalRegistry(); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Initializes the global session array and sets them to the default value |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + protected function initSession() |
|
101 | + { |
|
102 | + $this->session["query"] = true; |
|
103 | + unset($this->session["role"], $this->session["status"]); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Listen for and intercept properties that're not set |
|
108 | + * |
|
109 | + * @param string $role; |
|
110 | + * @throws \Exception |
|
111 | + * @return AclInterface |
|
112 | + */ |
|
113 | + public function __get(string $role) : AclInterface |
|
114 | + { |
|
115 | + if ($role === self::SYN_ALLOW || $role === self::SYN_DENY) |
|
116 | + { |
|
117 | + $this->session["status"] = ($role === self::SYN_ALLOW); |
|
118 | + |
|
119 | + if (!empty($this->session["role"])) |
|
120 | + { |
|
121 | + $this->session["query"] = false; |
|
122 | + } |
|
123 | + |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + if (!$this->roleRegistry->exists($role)) { |
|
128 | 128 | throw new \Exception( |
129 | 129 | sprintf( |
130 | 130 | "The role: %s doesnt exist", |
@@ -133,22 +133,22 @@ discard block |
||
133 | 133 | ); |
134 | 134 | } |
135 | 135 | |
136 | - $this->session["role"] = $role; |
|
137 | - |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Listen for and intercept undefined methods |
|
143 | - * |
|
144 | - * @param string $permission |
|
145 | - * @param string[] $args |
|
146 | - * @throws \Exception |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - public function __call(string $permission, array $args) |
|
150 | - { |
|
151 | - if (!$this->permissionRegistry->exists($permission)) { |
|
136 | + $this->session["role"] = $role; |
|
137 | + |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Listen for and intercept undefined methods |
|
143 | + * |
|
144 | + * @param string $permission |
|
145 | + * @param string[] $args |
|
146 | + * @throws \Exception |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + public function __call(string $permission, array $args) |
|
150 | + { |
|
151 | + if (!$this->permissionRegistry->exists($permission)) { |
|
152 | 152 | throw new \Exception( |
153 | 153 | sprintf( |
154 | 154 | "The permission: %s doesnt exist", |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | ); |
158 | 158 | } |
159 | 159 | |
160 | - if (!$this->resourceRegistry->exists($args[0])) { |
|
160 | + if (!$this->resourceRegistry->exists($args[0])) { |
|
161 | 161 | throw new \Exception( |
162 | 162 | sprintf( |
163 | 163 | "The resource: %s doesnt exist", |
@@ -166,122 +166,122 @@ discard block |
||
166 | 166 | ); |
167 | 167 | } |
168 | 168 | |
169 | - if ($this->session["query"]) |
|
170 | - { |
|
171 | - $result = $this->getPermissionStatus( |
|
169 | + if ($this->session["query"]) |
|
170 | + { |
|
171 | + $result = $this->getPermissionStatus( |
|
172 | 172 | $this->session["role"], |
173 | 173 | $permission, |
174 | 174 | $args[0] |
175 | 175 | ); |
176 | 176 | |
177 | - $this->initSession(); |
|
177 | + $this->initSession(); |
|
178 | 178 | |
179 | - return $result; |
|
180 | - } |
|
179 | + return $result; |
|
180 | + } |
|
181 | 181 | |
182 | - $this->allow( |
|
182 | + $this->allow( |
|
183 | 183 | $this->session["role"], |
184 | 184 | $permission, |
185 | 185 | $args[0], |
186 | 186 | (boolean)$this->session["status"] |
187 | 187 | ); |
188 | 188 | |
189 | - $this->initSession(); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Add a new role object to the registry |
|
194 | - * |
|
195 | - * @param string $role |
|
196 | - * @return void |
|
197 | - */ |
|
198 | - public function addRole(string ...$role) |
|
199 | - { |
|
200 | - foreach ($role as $_role) |
|
201 | - { |
|
202 | - $this->roleRegistry->save($_role); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Add a new resource object to the registry |
|
208 | - * |
|
209 | - * @param string $resource |
|
210 | - * @return void |
|
211 | - */ |
|
212 | - public function addResource(string ...$resource) |
|
213 | - { |
|
214 | - foreach ($resource as $_resource) |
|
215 | - { |
|
216 | - $this->resourceRegistry->save($resource); |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Add a new permission object to the registry |
|
222 | - * |
|
223 | - * @param string $permission |
|
224 | - * @return void |
|
225 | - */ |
|
226 | - public function addPermission(string $permission) |
|
227 | - { |
|
228 | - foreach ($permission as $_permission) |
|
229 | - { |
|
230 | - $this->permissionRegistry->save($_permission); |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Adds objects lazily. |
|
236 | - * |
|
237 | - * Automatically determine the type of an object and call the appropriate |
|
238 | - * add method on it. |
|
239 | - * |
|
240 | - * @param ObjectInterface $objects |
|
241 | - * @throws \Exception |
|
242 | - * @return void |
|
243 | - */ |
|
244 | - public function add(ObjectInterface ...$objects) |
|
245 | - { |
|
246 | - foreach ($objects as $object) |
|
247 | - { |
|
248 | - if ($object instanceof RoleInterface) |
|
249 | - { |
|
250 | - $this->addRole((string)$object); |
|
251 | - } |
|
252 | - else if ($object instanceof ResourceInterface) |
|
253 | - { |
|
254 | - $this->addResource((string)$object); |
|
255 | - } |
|
256 | - else if ($object instanceof PermissionInterface) |
|
257 | - { |
|
258 | - $this->addPermission((string)$object); |
|
259 | - } |
|
260 | - else { |
|
261 | - throw new \Exception( |
|
262 | - sprintf( |
|
263 | - "%s must implement one of RoleInterface, '. |
|
189 | + $this->initSession(); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Add a new role object to the registry |
|
194 | + * |
|
195 | + * @param string $role |
|
196 | + * @return void |
|
197 | + */ |
|
198 | + public function addRole(string ...$role) |
|
199 | + { |
|
200 | + foreach ($role as $_role) |
|
201 | + { |
|
202 | + $this->roleRegistry->save($_role); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Add a new resource object to the registry |
|
208 | + * |
|
209 | + * @param string $resource |
|
210 | + * @return void |
|
211 | + */ |
|
212 | + public function addResource(string ...$resource) |
|
213 | + { |
|
214 | + foreach ($resource as $_resource) |
|
215 | + { |
|
216 | + $this->resourceRegistry->save($resource); |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Add a new permission object to the registry |
|
222 | + * |
|
223 | + * @param string $permission |
|
224 | + * @return void |
|
225 | + */ |
|
226 | + public function addPermission(string $permission) |
|
227 | + { |
|
228 | + foreach ($permission as $_permission) |
|
229 | + { |
|
230 | + $this->permissionRegistry->save($_permission); |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Adds objects lazily. |
|
236 | + * |
|
237 | + * Automatically determine the type of an object and call the appropriate |
|
238 | + * add method on it. |
|
239 | + * |
|
240 | + * @param ObjectInterface $objects |
|
241 | + * @throws \Exception |
|
242 | + * @return void |
|
243 | + */ |
|
244 | + public function add(ObjectInterface ...$objects) |
|
245 | + { |
|
246 | + foreach ($objects as $object) |
|
247 | + { |
|
248 | + if ($object instanceof RoleInterface) |
|
249 | + { |
|
250 | + $this->addRole((string)$object); |
|
251 | + } |
|
252 | + else if ($object instanceof ResourceInterface) |
|
253 | + { |
|
254 | + $this->addResource((string)$object); |
|
255 | + } |
|
256 | + else if ($object instanceof PermissionInterface) |
|
257 | + { |
|
258 | + $this->addPermission((string)$object); |
|
259 | + } |
|
260 | + else { |
|
261 | + throw new \Exception( |
|
262 | + sprintf( |
|
263 | + "%s must implement one of RoleInterface, '. |
|
264 | 264 | 'ResourceInterface and PermissionInterface", |
265 | - $object |
|
266 | - ) |
|
267 | - ); |
|
268 | - } |
|
269 | - } |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Change the status option of an assigned permission to true |
|
274 | - * |
|
275 | - * @param string $role; |
|
276 | - * @param string $permission |
|
277 | - * @param string $resource |
|
278 | - * @param boolean $status Optional |
|
279 | - * @throws \Exception |
|
280 | - * @return void |
|
281 | - */ |
|
282 | - public function allow(string $role, string $permission, string $resource, boolean $status=true) |
|
283 | - { |
|
284 | - if (!$this->roleRegistry->exists($role)) { |
|
265 | + $object |
|
266 | + ) |
|
267 | + ); |
|
268 | + } |
|
269 | + } |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Change the status option of an assigned permission to true |
|
274 | + * |
|
275 | + * @param string $role; |
|
276 | + * @param string $permission |
|
277 | + * @param string $resource |
|
278 | + * @param boolean $status Optional |
|
279 | + * @throws \Exception |
|
280 | + * @return void |
|
281 | + */ |
|
282 | + public function allow(string $role, string $permission, string $resource, boolean $status=true) |
|
283 | + { |
|
284 | + if (!$this->roleRegistry->exists($role)) { |
|
285 | 285 | throw new \Exception( |
286 | 286 | sprintf( |
287 | 287 | "The role: %s doesnt exist", |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | ); |
291 | 291 | } |
292 | 292 | |
293 | - if (!$this->permissionRegistry->exists($permission)) { |
|
293 | + if (!$this->permissionRegistry->exists($permission)) { |
|
294 | 294 | throw new \Exception( |
295 | 295 | sprintf( |
296 | 296 | "The permission: %s doesnt exist", |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | ); |
300 | 300 | } |
301 | 301 | |
302 | - if (!$this->resourceRegistry->exists($resource)) { |
|
302 | + if (!$this->resourceRegistry->exists($resource)) { |
|
303 | 303 | throw new \Exception( |
304 | 304 | sprintf( |
305 | 305 | "The resource: %s doesnt exist", |
@@ -308,33 +308,33 @@ discard block |
||
308 | 308 | ); |
309 | 309 | } |
310 | 310 | |
311 | - $this->globalRegistry->save($role, $resource, $permission, $status); |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Change the status option of an assigned permission to false |
|
316 | - * |
|
317 | - * @param string $role; |
|
318 | - * @param string $permission |
|
319 | - * @param string $resource |
|
320 | - * @return void |
|
321 | - */ |
|
322 | - public function deny(string $role, string $permission, string $resource) |
|
323 | - { |
|
324 | - $this->allow($role, $permission, $resource, false); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Retrieve the status of a permission assigned to a role |
|
329 | - * |
|
330 | - * @param string $role; |
|
331 | - * @param string $permission |
|
332 | - * @param string $resource |
|
333 | - * @return boolean |
|
334 | - */ |
|
335 | - public function getPermissionStatus(string $role, string $permission, string $resource) : bool |
|
336 | - { |
|
337 | - if (!$this->roleRegistry->exists($role)) { |
|
311 | + $this->globalRegistry->save($role, $resource, $permission, $status); |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Change the status option of an assigned permission to false |
|
316 | + * |
|
317 | + * @param string $role; |
|
318 | + * @param string $permission |
|
319 | + * @param string $resource |
|
320 | + * @return void |
|
321 | + */ |
|
322 | + public function deny(string $role, string $permission, string $resource) |
|
323 | + { |
|
324 | + $this->allow($role, $permission, $resource, false); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Retrieve the status of a permission assigned to a role |
|
329 | + * |
|
330 | + * @param string $role; |
|
331 | + * @param string $permission |
|
332 | + * @param string $resource |
|
333 | + * @return boolean |
|
334 | + */ |
|
335 | + public function getPermissionStatus(string $role, string $permission, string $resource) : bool |
|
336 | + { |
|
337 | + if (!$this->roleRegistry->exists($role)) { |
|
338 | 338 | throw new \Exception( |
339 | 339 | sprintf( |
340 | 340 | "The role: %s doesnt exist", |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | ); |
344 | 344 | } |
345 | 345 | |
346 | - if (!$this->permissionRegistry->exists($permission)) { |
|
346 | + if (!$this->permissionRegistry->exists($permission)) { |
|
347 | 347 | throw new \Exception( |
348 | 348 | sprintf( |
349 | 349 | "The permission: %s doesnt exist", |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | ); |
353 | 353 | } |
354 | 354 | |
355 | - if (!$this->resourceRegistry->exists($resource)) { |
|
355 | + if (!$this->resourceRegistry->exists($resource)) { |
|
356 | 356 | throw new \Exception( |
357 | 357 | sprintf( |
358 | 358 | "The resource: %s doesnt exist", |
@@ -362,8 +362,8 @@ discard block |
||
362 | 362 | } |
363 | 363 | |
364 | 364 | |
365 | - $role = $this->globalRegistry->get($role); |
|
365 | + $role = $this->globalRegistry->get($role); |
|
366 | 366 | |
367 | - return $role[$resource][$permission]["status"]; |
|
368 | - } |
|
367 | + return $role[$resource][$permission]["status"]; |
|
368 | + } |
|
369 | 369 | } |