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; |
13
|
|
|
|
14
|
|
|
use Phalcon\Acl\Adapter\Memory; |
15
|
|
|
use Phalcon\Acl\Component; |
16
|
|
|
use Phalcon\Acl\Role; |
17
|
|
|
use Zemit\Config\ConfigInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
21
|
|
|
*/ |
22
|
|
|
class Security extends \Phalcon\Security |
23
|
|
|
{ |
24
|
|
|
protected ?array $permissions; |
25
|
|
|
|
26
|
|
|
public function getPermissionsConfig(): array |
27
|
|
|
{ |
28
|
|
|
return $this->getConfig()->pathToArray('permissions') ?? []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getConfig(): ConfigInterface |
32
|
|
|
{ |
33
|
|
|
return $this->getDI()->get('config'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return an ACL for the specified components name |
38
|
|
|
* @param array $componentsName |
39
|
|
|
* @param array|null $permissions |
40
|
|
|
* @param string $inherit |
41
|
|
|
* @return Memory |
42
|
|
|
* @todo cache the ACL |
43
|
|
|
* @todo move to its own ACL class, shouldn't be in the Phalcon\Security |
44
|
|
|
*/ |
45
|
|
|
public function getAcl(array $componentsName = ['components'], ?array $permissions = null, string $inherit = 'inherit'): Memory |
46
|
|
|
{ |
47
|
|
|
$acl = new Memory(); |
48
|
|
|
$aclRoleList = []; |
49
|
|
|
|
50
|
|
|
$this->permissions = $this->getPermissionsConfig(); |
51
|
|
|
|
52
|
|
|
$featureList = $this->permissions['features'] ?? []; |
53
|
|
|
$roleList = $this->permissions['roles'] ?? []; |
54
|
|
|
|
55
|
|
|
foreach ($roleList as $role => $rolePermission) { |
56
|
|
|
|
57
|
|
|
$role = $role === '*' ? 'everyone' : $role; |
58
|
|
|
$aclRole = new Role($role); |
59
|
|
|
$aclRoleList[$role] = $aclRole; |
60
|
|
|
$acl->addRole($aclRole); |
61
|
|
|
|
62
|
|
|
if (isset($rolePermission['features'])) { |
63
|
|
|
foreach ($rolePermission['features'] as $feature) { |
64
|
|
|
$rolePermission = array_merge_recursive($rolePermission, $featureList[$feature] ?? []); |
65
|
|
|
// @todo remove duplicates |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($componentsName as $componentName) { |
70
|
|
|
$components = $rolePermission[$componentName] ?? []; |
71
|
|
|
$components = is_array($components) ? $components : [$components]; |
72
|
|
|
|
73
|
|
|
foreach ($components as $component => $accessList) { |
74
|
|
|
if (empty($component)) { |
75
|
|
|
$component = $accessList; |
76
|
|
|
$accessList = '*'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($component !== '*') { |
80
|
|
|
$aclComponent = new Component($component); |
81
|
|
|
$acl->addComponent($aclComponent, $accessList); |
82
|
|
|
$acl->allow($aclRole, $aclComponent, $accessList); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add inheritance (role extends) |
90
|
|
|
*/ |
91
|
|
|
foreach ($aclRoleList as $role => $aclRole) { |
92
|
|
|
$inheritList = $permissions[$role][$inherit] ?? []; |
93
|
|
|
$inheritList = is_array($inheritList) ? $inheritList : [$inheritList]; |
94
|
|
|
foreach ($inheritList as $inheritRole) { |
95
|
|
|
$acl->addInherit($aclRole, $aclRoleList[$inheritRole]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $acl; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|