1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Role; |
4
|
|
|
|
5
|
|
|
use Broadway\EventSourcing\EventSourcedAggregateRoot; |
6
|
|
|
use CultuurNet\UDB3\Role\Events\ConstraintAdded; |
7
|
|
|
use CultuurNet\UDB3\Role\Events\ConstraintRemoved; |
8
|
|
|
use CultuurNet\UDB3\Role\Events\ConstraintUpdated; |
9
|
|
|
use CultuurNet\UDB3\Role\Events\LabelAdded; |
10
|
|
|
use CultuurNet\UDB3\Role\Events\LabelRemoved; |
11
|
|
|
use CultuurNet\UDB3\Role\Events\PermissionAdded; |
12
|
|
|
use CultuurNet\UDB3\Role\Events\PermissionRemoved; |
13
|
|
|
use CultuurNet\UDB3\Role\Events\RoleCreated; |
14
|
|
|
use CultuurNet\UDB3\Role\Events\RoleDeleted; |
15
|
|
|
use CultuurNet\UDB3\Role\Events\RoleRenamed; |
16
|
|
|
use CultuurNet\UDB3\Role\Events\UserAdded; |
17
|
|
|
use CultuurNet\UDB3\Role\Events\UserRemoved; |
18
|
|
|
use CultuurNet\UDB3\Role\ValueObjects\Permission; |
19
|
|
|
use CultuurNet\UDB3\Role\ValueObjects\Query; |
20
|
|
|
use CultuurNet\UDB3\ValueObject\SapiVersion; |
21
|
|
|
use ValueObjects\Identity\UUID; |
22
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
23
|
|
|
|
24
|
|
|
class Role extends EventSourcedAggregateRoot |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var UUID |
28
|
|
|
*/ |
29
|
|
|
private $uuid; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var StringLiteral |
33
|
|
|
*/ |
34
|
|
|
private $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Query[] |
38
|
|
|
*/ |
39
|
|
|
private $queries = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Permission[] |
43
|
|
|
*/ |
44
|
|
|
private $permissions = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var UUID[] |
48
|
|
|
*/ |
49
|
|
|
private $labelIds = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var StringLiteral[] |
53
|
|
|
*/ |
54
|
|
|
private $userIds = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getAggregateRootId() |
60
|
|
|
{ |
61
|
|
|
return $this->uuid; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param UUID $uuid |
66
|
|
|
* @param StringLiteral $name |
67
|
|
|
* @return Role |
68
|
|
|
*/ |
69
|
|
|
public static function create( |
70
|
|
|
UUID $uuid, |
71
|
|
|
StringLiteral $name |
72
|
|
|
) { |
73
|
|
|
$role = new Role(); |
74
|
|
|
|
75
|
|
|
$role->apply(new RoleCreated( |
76
|
|
|
$uuid, |
77
|
|
|
$name |
78
|
|
|
)); |
79
|
|
|
|
80
|
|
|
return $role; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Rename the role. |
85
|
|
|
* |
86
|
|
|
* @param UUID $uuid |
87
|
|
|
* @param StringLiteral $name |
88
|
|
|
*/ |
89
|
|
|
public function rename( |
90
|
|
|
UUID $uuid, |
91
|
|
|
StringLiteral $name |
92
|
|
|
) { |
93
|
|
|
$this->apply(new RoleRenamed($uuid, $name)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param SapiVersion $sapiVersion |
98
|
|
|
* @param Query $query |
99
|
|
|
*/ |
100
|
|
|
public function addConstraint(SapiVersion $sapiVersion, Query $query): void |
101
|
|
|
{ |
102
|
|
|
if ($this->queryEmpty($sapiVersion)) { |
103
|
|
|
$this->apply(new ConstraintAdded($this->uuid, $sapiVersion, $query)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param SapiVersion $sapiVersion |
109
|
|
|
* @param Query $query |
110
|
|
|
*/ |
111
|
|
|
public function updateConstraint(SapiVersion $sapiVersion, Query $query): void |
112
|
|
|
{ |
113
|
|
|
if (!$this->queryEmpty($sapiVersion) && |
114
|
|
|
!$this->querySameValue($sapiVersion, $query)) { |
115
|
|
|
$this->apply(new ConstraintUpdated($this->uuid, $sapiVersion, $query)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param SapiVersion $sapiVersion |
121
|
|
|
*/ |
122
|
|
|
public function removeConstraint(SapiVersion $sapiVersion): void |
123
|
|
|
{ |
124
|
|
|
if (!$this->queryEmpty($sapiVersion)) { |
125
|
|
|
$this->apply(new ConstraintRemoved($this->uuid, $sapiVersion)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param SapiVersion $sapiVersion |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
private function queryEmpty(SapiVersion $sapiVersion): bool |
134
|
|
|
{ |
135
|
|
|
return empty($this->queries[$sapiVersion->toNative()]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param SapiVersion $sapiVersion |
140
|
|
|
* @param Query $query |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
private function querySameValue(SapiVersion $sapiVersion, Query $query): bool |
144
|
|
|
{ |
145
|
|
|
return $this->queries[$sapiVersion->toNative()]->sameValueAs($query); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add a permission to the role. |
150
|
|
|
* |
151
|
|
|
* @param UUID $uuid |
152
|
|
|
* @param Permission $permission |
153
|
|
|
*/ |
154
|
|
|
public function addPermission( |
155
|
|
|
UUID $uuid, |
156
|
|
|
Permission $permission |
157
|
|
|
) { |
158
|
|
|
if (!in_array($permission, $this->permissions)) { |
159
|
|
|
$this->apply(new PermissionAdded($uuid, $permission)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Remove a permission from the role. |
165
|
|
|
* |
166
|
|
|
* @param UUID $uuid |
167
|
|
|
* @param Permission $permission |
168
|
|
|
*/ |
169
|
|
|
public function removePermission( |
170
|
|
|
UUID $uuid, |
171
|
|
|
Permission $permission |
172
|
|
|
) { |
173
|
|
|
if (in_array($permission, $this->permissions)) { |
174
|
|
|
$this->apply(new PermissionRemoved($uuid, $permission)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param UUID $labelId |
180
|
|
|
*/ |
181
|
|
|
public function addLabel( |
182
|
|
|
UUID $labelId |
183
|
|
|
) { |
184
|
|
|
if (!in_array($labelId, $this->labelIds)) { |
185
|
|
|
$this->apply(new LabelAdded($this->uuid, $labelId)); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param \ValueObjects\Identity\UUID $labelId |
191
|
|
|
*/ |
192
|
|
|
public function removeLabel( |
193
|
|
|
UUID $labelId |
194
|
|
|
) { |
195
|
|
|
if (in_array($labelId, $this->labelIds)) { |
196
|
|
|
$this->apply(new LabelRemoved($this->uuid, $labelId)); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param StringLiteral $userId |
202
|
|
|
*/ |
203
|
|
|
public function addUser( |
204
|
|
|
StringLiteral $userId |
205
|
|
|
) { |
206
|
|
|
if (!in_array($userId, $this->userIds)) { |
207
|
|
|
$this->apply(new UserAdded($this->uuid, $userId)); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param StringLiteral $userId |
213
|
|
|
*/ |
214
|
|
|
public function removeUser( |
215
|
|
|
StringLiteral $userId |
216
|
|
|
) { |
217
|
|
|
if (in_array($userId, $this->userIds)) { |
218
|
|
|
$this->apply(new UserRemoved($this->uuid, $userId)); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Delete a role. |
224
|
|
|
* |
225
|
|
|
* @param UUID $uuid |
226
|
|
|
*/ |
227
|
|
|
public function delete( |
228
|
|
|
UUID $uuid |
229
|
|
|
) { |
230
|
|
|
$this->apply(new RoleDeleted($uuid)); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param RoleCreated $roleCreated |
235
|
|
|
*/ |
236
|
|
|
public function applyRoleCreated(RoleCreated $roleCreated) |
237
|
|
|
{ |
238
|
|
|
$this->uuid = $roleCreated->getUuid(); |
239
|
|
|
$this->name = $roleCreated->getName(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param RoleRenamed $roleRenamed |
244
|
|
|
*/ |
245
|
|
|
public function applyRoleRenamed(RoleRenamed $roleRenamed) |
246
|
|
|
{ |
247
|
|
|
$this->name = $roleRenamed->getName(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param ConstraintAdded $constraintAdded |
252
|
|
|
*/ |
253
|
|
|
public function applyConstraintAdded(ConstraintAdded $constraintAdded) |
254
|
|
|
{ |
255
|
|
|
$this->queries[$constraintAdded->getSapiVersion()->toNative()] = $constraintAdded->getQuery(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param ConstraintUpdated $constraintUpdated |
260
|
|
|
*/ |
261
|
|
|
public function applyConstraintUpdated(ConstraintUpdated $constraintUpdated) |
262
|
|
|
{ |
263
|
|
|
$this->queries[$constraintUpdated->getSapiVersion()->toNative()] = $constraintUpdated->getQuery(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param ConstraintRemoved $constraintRemoved |
268
|
|
|
*/ |
269
|
|
|
public function applyConstraintRemoved(ConstraintRemoved $constraintRemoved) |
270
|
|
|
{ |
271
|
|
|
$this->queries[$constraintRemoved->getSapiVersion()->toNative()] = null; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param PermissionAdded $permissionAdded |
276
|
|
|
*/ |
277
|
|
|
public function applyPermissionAdded(PermissionAdded $permissionAdded) |
278
|
|
|
{ |
279
|
|
|
$permission = $permissionAdded->getPermission(); |
280
|
|
|
|
281
|
|
|
$this->permissions[$permission->getName()] = $permission; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param PermissionRemoved $permissionRemoved |
286
|
|
|
*/ |
287
|
|
|
public function applyPermissionRemoved(PermissionRemoved $permissionRemoved) |
288
|
|
|
{ |
289
|
|
|
$permission = $permissionRemoved->getPermission(); |
290
|
|
|
|
291
|
|
|
unset($this->permissions[$permission->getName()]); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param LabelAdded $labelAdded |
296
|
|
|
*/ |
297
|
|
|
public function applyLabelAdded(LabelAdded $labelAdded) |
298
|
|
|
{ |
299
|
|
|
$labelId = $labelAdded->getLabelId(); |
300
|
|
|
$this->labelIds[] = $labelId; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param LabelRemoved $labelRemoved |
305
|
|
|
*/ |
306
|
|
|
public function applyLabelRemoved(LabelRemoved $labelRemoved) |
307
|
|
|
{ |
308
|
|
|
$labelId = $labelRemoved->getLabelId(); |
309
|
|
|
$this->labelIds = array_diff($this->labelIds, [$labelId]); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param UserAdded $userAdded |
314
|
|
|
*/ |
315
|
|
|
public function applyUserAdded(UserAdded $userAdded) |
316
|
|
|
{ |
317
|
|
|
$userId = $userAdded->getUserId(); |
318
|
|
|
$this->userIds[] = $userId; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param UserRemoved $userRemoved |
323
|
|
|
*/ |
324
|
|
|
public function applyUserRemoved(UserRemoved $userRemoved) |
325
|
|
|
{ |
326
|
|
|
$userId = $userRemoved->getUserId(); |
327
|
|
|
|
328
|
|
|
if (($index = array_search($userId, $this->userIds)) !== false) { |
329
|
|
|
unset($this->userIds[$index]); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|