1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Acl; |
13
|
|
|
|
14
|
|
|
use Phalcon\Di\AbstractInjectionAware; |
15
|
|
|
use Phalcon\Acl\Adapter\Memory; |
16
|
|
|
use Phalcon\Acl\Component; |
17
|
|
|
use Phalcon\Acl\Role; |
18
|
|
|
use Zemit\Support\Options\Options; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Acl |
22
|
|
|
* |
23
|
|
|
* This class represents an Access Control List (ACL) and is used |
24
|
|
|
* to configure and manage access permissions for different components. |
25
|
|
|
*/ |
26
|
|
|
class Acl extends AbstractInjectionAware |
27
|
|
|
{ |
28
|
|
|
use Options; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Return an ACL for the specified components name |
32
|
|
|
* @param array $componentsName |
33
|
|
|
* @param array|null $permissions |
34
|
|
|
* @param string $inherit |
35
|
|
|
* @return Memory |
36
|
|
|
* @todo cache the ACL |
37
|
|
|
*/ |
38
|
|
|
public function get(array $componentsName = ['components'], ?array $permissions = null, string $inherit = 'inherit'): Memory |
39
|
|
|
{ |
40
|
|
|
$acl = new Memory(); |
41
|
|
|
$aclRoleList = []; |
42
|
|
|
|
43
|
|
|
$permissions = $this->getOption('permissions', []); |
44
|
|
|
$featureList = $permissions['features'] ?? []; |
45
|
|
|
$roleList = $permissions['roles'] ?? []; |
46
|
|
|
|
47
|
|
|
foreach ($roleList as $role => $rolePermission) { |
48
|
|
|
|
49
|
|
|
$role = $role === '*' ? 'everyone' : $role; |
50
|
|
|
$aclRole = new Role($role); |
51
|
|
|
$aclRoleList[$role] = $aclRole; |
52
|
|
|
$acl->addRole($aclRole); |
53
|
|
|
|
54
|
|
|
if (isset($rolePermission['features'])) { |
55
|
|
|
foreach ($rolePermission['features'] as $feature) { |
56
|
|
|
$rolePermission = array_merge_recursive($rolePermission, $featureList[$feature] ?? []); |
57
|
|
|
// @todo remove duplicates |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($componentsName as $componentName) { |
62
|
|
|
$components = $rolePermission[$componentName] ?? []; |
63
|
|
|
$components = is_array($components) ? $components : [$components]; |
64
|
|
|
|
65
|
|
|
foreach ($components as $component => $accessList) { |
66
|
|
|
if (empty($component)) { |
67
|
|
|
$component = $accessList; |
68
|
|
|
$accessList = '*'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($component !== '*') { |
72
|
|
|
$aclComponent = new Component($component); |
73
|
|
|
$acl->addComponent($aclComponent, $accessList); |
74
|
|
|
$acl->allow($aclRole, $aclComponent, $accessList); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add inheritance (role extends) |
82
|
|
|
*/ |
83
|
|
|
foreach ($aclRoleList as $role => $aclRole) { |
84
|
|
|
$inheritList = $permissions[$role][$inherit] ?? []; |
85
|
|
|
$inheritList = is_array($inheritList) ? $inheritList : [$inheritList]; |
86
|
|
|
foreach ($inheritList as $inheritRole) { |
87
|
|
|
$acl->addInherit($aclRole, $aclRoleList[$inheritRole]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $acl; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|