1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Workflow library. |
5
|
|
|
* |
6
|
|
|
* @package workflow |
7
|
|
|
* @author David Molineus <[email protected]> |
8
|
|
|
* @copyright 2014-2017 netzmacht David Molineus |
9
|
|
|
* @license LGPL 3.0 https://github.com/netzmacht/workflow |
10
|
|
|
* @filesource |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Netzmacht\Workflow\Flow\Condition\Transition; |
16
|
|
|
|
17
|
|
|
use Netzmacht\Workflow\Data\ErrorCollection; |
18
|
|
|
use Netzmacht\Workflow\Security\Permission; |
19
|
|
|
use Netzmacht\Workflow\Flow\Context; |
20
|
|
|
use Netzmacht\Workflow\Flow\Item; |
21
|
|
|
use Netzmacht\Workflow\Flow\Transition; |
22
|
|
|
use Netzmacht\Workflow\Security\User; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class StepPermissionCondition can be used to limit permissions of a workflow step. |
26
|
|
|
* |
27
|
|
|
* That means that a user can only transform the transition if he has the role which is assigned to the starting step. |
28
|
|
|
* |
29
|
|
|
* If this condition is assigned it always requires that the step has a role. If no role is given it will always fail. |
30
|
|
|
* |
31
|
|
|
* @package Netzmacht\Workflow\Flow\Condition\Transition |
32
|
|
|
*/ |
33
|
|
|
class StepPermissionCondition extends AbstractPermissionCondition |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* If a workflow is not started it does not have a current step. So you can decide if it should be allowed. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $allowStartTransition; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* StepPermissionCondition constructor. |
44
|
|
|
* |
45
|
|
|
* @param User $user Security user instance. |
46
|
|
|
* @param bool $grantAccessByDefault Default access value if no permission is found. |
47
|
|
|
* @param bool $allowStartTransition Allow start transition. |
48
|
|
|
*/ |
49
|
|
|
public function __construct(User $user, bool $grantAccessByDefault = false, bool $allowStartTransition = true) |
50
|
|
|
{ |
51
|
|
|
parent::__construct($user, $grantAccessByDefault); |
52
|
|
|
|
53
|
|
|
$this->allowStartTransition = $allowStartTransition; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function match(Transition $transition, Item $item, Context $context, ErrorCollection $errorCollection): bool |
60
|
|
|
{ |
61
|
|
|
// workflow is not started, so no start step exists |
62
|
|
|
if (!$item->isWorkflowStarted()) { |
63
|
|
|
if ($this->allowStartTransition) { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$errorCollection->addError( |
68
|
|
|
'transition.condition.step.failed.not-started' |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$permission = $this->getStepPermission($transition, $item); |
75
|
|
|
if ($this->checkPermission($permission)) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$errorCollection->addError( |
80
|
|
|
'transition.condition.step.failed', |
81
|
|
|
array( |
82
|
|
|
$item->getCurrentStepName(), |
83
|
|
|
$permission ? ((string) $permission) : '-' |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get role of current step. |
92
|
|
|
* |
93
|
|
|
* @param Transition $transition The transition being in. |
94
|
|
|
* @param Item $item The entity being transits. |
95
|
|
|
* |
96
|
|
|
* @return Permission|null |
97
|
|
|
*/ |
98
|
|
|
protected function getStepPermission(Transition $transition, Item $item):? Permission |
99
|
|
|
{ |
100
|
|
|
$stepName = $item->getCurrentStepName(); |
101
|
|
|
$step = $transition->getWorkflow()->getStep($stepName); |
102
|
|
|
$permission = $step->getPermission(); |
103
|
|
|
|
104
|
|
|
return $permission; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|