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