1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Role.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:Permissions! |
9
|
|
|
* @subpackage Entities |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 12.03.14 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Permissions\Entities; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
|
21
|
|
|
use IPub; |
22
|
|
|
use IPub\Permissions\Exceptions; |
23
|
|
|
|
24
|
1 |
|
class Role extends Nette\Object implements IRole |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var IRole|NULL |
33
|
|
|
*/ |
34
|
|
|
protected $parent; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \SplObjectStorage |
38
|
|
|
*/ |
39
|
|
|
protected $children; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string|NULL |
43
|
|
|
*/ |
44
|
|
|
protected $name; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string|NULL |
48
|
|
|
*/ |
49
|
|
|
protected $comment; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \SplObjectStorage |
53
|
|
|
*/ |
54
|
|
|
protected $permissions; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $id |
58
|
|
|
* @param string|NULL $name |
59
|
|
|
* @param string|NULL $comment |
60
|
|
|
*/ |
61
|
|
|
public function __construct(string $id, string $name = NULL, string $comment = NULL) |
62
|
|
|
{ |
63
|
|
|
// Role identifier |
64
|
1 |
|
$this->id = $id; |
65
|
|
|
|
66
|
1 |
|
$this->name = $name; |
67
|
1 |
|
$this->comment = $comment; |
68
|
|
|
|
69
|
|
|
// Storage initialization |
70
|
1 |
|
$this->permissions = new \SplObjectStorage(); |
71
|
1 |
|
$this->children = new \SplObjectStorage(); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function setParent(IRole $parent = NULL) |
78
|
|
|
{ |
79
|
1 |
|
$parent->addChild($this); |
|
|
|
|
80
|
|
|
|
81
|
1 |
|
$this->parent = $parent; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getParent() |
88
|
|
|
{ |
89
|
1 |
|
return $this->parent; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function setChildren(array $roles) |
96
|
|
|
{ |
97
|
|
|
foreach ($this->getChildren() as $child) { |
98
|
|
|
$child->setParent(NULL); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->children = new \SplObjectStorage(); |
102
|
|
|
|
103
|
|
|
foreach ($roles as $child) { |
104
|
|
|
if ($child instanceof IRole) { |
105
|
|
|
$this->children->attach($child); |
106
|
|
|
$child->setParent($this); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function addChild(IRole $role) |
115
|
|
|
{ |
116
|
1 |
|
if (!$this->children->contains($role)) { |
117
|
1 |
|
$this->children->attach($role); |
118
|
1 |
|
$role->setParent($this); |
119
|
|
|
} |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function getChildren() : array |
|
|
|
|
126
|
|
|
{ |
127
|
1 |
|
$children = []; |
128
|
|
|
|
129
|
1 |
|
$this->children->rewind(); |
130
|
|
|
|
131
|
1 |
|
while ($this->children->valid()) |
132
|
|
|
{ |
133
|
1 |
|
$children[] = $this->children->current(); |
134
|
1 |
|
$this->children->next(); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return $children; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function getRoleId() : string |
144
|
|
|
{ |
145
|
1 |
|
return $this->id; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function setName(string $name) |
152
|
|
|
{ |
153
|
1 |
|
$this->name = $name; |
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function getName() |
160
|
|
|
{ |
161
|
|
|
return $this->name ?: $this->id; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function setComment(string $comment) |
168
|
|
|
{ |
169
|
1 |
|
$this->comment = $comment; |
170
|
1 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function getComment() |
176
|
|
|
{ |
177
|
|
|
return $this->comment; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function setPermissions(array $permissions) |
184
|
|
|
{ |
185
|
1 |
|
$this->permissions = new \SplObjectStorage(); |
186
|
|
|
|
187
|
1 |
|
foreach ($permissions as $permission) { |
188
|
1 |
|
if ($permission instanceof IPermission) { |
189
|
1 |
|
$this->permissions->attach($permission); |
190
|
|
|
} |
191
|
|
|
} |
192
|
1 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public function addPermission(IPermission $permission) |
198
|
|
|
{ |
199
|
|
|
if (!$this->permissions->contains($permission)) { |
200
|
|
|
$this->permissions->attach($permission); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function getPermissions() : array |
208
|
|
|
{ |
209
|
1 |
|
$permissions = []; |
210
|
|
|
|
211
|
1 |
|
$this->permissions->rewind(); |
212
|
|
|
|
213
|
1 |
|
while ($this->permissions->valid()) |
214
|
|
|
{ |
215
|
1 |
|
$permissions[] = $this->permissions->current(); |
216
|
1 |
|
$this->permissions->next(); |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
return $permissions; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
|
|
public function hasPermission(IPermission $permission) : bool |
226
|
|
|
{ |
227
|
|
|
return $this->permissions->contains($permission); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
|
|
public function removePermission(IPermission $permission) |
234
|
|
|
{ |
235
|
|
|
if (!$this->permissions->contains($permission)) { |
236
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Permission "%s" cannot be removed since it is not associated with the role %s', $permission, $this->getName())); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$this->permissions->detach($permission); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
|
|
public function clearPermissions() |
246
|
|
|
{ |
247
|
|
|
$this->permissions = new \SplObjectStorage(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
|
public function isLocked() : bool |
254
|
|
|
{ |
255
|
|
|
return in_array($this->id, [IRole::ROLE_ANONYMOUS, IRole::ROLE_AUTHENTICATED, IRole::ROLE_ADMINISTRATOR], TRUE); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
|
|
public function isAnonymous() : bool |
262
|
|
|
{ |
263
|
|
|
return $this->id === IRole::ROLE_ANONYMOUS; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function isAuthenticated() : bool |
270
|
|
|
{ |
271
|
|
|
return $this->id === IRole::ROLE_AUTHENTICATED; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
|
|
public function isAdministrator() : bool |
278
|
|
|
{ |
279
|
1 |
|
return $this->id === IRole::ROLE_ADMINISTRATOR; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Convert role object to string |
284
|
|
|
* |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
|
|
public function __toString() |
288
|
|
|
{ |
289
|
|
|
return $this->id; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: