1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ThallesDella\GateKeeper\Roles; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use ThallesDella\GateKeeper\UserInfo; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Gate Keeper | Class Roles [ ROLE SYSTEM ] |
10
|
|
|
* |
11
|
|
|
* @category GateKeeper\Roles |
12
|
|
|
* @package ThallesDella\GateKeeper\Roles |
13
|
|
|
* @author Thalles D. koester <[email protected]> |
14
|
|
|
* @license https://choosealicense.com/licenses/mit/ MIT |
15
|
|
|
* @link https://github.com/thallesdella/gate-keeper |
16
|
|
|
*/ |
17
|
|
|
class Roles |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Array Object containing instances of group object |
21
|
|
|
* |
22
|
|
|
* @var ArrayObject |
23
|
|
|
*/ |
24
|
|
|
private $groups; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array Object containing instances of role object |
28
|
|
|
* |
29
|
|
|
* @var ArrayObject |
30
|
|
|
*/ |
31
|
|
|
private $roles; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Roles constructor. |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->groups = new ArrayObject(); |
39
|
|
|
$this->roles = new ArrayObject(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $groupName Name of the group to add |
44
|
|
|
* @param string|null $authUrl Auth page of the group |
45
|
|
|
* |
46
|
|
|
* @return Roles |
47
|
|
|
*/ |
48
|
|
|
public function addGroup(string $groupName, ?string $authUrl = null): Roles |
49
|
|
|
{ |
50
|
|
|
$group = new Group($groupName, $this->groups->count()); |
51
|
|
|
$group->setAuthUrl($authUrl); |
52
|
|
|
|
53
|
|
|
$this->groups->append([$groupName => $group]); |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $roleName Name of role to add |
59
|
|
|
* @param array|null $permissions Permissions of the role |
60
|
|
|
* @param string $groupName Group of the role |
61
|
|
|
* |
62
|
|
|
* @return Roles |
63
|
|
|
*/ |
64
|
|
|
public function addRole(string $roleName, ?array $permissions, string $groupName): Roles |
65
|
|
|
{ |
66
|
|
|
$roleInfo = new Role($roleName, $this->roles->count()); |
67
|
|
|
$roleInfo->setPermissions($permissions); |
68
|
|
|
$roleInfo->setGroup($groupName); |
69
|
|
|
|
70
|
|
|
$this->roles->append([$roleName => $roleInfo]); |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $roleName Name of the role to edit |
76
|
|
|
* @param array|null $permissions New permissions of the role |
77
|
|
|
* |
78
|
|
|
* @return Roles |
79
|
|
|
*/ |
80
|
|
|
public function editPermissions(string $roleName, ?array $permissions): Roles |
81
|
|
|
{ |
82
|
|
|
/** |
83
|
|
|
* Role object |
84
|
|
|
* |
85
|
|
|
* @var Role $roleInfo |
86
|
|
|
*/ |
87
|
|
|
$roleInfo = $this->roles[$roleName]; |
88
|
|
|
$roleInfo->setPermissions($permissions); |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $roleName Name of the role to edit |
94
|
|
|
* @param string $groupName New group |
95
|
|
|
* |
96
|
|
|
* @return Roles |
97
|
|
|
*/ |
98
|
|
|
public function editRoleGroup(string $roleName, string $groupName): Roles |
99
|
|
|
{ |
100
|
|
|
/** |
101
|
|
|
* Role object |
102
|
|
|
* |
103
|
|
|
* @var Role $roleInfo |
104
|
|
|
*/ |
105
|
|
|
$roleInfo = $this->roles[$roleName]; |
106
|
|
|
$roleInfo->setGroup($groupName); |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $groupName Name of the group to edit |
112
|
|
|
* @param string|null $url New auth url |
113
|
|
|
* |
114
|
|
|
* @return Roles |
115
|
|
|
*/ |
116
|
|
|
public function editAuthUrl(string $groupName, ?string $url): Roles |
117
|
|
|
{ |
118
|
|
|
/** |
119
|
|
|
* Group object |
120
|
|
|
* |
121
|
|
|
* @var Group $groupInfo |
122
|
|
|
*/ |
123
|
|
|
$groupInfo = $this->groups[$groupName]; |
124
|
|
|
$groupInfo->setAuthUrl($url); |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $roleName Name of the role to be moved |
130
|
|
|
* @param string|null $otherRole Pivot role |
131
|
|
|
* |
132
|
|
|
* @return Roles |
133
|
|
|
*/ |
134
|
|
|
public function moveRoleAfter(string $roleName, ?string $otherRole): Roles |
135
|
|
|
{ |
136
|
|
|
foreach ($this->roles->getIterator() as $key => $value) { |
137
|
|
|
if ($value->level > $this->roles[$roleName]->level) { |
138
|
|
|
$this->roles[$key]->level -= 1; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
foreach ($this->roles->getIterator() as $key => $value) { |
143
|
|
|
if (empty($otherRole) |
144
|
|
|
|| $value->level > $this->roles[$otherRole]->level |
145
|
|
|
) { |
146
|
|
|
$this->roles[$key]->level += 1; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->roles[$roleName]->level = $this->roles[$otherRole]->level + 1; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $roleName Name of the role to be deleted |
157
|
|
|
* |
158
|
|
|
* @return Roles |
159
|
|
|
*/ |
160
|
|
|
public function deleteRole(string $roleName): Roles |
161
|
|
|
{ |
162
|
|
|
foreach ($this->roles->getIterator() as $key => $value) { |
163
|
|
|
if ($value->level > $this->roles[$roleName]->level) { |
164
|
|
|
$this->roles[$key]->level -= 1; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->roles->offsetUnset($roleName); |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param UserInfo $user |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
public function getPermissions(UserInfo $user): array |
178
|
|
|
{ |
179
|
|
|
if (!isset($this->roles[$user->role]) |
180
|
|
|
|| empty($this->roles[$user->role]) |
181
|
|
|
) { |
182
|
|
|
return []; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$permissions = new ArrayObject(); |
186
|
|
|
$userRole = $this->roles[$user->role]; |
187
|
|
|
foreach ($this->roles as $key => $value) { |
188
|
|
|
if ($value->level <= $userRole->level) { |
189
|
|
|
$permissions->append($value->permissions); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
return $permissions->getArrayCopy(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return ArrayObject |
197
|
|
|
*/ |
198
|
|
|
public function getRoles(): ArrayObject |
199
|
|
|
{ |
200
|
|
|
return $this->roles; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $roleName Name of the role |
205
|
|
|
* |
206
|
|
|
* @return Role |
207
|
|
|
*/ |
208
|
|
|
public function getRole(string $roleName): Role |
209
|
|
|
{ |
210
|
|
|
return $this->roles[$roleName]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $groupName |
215
|
|
|
* |
216
|
|
|
* @return Group|null |
217
|
|
|
*/ |
218
|
|
|
public function getGroup(string $groupName): ?Group |
219
|
|
|
{ |
220
|
|
|
|
221
|
|
|
return ($this->groups[$groupName] ?? null); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $roleName |
226
|
|
|
* |
227
|
|
|
* @return string|null |
228
|
|
|
*/ |
229
|
|
|
public function getRoleAuthUrl(string $roleName): ?string |
230
|
|
|
{ |
231
|
|
|
$roleInfo = $this->roles[$roleName]; |
232
|
|
|
$group = $this->getGroup($roleInfo->group); |
233
|
|
|
return $group->auth_url; |
234
|
|
|
} |
235
|
|
|
} |