/
AbstractWorkflowEventsListener.php
337 lines (313 loc) · 12.5 KB
/
AbstractWorkflowEventsListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
* Routes.
*
* @copyright Zikula contributors (Zikula)
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @author Zikula contributors <info@ziku.la>.
* @see https://ziku.la
* @version Generated by ModuleStudio 1.4.0 (https://modulestudio.de).
*/
declare(strict_types=1);
namespace Zikula\RoutesModule\Listener\Base;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Zikula\Bundle\CoreBundle\Doctrine\EntityAccess;
use Zikula\RoutesModule\Entity\Factory\EntityFactory;
use Zikula\RoutesModule\Helper\PermissionHelper;
/**
* Event handler implementation class for workflow events.
*
* @see /src/docs/Workflows/WorkflowEvents.md
*/
abstract class AbstractWorkflowEventsListener implements EventSubscriberInterface
{
/**
* @var EntityFactory
*/
protected $entityFactory;
/**
* @var PermissionHelper
*/
protected $permissionHelper;
public function __construct(
EntityFactory $entityFactory,
PermissionHelper $permissionHelper
) {
$this->entityFactory = $entityFactory;
$this->permissionHelper = $permissionHelper;
}
public static function getSubscribedEvents()
{
return [
'workflow.guard' => ['onGuard', 5],
'workflow.leave' => ['onLeave', 5],
'workflow.transition' => ['onTransition', 5],
'workflow.enter' => ['onEnter', 5],
'workflow.entered' => ['onEntered', 5],
'workflow.completed' => ['onCompleted', 5],
'workflow.announce' => ['onAnnounce', 5]
];
}
/**
* Listener for the `workflow.guard` event.
*
* Occurs before a transition is started and when testing which transitions are available.
* Validates whether the transition is allowed or not.
* Allows to block it by calling `$event->setBlocked(true);`.
*
* This event is also triggered for each workflow individually, so you can react only to the events
* of a specific workflow by listening to `workflow.<workflow_name>.guard` instead.
* You can even listen to some specific transitions or states for a specific workflow
* using `workflow.<workflow_name>.guard.<state_name>`.
*
* You can access general data available in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
*
* Access the entity: `$entity = $event->getSubject();`
* Access the marking: `$marking = $event->getMarking();`
* Access the transition: `$transition = $event->getTransition();`
* Access the workflow name: `$workflowName = $event->getWorkflowName();`
* Example for preventing a transition:
* `if (!$event->isBlocked()) {
* $event->setBlocked(true);
* }`
* Example with providing a reason:
* `$event->addTransitionBlocker(
* new TransitionBlocker('You can not this because that.')
* );`
*/
public function onGuard(GuardEvent $event): void
{
/** @var EntityAccess $entity */
$entity = $event->getSubject();
if (!$this->isEntityManagedByThisBundle($entity) || !method_exists($entity, 'get_objectType')) {
return;
}
$objectType = $entity->get_objectType();
$permissionLevel = ACCESS_READ;
$transitionName = $event->getTransition()->getName();
$hasApproval = false;
switch ($transitionName) {
case 'defer':
case 'submit':
$permissionLevel = $hasApproval ? ACCESS_COMMENT : ACCESS_EDIT;
break;
case 'update':
case 'reject':
case 'accept':
case 'publish':
case 'unpublish':
case 'archive':
case 'trash':
case 'recover':
$permissionLevel = ACCESS_EDIT;
break;
case 'approve':
case 'demote':
$permissionLevel = ACCESS_ADD;
break;
case 'delete':
$permissionLevel = ACCESS_DELETE;
break;
}
if (!$this->permissionHelper->hasEntityPermission($entity, $permissionLevel)) {
// no permission for this transition, so disallow it (without a reason)
$event->setBlocked(true);
return;
}
}
/**
* Listener for the `workflow.leave` event.
*
* Occurs after a subject has left it's current state.
* Carries the marking with the initial places.
*
* This event is also triggered for each workflow individually, so you can react only to the events
* of a specific workflow by listening to `workflow.<workflow_name>.leave` instead.
* You can even listen to some specific transitions or states for a specific workflow
* using `workflow.<workflow_name>.leave.<state_name>`.
*
* You can access general data available in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
*
* Access the entity: `$entity = $event->getSubject();`
* Access the marking: `$marking = $event->getMarking();`
* Access the transition: `$transition = $event->getTransition();`
* Access the workflow name: `$workflowName = $event->getWorkflowName();`
*/
public function onLeave(Event $event): void
{
/** @var EntityAccess $entity */
$entity = $event->getSubject();
if (!$this->isEntityManagedByThisBundle($entity) || !method_exists($entity, 'get_objectType')) {
return;
}
}
/**
* Listener for the `workflow.transition` event.
*
* Occurs before starting to transition to the new state.
* Carries the marking with the current places.
*
* This event is also triggered for each workflow individually, so you can react only to the events
* of a specific workflow by listening to `workflow.<workflow_name>.transition` instead.
* You can even listen to some specific transitions or states for a specific workflow
* using `workflow.<workflow_name>.transition.<state_name>`.
*
* You can access general data available in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
*
* Access the entity: `$entity = $event->getSubject();`
* Access the marking: `$marking = $event->getMarking();`
* Access the transition: `$transition = $event->getTransition();`
* Access the workflow name: `$workflowName = $event->getWorkflowName();`
*/
public function onTransition(Event $event): void
{
/** @var EntityAccess $entity */
$entity = $event->getSubject();
if (!$this->isEntityManagedByThisBundle($entity) || !method_exists($entity, 'get_objectType')) {
return;
}
}
/**
* Listener for the `workflow.enter` event.
*
* Occurs before the subject enters into the new state and places are updated.
* This means the marking of the subject is not yet updated with the new places.
*
* This event is also triggered for each workflow individually, so you can react only to the events
* of a specific workflow by listening to `workflow.<workflow_name>.enter` instead.
* You can even listen to some specific transitions or states for a specific workflow
* using `workflow.<workflow_name>.enter.<state_name>`.
*
* You can access general data available in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
*
* Access the entity: `$entity = $event->getSubject();`
* Access the marking: `$marking = $event->getMarking();`
* Access the transition: `$transition = $event->getTransition();`
* Access the workflow name: `$workflowName = $event->getWorkflowName();`
*/
public function onEnter(Event $event): void
{
/** @var EntityAccess $entity */
$entity = $event->getSubject();
if (!$this->isEntityManagedByThisBundle($entity) || !method_exists($entity, 'get_objectType')) {
return;
}
}
/**
* Listener for the `workflow.entered` event.
*
* Occurs after the subject has entered into the new state.
* Carries the marking with the new places.
* This is a good place to flush data in Doctrine based on the entity not being updated yet.
*
* This event is also triggered for each workflow individually, so you can react only to the events
* of a specific workflow by listening to `workflow.<workflow_name>.entered` instead.
* You can even listen to some specific transitions or states for a specific workflow
* using `workflow.<workflow_name>.entered.<state_name>`.
*
* You can access general data available in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
*
* Access the entity: `$entity = $event->getSubject();`
* Access the marking: `$marking = $event->getMarking();`
* Access the transition: `$transition = $event->getTransition();`
* Access the workflow name: `$workflowName = $event->getWorkflowName();`
*/
public function onEntered(Event $event): void
{
/** @var EntityAccess $entity */
$entity = $event->getSubject();
if (!$this->isEntityManagedByThisBundle($entity) || !method_exists($entity, 'get_objectType')) {
return;
}
}
/**
* Listener for the `workflow.completed` event.
*
* Occurs after the subject has completed a transition.
*
* This event is also triggered for each workflow individually, so you can react only to the events
* of a specific workflow by listening to `workflow.<workflow_name>.completed` instead.
* You can even listen to some specific transitions or states for a specific workflow
* using `workflow.<workflow_name>.completed.<state_name>`.
*
* You can access general data available in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
*
* Access the entity: `$entity = $event->getSubject();`
* Access the marking: `$marking = $event->getMarking();`
* Access the transition: `$transition = $event->getTransition();`
* Access the workflow name: `$workflowName = $event->getWorkflowName();`
*/
public function onCompleted(Event $event): void
{
/** @var EntityAccess $entity */
$entity = $event->getSubject();
if (!$this->isEntityManagedByThisBundle($entity) || !method_exists($entity, 'get_objectType')) {
return;
}
}
/**
* Listener for the `workflow.announce` event.
*
* Triggered for each place that now is available for the subject.
*
* This event is also triggered for each workflow individually, so you can react only to the events
* of a specific workflow by listening to `workflow.<workflow_name>.announce` instead.
* You can even listen to some specific transitions or states for a specific workflow
* using `workflow.<workflow_name>.announce.<state_name>`.
*
* You can access general data available in the event.
*
* The event name:
* `echo 'Event: ' . $event->getName();`
*
* Access the entity: `$entity = $event->getSubject();`
* Access the marking: `$marking = $event->getMarking();`
* Access the transition: `$transition = $event->getTransition();`
* Access the workflow name: `$workflowName = $event->getWorkflowName();`
*/
public function onAnnounce(Event $event): void
{
/** @var EntityAccess $entity */
$entity = $event->getSubject();
if (!$this->isEntityManagedByThisBundle($entity) || !method_exists($entity, 'get_objectType')) {
return;
}
}
/**
* Checks whether this listener is responsible for the given entity or not.
*
* @param EntityAccess $entity The given entity
*/
protected function isEntityManagedByThisBundle($entity): bool
{
if (!($entity instanceof EntityAccess)) {
return false;
}
$entityClassParts = explode('\\', get_class($entity));
if ('DoctrineProxy' === $entityClassParts[0] && '__CG__' === $entityClassParts[1]) {
array_shift($entityClassParts);
array_shift($entityClassParts);
}
return 'Zikula' === $entityClassParts[0] && 'RoutesModule' === $entityClassParts[1];
}
}