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
|
|
|
class Role implements IRole |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Implement nette smart magic |
28
|
|
|
*/ |
29
|
|
|
use Nette\SmartObject; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $id; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var IRole|NULL |
38
|
|
|
*/ |
39
|
|
|
protected $parent; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \SplObjectStorage |
43
|
|
|
*/ |
44
|
|
|
protected $children; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string|NULL |
48
|
|
|
*/ |
49
|
|
|
protected $name; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string|NULL |
53
|
|
|
*/ |
54
|
|
|
protected $comment; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \SplObjectStorage |
58
|
|
|
*/ |
59
|
|
|
protected $permissions; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $id |
63
|
|
|
* @param string|NULL $name |
64
|
|
|
* @param string|NULL $comment |
65
|
|
|
*/ |
66
|
|
|
public function __construct(string $id, string $name = NULL, string $comment = NULL) |
67
|
|
|
{ |
68
|
|
|
// Role identifier |
69
|
|
|
$this->id = $id; |
70
|
|
|
|
71
|
|
|
$this->name = $name; |
72
|
|
|
$this->comment = $comment; |
73
|
|
|
|
74
|
|
|
// Storage initialization |
75
|
|
|
$this->permissions = new \SplObjectStorage(); |
76
|
|
|
$this->children = new \SplObjectStorage(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function setParent(IRole $parent = NULL) |
83
|
|
|
{ |
84
|
|
|
$this->parent = $parent; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function getParent() |
91
|
|
|
{ |
92
|
|
|
return $this->parent; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function setChildren(array $roles) |
99
|
|
|
{ |
100
|
|
|
$this->children = new \SplObjectStorage(); |
101
|
|
|
|
102
|
|
|
foreach ($roles as $child) { |
103
|
|
|
if ($child instanceof IRole) { |
104
|
|
|
$this->children->attach($child); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function addChild(IRole $role) |
113
|
|
|
{ |
114
|
|
|
if (!$this->children->contains($role)) { |
115
|
|
|
$this->children->attach($role); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function getChildren() : array |
123
|
|
|
{ |
124
|
|
|
return $this->children; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function getRoleId() : string |
131
|
|
|
{ |
132
|
|
|
return $this->id; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function setName(string $name) |
139
|
|
|
{ |
140
|
|
|
$this->name = $name; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function getName() |
147
|
|
|
{ |
148
|
|
|
return $this->name ?: $this->id; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function setComment(string $comment) |
155
|
|
|
{ |
156
|
|
|
$this->comment = $comment; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function getComment() |
163
|
|
|
{ |
164
|
|
|
return $this->comment; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
public function setPermissions(array $permissions) |
171
|
|
|
{ |
172
|
|
|
$this->permissions = new \SplObjectStorage(); |
173
|
|
|
|
174
|
|
|
foreach ($permissions as $permission) { |
175
|
|
|
if ($permission instanceof IPermission) { |
176
|
|
|
$this->permissions->attach($permission); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function addPermission(IPermission $permission) |
185
|
|
|
{ |
186
|
|
|
if (!$this->permissions->contains($permission)) { |
187
|
|
|
$this->permissions->attach($permission); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function getPermissions() : array |
195
|
|
|
{ |
196
|
|
|
return $this->permissions; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function hasPermission(IPermission $permission) : bool |
203
|
|
|
{ |
204
|
|
|
return $this->permissions->contains($permission); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function removePermission(IPermission $permission) |
211
|
|
|
{ |
212
|
|
|
if (!$this->permissions->contains($permission)) { |
213
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Permission "%s" cannot be removed since it is not associated with the role %s', $permission, $this->getName())); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->permissions->detach($permission); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
|
|
public function clearPermissions() |
223
|
|
|
{ |
224
|
|
|
$this->permissions = new \SplObjectStorage(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function isLocked() : bool |
231
|
|
|
{ |
232
|
|
|
return in_array($this->id, [IRole::ROLE_ANONYMOUS, IRole::ROLE_AUTHENTICATED, IRole::ROLE_ADMINISTRATOR], TRUE); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function isAnonymous() : bool |
239
|
|
|
{ |
240
|
|
|
return $this->id === IRole::ROLE_ANONYMOUS; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public function isAuthenticated() : bool |
247
|
|
|
{ |
248
|
|
|
return $this->id === IRole::ROLE_AUTHENTICATED; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritdoc} |
253
|
|
|
*/ |
254
|
|
|
public function isAdministrator() : bool |
255
|
|
|
{ |
256
|
|
|
return $this->id === IRole::ROLE_ADMINISTRATOR; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Convert role object to string |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function __toString() |
265
|
|
|
{ |
266
|
|
|
return $this->id; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|