|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\rbac\entity; |
|
4
|
|
|
|
|
5
|
|
|
use WebComplete\rbac\resource\ResourceInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Role implements RoleInterface |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $name; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ResourceInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $resource; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $childrenNames = []; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $permissionNames = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $name |
|
29
|
|
|
* @param ResourceInterface $resource |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(string $name, ResourceInterface $resource) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->name = $name; |
|
34
|
|
|
$this->resource = $resource; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getName(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->name; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param RoleInterface $role |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addChild(RoleInterface $role) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->childrenNames[$role->getName()] = true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $roleName |
|
55
|
|
|
*/ |
|
56
|
|
|
public function removeChild(string $roleName) |
|
57
|
|
|
{ |
|
58
|
|
|
unset($this->childrenNames[$roleName]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return RoleInterface[] |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getChildren(): array |
|
65
|
|
|
{ |
|
66
|
|
|
$result = []; |
|
67
|
|
|
$roleNames = \array_keys($this->childrenNames); |
|
68
|
|
|
foreach ($roleNames as $name) { |
|
69
|
|
|
$result[$name] = $this->resource->getRole($name); |
|
70
|
|
|
} |
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param PermissionInterface $permission |
|
76
|
|
|
*/ |
|
77
|
|
|
public function addPermission(PermissionInterface $permission) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->permissionNames[$permission->getName()] = true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $permissionName |
|
84
|
|
|
*/ |
|
85
|
|
|
public function removePermission(string $permissionName) |
|
86
|
|
|
{ |
|
87
|
|
|
unset($this->permissionNames[$permissionName]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param bool $withChildren |
|
92
|
|
|
* |
|
93
|
|
|
* @return PermissionInterface[] |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getPermissions(bool $withChildren = false): array |
|
96
|
|
|
{ |
|
97
|
|
|
$result = []; |
|
98
|
|
|
$permissionNames = \array_keys($this->permissionNames); |
|
99
|
|
|
|
|
100
|
|
|
foreach ($permissionNames as $name) { |
|
101
|
|
|
$permission = $this->resource->getPermission($name); |
|
102
|
|
|
$result[$name] = $permission; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ($withChildren) { |
|
106
|
|
|
foreach ($result as $permission) { |
|
107
|
|
|
$this->collectChildrenPermissions($permission, $result); |
|
108
|
|
|
} |
|
109
|
|
|
foreach ($this->getChildren() as $child) { |
|
110
|
|
|
$result = \array_merge($result, $child->getPermissions(true)); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $permissionName |
|
119
|
|
|
* @param array|null $params |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function checkAccess($permissionName, $params = null): bool |
|
124
|
|
|
{ |
|
125
|
|
|
$permissions = $this->getPermissions(true); |
|
126
|
|
|
if (isset($permissions[$permissionName])) { |
|
127
|
|
|
if ($permissions[$permissionName]->checkAccess($params)) { |
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param PermissionInterface $permission |
|
136
|
|
|
* @param $result |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function collectChildrenPermissions(PermissionInterface $permission, &$result) |
|
139
|
|
|
{ |
|
140
|
|
|
foreach ($permission->getChildren() as $childPermission) { |
|
141
|
|
|
$childPermissionName = $childPermission->getName(); |
|
142
|
|
|
if (!isset($result[$childPermissionName])) { |
|
143
|
|
|
$result[$childPermissionName] = $childPermission; |
|
144
|
|
|
$this->collectChildrenPermissions($childPermission, $result); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|