|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Auth\Authorization\PolicyAdministration; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2019 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\AdviceInterface; |
|
22
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\MethodInterface; |
|
23
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\ObligationInterface; |
|
24
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\RuleInterface; |
|
25
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\TargetInterface; |
|
26
|
|
|
use function assert; |
|
27
|
|
|
use function call_user_func; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @package Limoncello\Auth |
|
31
|
|
|
*/ |
|
32
|
|
|
class Rule implements RuleInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $name; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var TargetInterface|null |
|
41
|
|
|
*/ |
|
42
|
|
|
private $target; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var MethodInterface|null |
|
46
|
|
|
*/ |
|
47
|
|
|
private $condition; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var MethodInterface|null |
|
51
|
|
|
*/ |
|
52
|
|
|
private $effect; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var ObligationInterface[] |
|
56
|
|
|
*/ |
|
57
|
|
|
private $obligations; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var AdviceInterface[] |
|
61
|
|
|
*/ |
|
62
|
|
|
private $advice; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param null|string $name |
|
66
|
|
|
* @param TargetInterface|null $target |
|
67
|
|
|
* @param MethodInterface|null $condition |
|
68
|
36 |
|
* @param MethodInterface|null $effect |
|
69
|
|
|
* @param ObligationInterface[] $obligations |
|
70
|
|
|
* @param AdviceInterface[] $advice |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct( |
|
73
|
|
|
string $name = null, |
|
74
|
|
|
TargetInterface $target = null, |
|
75
|
|
|
MethodInterface $condition = null, |
|
76
|
36 |
|
MethodInterface $effect = null, |
|
77
|
36 |
|
array $obligations = [], |
|
78
|
|
|
array $advice = [] |
|
79
|
|
|
) { |
|
80
|
|
|
$this->setName($name)->setTarget($target)->setCondition($condition)->setEffect($effect) |
|
|
|
|
|
|
81
|
|
|
->setObligations($obligations)->setAdvice($advice); |
|
82
|
|
|
} |
|
83
|
36 |
|
|
|
84
|
|
|
/** |
|
85
|
36 |
|
* @inheritdoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getName(): ?string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->name; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
36 |
|
* @param null|string $name |
|
94
|
|
|
* |
|
95
|
36 |
|
* @return self |
|
96
|
|
|
*/ |
|
97
|
36 |
|
public function setName(?string $name): self |
|
98
|
|
|
{ |
|
99
|
|
|
$this->name = $name; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
36 |
|
|
|
104
|
|
|
/** |
|
105
|
36 |
|
* @inheritdoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTarget(): ?TargetInterface |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->target; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
36 |
|
* @param TargetInterface|null $target |
|
114
|
|
|
* |
|
115
|
36 |
|
* @return self |
|
116
|
|
|
*/ |
|
117
|
36 |
|
public function setTarget(?TargetInterface $target): self |
|
118
|
|
|
{ |
|
119
|
|
|
$this->target = $target; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
36 |
|
|
|
124
|
|
|
/** |
|
125
|
36 |
|
* @inheritdoc |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getCondition(): ?MethodInterface |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->condition; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
36 |
|
* @param MethodInterface|null $condition |
|
134
|
|
|
* |
|
135
|
36 |
|
* @return self |
|
136
|
|
|
*/ |
|
137
|
36 |
|
public function setCondition(?MethodInterface $condition): self |
|
138
|
|
|
{ |
|
139
|
|
|
$this->condition = $condition; |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
36 |
|
|
|
144
|
|
|
/** |
|
145
|
36 |
|
* @inheritdoc |
|
146
|
|
|
*/ |
|
147
|
|
|
public function effect(): ?MethodInterface |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->effect; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
36 |
|
* @param MethodInterface $effect |
|
154
|
|
|
* |
|
155
|
36 |
|
* @return self |
|
156
|
|
|
*/ |
|
157
|
36 |
|
public function setEffect(?MethodInterface $effect): self |
|
158
|
|
|
{ |
|
159
|
|
|
$this->effect = $effect; |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
36 |
|
|
|
164
|
|
|
/** |
|
165
|
36 |
|
* @inheritdoc |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getObligations(): array |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->obligations; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
36 |
|
* @param ObligationInterface[] $obligations |
|
174
|
|
|
* |
|
175
|
|
|
* @return self |
|
176
|
36 |
|
*/ |
|
177
|
|
|
public function setObligations(array $obligations): self |
|
178
|
36 |
|
{ |
|
179
|
18 |
|
// check every item is Obligation (debug mode only) |
|
180
|
|
|
assert(call_user_func( |
|
181
|
36 |
|
function () use ($obligations) { |
|
182
|
36 |
|
foreach ($obligations as $item) { |
|
183
|
36 |
|
assert($item instanceof ObligationInterface); |
|
184
|
|
|
} |
|
185
|
36 |
|
return true; |
|
186
|
|
|
} |
|
187
|
36 |
|
) === true); |
|
188
|
|
|
|
|
189
|
|
|
$this->obligations = $obligations; |
|
190
|
|
|
|
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
36 |
|
|
|
194
|
|
|
/** |
|
195
|
36 |
|
* @inheritdoc |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getAdvice(): array |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->advice; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
36 |
|
* @param AdviceInterface[] $advice |
|
204
|
|
|
* |
|
205
|
|
|
* @return self |
|
206
|
36 |
|
*/ |
|
207
|
|
|
public function setAdvice(array $advice): self |
|
208
|
36 |
|
{ |
|
209
|
20 |
|
// check every item is Obligation (debug mode only) |
|
210
|
|
|
assert(call_user_func( |
|
211
|
36 |
|
function () use ($advice) { |
|
212
|
36 |
|
foreach ($advice as $item) { |
|
213
|
36 |
|
assert($item instanceof AdviceInterface); |
|
214
|
|
|
} |
|
215
|
36 |
|
return true; |
|
216
|
|
|
} |
|
217
|
36 |
|
) === true); |
|
218
|
|
|
|
|
219
|
|
|
$this->advice = $advice; |
|
220
|
|
|
|
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):