|
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\ObligationInterface; |
|
23
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\PolicyInterface; |
|
24
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\RuleCombiningAlgorithmInterface; |
|
25
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\RuleInterface; |
|
26
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\TargetInterface; |
|
27
|
|
|
use function assert; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @package Limoncello\Auth |
|
31
|
|
|
*/ |
|
32
|
|
|
class Policy implements PolicyInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $name; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var TargetInterface|null |
|
41
|
|
|
*/ |
|
42
|
|
|
private $target; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var RuleInterface[] |
|
46
|
|
|
*/ |
|
47
|
|
|
private $rules; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var RuleCombiningAlgorithmInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $combiningAlgorithm; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var ObligationInterface[] |
|
56
|
|
|
*/ |
|
57
|
|
|
private $obligations; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var AdviceInterface[] |
|
61
|
|
|
*/ |
|
62
|
|
|
private $advice; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param RuleInterface[] $rules |
|
66
|
|
|
* @param RuleCombiningAlgorithmInterface $combiningAlgorithm |
|
67
|
|
|
* @param null|string $name |
|
68
|
|
|
* @param TargetInterface|null $target |
|
69
|
24 |
|
* @param ObligationInterface[] $obligations |
|
70
|
|
|
* @param AdviceInterface[] $advice |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct( |
|
73
|
|
|
array $rules, |
|
74
|
|
|
RuleCombiningAlgorithmInterface $combiningAlgorithm, |
|
75
|
|
|
string $name = null, |
|
76
|
|
|
TargetInterface $target = null, |
|
77
|
24 |
|
array $obligations = [], |
|
78
|
24 |
|
array $advice = [] |
|
79
|
|
|
) { |
|
80
|
|
|
$this->setName($name)->setTarget($target)->setRules($rules)->setCombiningAlgorithm($combiningAlgorithm) |
|
81
|
|
|
->setObligations($obligations)->setAdvice($advice); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
24 |
|
/** |
|
85
|
|
|
* @inheritdoc |
|
86
|
24 |
|
*/ |
|
87
|
|
|
public function getName(): ?string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->name; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param null|string $name |
|
94
|
24 |
|
* |
|
95
|
|
|
* @return self |
|
96
|
24 |
|
*/ |
|
97
|
|
|
public function setName(?string $name): self |
|
98
|
24 |
|
{ |
|
99
|
|
|
$this->name = $name; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
24 |
|
/** |
|
105
|
|
|
* @inheritdoc |
|
106
|
24 |
|
*/ |
|
107
|
|
|
public function getTarget(): ?TargetInterface |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->target; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param TargetInterface|null $target |
|
114
|
24 |
|
* |
|
115
|
|
|
* @return self |
|
116
|
24 |
|
*/ |
|
117
|
|
|
public function setTarget(?TargetInterface $target): self |
|
118
|
24 |
|
{ |
|
119
|
|
|
$this->target = $target; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
24 |
|
/** |
|
125
|
|
|
* @inheritdoc |
|
126
|
24 |
|
*/ |
|
127
|
|
|
public function getRules(): array |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->rules; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param RuleInterface[] $rules |
|
134
|
24 |
|
* |
|
135
|
|
|
* @return self |
|
136
|
24 |
|
*/ |
|
137
|
|
|
public function setRules(array $rules): self |
|
138
|
24 |
|
{ |
|
139
|
|
|
assert(empty($rules) === false); |
|
140
|
24 |
|
|
|
141
|
|
|
$this->rules = $rules; |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
24 |
|
/** |
|
147
|
|
|
* @inheritdoc |
|
148
|
24 |
|
*/ |
|
149
|
|
|
public function getCombiningAlgorithm(): RuleCombiningAlgorithmInterface |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->combiningAlgorithm; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param RuleCombiningAlgorithmInterface $combiningAlgorithm |
|
156
|
24 |
|
* |
|
157
|
|
|
* @return self |
|
158
|
24 |
|
*/ |
|
159
|
|
|
public function setCombiningAlgorithm(RuleCombiningAlgorithmInterface $combiningAlgorithm): self |
|
160
|
24 |
|
{ |
|
161
|
|
|
$this->combiningAlgorithm = $combiningAlgorithm; |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
24 |
|
/** |
|
167
|
|
|
* @inheritdoc |
|
168
|
24 |
|
*/ |
|
169
|
|
|
public function getObligations(): array |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->obligations; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param ObligationInterface[] $obligations |
|
176
|
24 |
|
* |
|
177
|
|
|
* @return self |
|
178
|
24 |
|
*/ |
|
179
|
|
|
public function setObligations(array $obligations): self |
|
180
|
24 |
|
{ |
|
181
|
|
|
$this->obligations = $obligations; |
|
182
|
|
|
|
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
24 |
|
/** |
|
187
|
|
|
* @inheritdoc |
|
188
|
24 |
|
*/ |
|
189
|
|
|
public function getAdvice(): array |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->advice; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param AdviceInterface[] $advice |
|
196
|
24 |
|
* |
|
197
|
|
|
* @return self |
|
198
|
24 |
|
*/ |
|
199
|
|
|
public function setAdvice(array $advice): self |
|
200
|
24 |
|
{ |
|
201
|
|
|
$this->advice = $advice; |
|
202
|
|
|
|
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|