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\Security; |
16
|
|
|
|
17
|
|
|
use Assert\Assertion; |
18
|
|
|
use Netzmacht\Workflow\Flow\Workflow; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Permission describes a permission in a workflow. |
22
|
|
|
* |
23
|
|
|
* @package Netzmacht\Workflow\Security |
24
|
|
|
*/ |
25
|
|
|
class Permission |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The workflow name. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $workflowName; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The permission id. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $permissionId; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct. |
43
|
|
|
* |
44
|
|
|
* @param string $workflowName The workflow name. |
45
|
|
|
* @param string $permissionId The permission id. |
46
|
|
|
*/ |
47
|
|
|
protected function __construct(string $workflowName, string $permissionId) |
48
|
|
|
{ |
49
|
|
|
$this->workflowName = $workflowName; |
50
|
|
|
$this->permissionId = $permissionId; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a permission for a workflow. |
55
|
|
|
* |
56
|
|
|
* @param Workflow $workflow Workflow to which the permission belongs to. |
57
|
|
|
* @param string $permissionId The permission id. |
58
|
|
|
* |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
|
|
public static function forWorkflow(Workflow $workflow, string $permissionId): self |
62
|
|
|
{ |
63
|
|
|
return static::forWorkflowName($workflow->getName(), $permissionId); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Reconstruct permission from a string representation. |
68
|
|
|
* |
69
|
|
|
* @param string $workflowName Workflow name. |
70
|
|
|
* @param string $permissionId Permission id. |
71
|
|
|
* |
72
|
|
|
* @return static |
73
|
|
|
*/ |
74
|
|
|
public static function forWorkflowName(string $workflowName, string $permissionId): self |
75
|
|
|
{ |
76
|
|
|
Assertion::notBlank($workflowName); |
77
|
|
|
Assertion::notBlank($permissionId); |
78
|
|
|
|
79
|
|
|
self::guardValidPermission($workflowName, $permissionId); |
80
|
|
|
|
81
|
|
|
return new static($workflowName, $permissionId); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Reconstruct permission from a string representation. |
86
|
|
|
* |
87
|
|
|
* @param string $permission Permission string representation. |
88
|
|
|
* |
89
|
|
|
* @return static |
90
|
|
|
*/ |
91
|
|
|
public static function fromString(string $permission): self |
92
|
|
|
{ |
93
|
|
|
list($workflowName, $permissionId) = explode(':', $permission); |
94
|
|
|
|
95
|
|
|
$message = sprintf( |
96
|
|
|
'Invalid permission string given. Expected "workflowName:permissionId, got "%s"".', |
97
|
|
|
$permission |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
self::guardValidPermission($workflowName, $permissionId, $message); |
101
|
|
|
|
102
|
|
|
return new static($workflowName, $permissionId); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the permission id. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getPermissionId(): string |
111
|
|
|
{ |
112
|
|
|
return $this->permissionId; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the workflow name. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getWorkflowName(): string |
121
|
|
|
{ |
122
|
|
|
return $this->workflowName; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Cast permission to a string representation. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function __toString(): string |
131
|
|
|
{ |
132
|
|
|
return $this->workflowName . ':' . $this->permissionId; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Consider if permission equals with another one. |
137
|
|
|
* |
138
|
|
|
* @param Permission $permission Permission to check against. |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function equals(Permission $permission): bool |
143
|
|
|
{ |
144
|
|
|
return ((string) $this === (string) $permission); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Guard that permission values are valid. |
149
|
|
|
* |
150
|
|
|
* @param string $workflowName The workflow name. |
151
|
|
|
* @param string $permissionId The permission id. |
152
|
|
|
* @param string|null $message Optional error message. |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
protected static function guardValidPermission( |
157
|
|
|
string $workflowName, |
158
|
|
|
string $permissionId, |
159
|
|
|
string $message = null |
160
|
|
|
): void { |
161
|
|
|
Assertion::notBlank($workflowName, $message); |
162
|
|
|
Assertion::notBlank($permissionId, $message); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|