1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Event; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\RoleService as RoleServiceInterface; |
12
|
|
|
use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation; |
13
|
|
|
use eZ\Publish\API\Repository\Values\User\PolicyCreateStruct; |
14
|
|
|
use eZ\Publish\API\Repository\Values\User\PolicyDraft; |
15
|
|
|
use eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct; |
16
|
|
|
use eZ\Publish\API\Repository\Values\User\Role; |
17
|
|
|
use eZ\Publish\API\Repository\Values\User\RoleAssignment; |
18
|
|
|
use eZ\Publish\API\Repository\Values\User\RoleCreateStruct; |
19
|
|
|
use eZ\Publish\API\Repository\Values\User\RoleDraft; |
20
|
|
|
use eZ\Publish\API\Repository\Values\User\RoleUpdateStruct; |
21
|
|
|
use eZ\Publish\API\Repository\Values\User\User; |
22
|
|
|
use eZ\Publish\API\Repository\Values\User\UserGroup; |
23
|
|
|
use eZ\Publish\API\Repository\Events\Role\AddPolicyByRoleDraftEvent; |
24
|
|
|
use eZ\Publish\API\Repository\Events\Role\AssignRoleToUserEvent; |
25
|
|
|
use eZ\Publish\API\Repository\Events\Role\AssignRoleToUserGroupEvent; |
26
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeAddPolicyByRoleDraftEvent; |
27
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeAssignRoleToUserEvent; |
28
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeAssignRoleToUserGroupEvent; |
29
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeCreateRoleDraftEvent; |
30
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeCreateRoleEvent; |
31
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeDeleteRoleDraftEvent; |
32
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeDeleteRoleEvent; |
33
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforePublishRoleDraftEvent; |
34
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeRemovePolicyByRoleDraftEvent; |
35
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeRemoveRoleAssignmentEvent; |
36
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeUpdatePolicyByRoleDraftEvent; |
37
|
|
|
use eZ\Publish\API\Repository\Events\Role\BeforeUpdateRoleDraftEvent; |
38
|
|
|
use eZ\Publish\API\Repository\Events\Role\CreateRoleDraftEvent; |
39
|
|
|
use eZ\Publish\API\Repository\Events\Role\CreateRoleEvent; |
40
|
|
|
use eZ\Publish\API\Repository\Events\Role\DeleteRoleDraftEvent; |
41
|
|
|
use eZ\Publish\API\Repository\Events\Role\DeleteRoleEvent; |
42
|
|
|
use eZ\Publish\API\Repository\Events\Role\PublishRoleDraftEvent; |
43
|
|
|
use eZ\Publish\API\Repository\Events\Role\RemovePolicyByRoleDraftEvent; |
44
|
|
|
use eZ\Publish\API\Repository\Events\Role\RemoveRoleAssignmentEvent; |
45
|
|
|
use eZ\Publish\API\Repository\Events\Role\UpdatePolicyByRoleDraftEvent; |
46
|
|
|
use eZ\Publish\API\Repository\Events\Role\UpdateRoleDraftEvent; |
47
|
|
|
use eZ\Publish\SPI\Repository\Decorator\RoleServiceDecorator; |
48
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
49
|
|
|
|
50
|
|
|
class RoleService extends RoleServiceDecorator |
51
|
|
|
{ |
52
|
|
|
/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */ |
53
|
|
|
protected $eventDispatcher; |
54
|
|
|
|
55
|
|
|
public function __construct( |
56
|
|
|
RoleServiceInterface $innerService, |
57
|
|
|
EventDispatcherInterface $eventDispatcher |
58
|
|
|
) { |
59
|
|
|
parent::__construct($innerService); |
60
|
|
|
|
61
|
|
|
$this->eventDispatcher = $eventDispatcher; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function createRole(RoleCreateStruct $roleCreateStruct): RoleDraft |
65
|
|
|
{ |
66
|
|
|
$eventData = [$roleCreateStruct]; |
67
|
|
|
|
68
|
|
|
$beforeEvent = new BeforeCreateRoleEvent(...$eventData); |
69
|
|
|
|
70
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
71
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
72
|
|
|
return $beforeEvent->getRoleDraft(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$roleDraft = $beforeEvent->hasRoleDraft() |
76
|
|
|
? $beforeEvent->getRoleDraft() |
77
|
|
|
: $this->innerService->createRole($roleCreateStruct); |
78
|
|
|
|
79
|
|
|
$this->eventDispatcher->dispatch( |
80
|
|
|
new CreateRoleEvent($roleDraft, ...$eventData) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $roleDraft; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function createRoleDraft(Role $role) |
87
|
|
|
{ |
88
|
|
|
$eventData = [$role]; |
89
|
|
|
|
90
|
|
|
$beforeEvent = new BeforeCreateRoleDraftEvent(...$eventData); |
91
|
|
|
|
92
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
93
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
94
|
|
|
return $beforeEvent->getRoleDraft(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$roleDraft = $beforeEvent->hasRoleDraft() |
98
|
|
|
? $beforeEvent->getRoleDraft() |
99
|
|
|
: $this->innerService->createRoleDraft($role); |
100
|
|
|
|
101
|
|
|
$this->eventDispatcher->dispatch( |
102
|
|
|
new CreateRoleDraftEvent($roleDraft, ...$eventData) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
return $roleDraft; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function updateRoleDraft( |
109
|
|
|
RoleDraft $roleDraft, |
110
|
|
|
RoleUpdateStruct $roleUpdateStruct |
111
|
|
|
) { |
112
|
|
|
$eventData = [ |
113
|
|
|
$roleDraft, |
114
|
|
|
$roleUpdateStruct, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$beforeEvent = new BeforeUpdateRoleDraftEvent(...$eventData); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
120
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
121
|
|
|
return $beforeEvent->getUpdatedRoleDraft(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$updatedRoleDraft = $beforeEvent->hasUpdatedRoleDraft() |
125
|
|
|
? $beforeEvent->getUpdatedRoleDraft() |
126
|
|
|
: $this->innerService->updateRoleDraft($roleDraft, $roleUpdateStruct); |
127
|
|
|
|
128
|
|
|
$this->eventDispatcher->dispatch( |
129
|
|
|
new UpdateRoleDraftEvent($updatedRoleDraft, ...$eventData) |
|
|
|
|
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
return $updatedRoleDraft; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function addPolicyByRoleDraft( |
136
|
|
|
RoleDraft $roleDraft, |
137
|
|
|
PolicyCreateStruct $policyCreateStruct |
138
|
|
|
) { |
139
|
|
|
$eventData = [ |
140
|
|
|
$roleDraft, |
141
|
|
|
$policyCreateStruct, |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
$beforeEvent = new BeforeAddPolicyByRoleDraftEvent(...$eventData); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
147
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
148
|
|
|
return $beforeEvent->getUpdatedRoleDraft(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$updatedRoleDraft = $beforeEvent->hasUpdatedRoleDraft() |
152
|
|
|
? $beforeEvent->getUpdatedRoleDraft() |
153
|
|
|
: $this->innerService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct); |
154
|
|
|
|
155
|
|
|
$this->eventDispatcher->dispatch( |
156
|
|
|
new AddPolicyByRoleDraftEvent($updatedRoleDraft, ...$eventData) |
|
|
|
|
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
return $updatedRoleDraft; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function removePolicyByRoleDraft( |
163
|
|
|
RoleDraft $roleDraft, |
164
|
|
|
PolicyDraft $policyDraft |
165
|
|
|
) { |
166
|
|
|
$eventData = [ |
167
|
|
|
$roleDraft, |
168
|
|
|
$policyDraft, |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
$beforeEvent = new BeforeRemovePolicyByRoleDraftEvent(...$eventData); |
|
|
|
|
172
|
|
|
|
173
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
174
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
175
|
|
|
return $beforeEvent->getUpdatedRoleDraft(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$updatedRoleDraft = $beforeEvent->hasUpdatedRoleDraft() |
179
|
|
|
? $beforeEvent->getUpdatedRoleDraft() |
180
|
|
|
: $this->innerService->removePolicyByRoleDraft($roleDraft, $policyDraft); |
181
|
|
|
|
182
|
|
|
$this->eventDispatcher->dispatch( |
183
|
|
|
new RemovePolicyByRoleDraftEvent($updatedRoleDraft, ...$eventData) |
|
|
|
|
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
return $updatedRoleDraft; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function updatePolicyByRoleDraft( |
190
|
|
|
RoleDraft $roleDraft, |
191
|
|
|
PolicyDraft $policy, |
192
|
|
|
PolicyUpdateStruct $policyUpdateStruct |
193
|
|
|
) { |
194
|
|
|
$eventData = [ |
195
|
|
|
$roleDraft, |
196
|
|
|
$policy, |
197
|
|
|
$policyUpdateStruct, |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
$beforeEvent = new BeforeUpdatePolicyByRoleDraftEvent(...$eventData); |
|
|
|
|
201
|
|
|
|
202
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
203
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
204
|
|
|
return $beforeEvent->getUpdatedPolicyDraft(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$updatedPolicyDraft = $beforeEvent->hasUpdatedPolicyDraft() |
208
|
|
|
? $beforeEvent->getUpdatedPolicyDraft() |
209
|
|
|
: $this->innerService->updatePolicyByRoleDraft($roleDraft, $policy, $policyUpdateStruct); |
210
|
|
|
|
211
|
|
|
$this->eventDispatcher->dispatch( |
212
|
|
|
new UpdatePolicyByRoleDraftEvent($updatedPolicyDraft, ...$eventData) |
|
|
|
|
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
return $updatedPolicyDraft; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
View Code Duplication |
public function deleteRoleDraft(RoleDraft $roleDraft): void |
219
|
|
|
{ |
220
|
|
|
$eventData = [$roleDraft]; |
221
|
|
|
|
222
|
|
|
$beforeEvent = new BeforeDeleteRoleDraftEvent(...$eventData); |
223
|
|
|
|
224
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
225
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
226
|
|
|
return; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$this->innerService->deleteRoleDraft($roleDraft); |
230
|
|
|
|
231
|
|
|
$this->eventDispatcher->dispatch( |
232
|
|
|
new DeleteRoleDraftEvent(...$eventData) |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function publishRoleDraft(RoleDraft $roleDraft): void |
237
|
|
|
{ |
238
|
|
|
$eventData = [$roleDraft]; |
239
|
|
|
|
240
|
|
|
$beforeEvent = new BeforePublishRoleDraftEvent(...$eventData); |
241
|
|
|
|
242
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
243
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
244
|
|
|
return; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$this->innerService->publishRoleDraft($roleDraft); |
248
|
|
|
|
249
|
|
|
$this->eventDispatcher->dispatch( |
250
|
|
|
new PublishRoleDraftEvent(...$eventData) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function deleteRole(Role $role): void |
255
|
|
|
{ |
256
|
|
|
$eventData = [$role]; |
257
|
|
|
|
258
|
|
|
$beforeEvent = new BeforeDeleteRoleEvent(...$eventData); |
259
|
|
|
|
260
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
261
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
262
|
|
|
return; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$this->innerService->deleteRole($role); |
266
|
|
|
|
267
|
|
|
$this->eventDispatcher->dispatch( |
268
|
|
|
new DeleteRoleEvent(...$eventData) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
View Code Duplication |
public function assignRoleToUserGroup( |
273
|
|
|
Role $role, |
274
|
|
|
UserGroup $userGroup, |
275
|
|
|
RoleLimitation $roleLimitation = null |
276
|
|
|
): void { |
277
|
|
|
$eventData = [ |
278
|
|
|
$role, |
279
|
|
|
$userGroup, |
280
|
|
|
$roleLimitation, |
281
|
|
|
]; |
282
|
|
|
|
283
|
|
|
$beforeEvent = new BeforeAssignRoleToUserGroupEvent(...$eventData); |
|
|
|
|
284
|
|
|
|
285
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
286
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
287
|
|
|
return; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$this->innerService->assignRoleToUserGroup($role, $userGroup, $roleLimitation); |
291
|
|
|
|
292
|
|
|
$this->eventDispatcher->dispatch( |
293
|
|
|
new AssignRoleToUserGroupEvent(...$eventData) |
|
|
|
|
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
View Code Duplication |
public function assignRoleToUser( |
298
|
|
|
Role $role, |
299
|
|
|
User $user, |
300
|
|
|
RoleLimitation $roleLimitation = null |
301
|
|
|
): void { |
302
|
|
|
$eventData = [ |
303
|
|
|
$role, |
304
|
|
|
$user, |
305
|
|
|
$roleLimitation, |
306
|
|
|
]; |
307
|
|
|
|
308
|
|
|
$beforeEvent = new BeforeAssignRoleToUserEvent(...$eventData); |
|
|
|
|
309
|
|
|
|
310
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
311
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
312
|
|
|
return; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$this->innerService->assignRoleToUser($role, $user, $roleLimitation); |
316
|
|
|
|
317
|
|
|
$this->eventDispatcher->dispatch( |
318
|
|
|
new AssignRoleToUserEvent(...$eventData) |
|
|
|
|
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function removeRoleAssignment(RoleAssignment $roleAssignment): void |
323
|
|
|
{ |
324
|
|
|
$eventData = [$roleAssignment]; |
325
|
|
|
|
326
|
|
|
$beforeEvent = new BeforeRemoveRoleAssignmentEvent(...$eventData); |
327
|
|
|
|
328
|
|
|
$this->eventDispatcher->dispatch($beforeEvent); |
329
|
|
|
if ($beforeEvent->isPropagationStopped()) { |
330
|
|
|
return; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$this->innerService->removeRoleAssignment($roleAssignment); |
334
|
|
|
|
335
|
|
|
$this->eventDispatcher->dispatch( |
336
|
|
|
new RemoveRoleAssignmentEvent(...$eventData) |
337
|
|
|
); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
This check looks for function calls that miss required arguments.