1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Casbin\Model; |
6
|
|
|
|
7
|
|
|
use Casbin\Exceptions\BatchOperationException; |
8
|
|
|
use Casbin\Exceptions\CasbinException; |
9
|
|
|
use Casbin\Log\Log; |
10
|
|
|
use Casbin\Rbac\RoleManager; |
11
|
|
|
use Casbin\Util\Util; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait Policy. |
15
|
|
|
* |
16
|
|
|
* @author [email protected] |
|
|
|
|
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
trait Policy |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* initializes the roles in RBAC. |
|
|
|
|
22
|
|
|
* |
23
|
|
|
* @param RoleManager $rm |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
174 |
|
public function buildRoleLinks(RoleManager $rm): void |
26
|
|
|
{ |
27
|
174 |
|
if (!isset($this->items['g'])) { |
28
|
81 |
|
return; |
29
|
|
|
} |
30
|
93 |
|
foreach ($this->items['g'] as $ast) { |
31
|
93 |
|
$ast->buildRoleLinks($rm); |
32
|
|
|
} |
33
|
93 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* prints the policy to log. |
|
|
|
|
37
|
|
|
*/ |
|
|
|
|
38
|
174 |
|
public function printPolicy(): void |
39
|
|
|
{ |
40
|
174 |
|
Log::logPrint('Policy:'); |
41
|
174 |
|
foreach (['p', 'g'] as $sec) { |
42
|
174 |
|
if (!isset($this->items[$sec])) { |
43
|
81 |
|
return; |
44
|
|
|
} |
45
|
174 |
|
foreach ($this->items[$sec] as $key => $ast) { |
46
|
174 |
|
Log::logPrint($key, ': ', $ast->value, ': ', $ast->policy); |
47
|
|
|
} |
48
|
|
|
} |
49
|
93 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* clears all current policy. |
|
|
|
|
53
|
|
|
*/ |
|
|
|
|
54
|
177 |
|
public function clearPolicy(): void |
55
|
|
|
{ |
56
|
177 |
|
foreach (['p', 'g'] as $sec) { |
57
|
177 |
|
if (!isset($this->items[$sec])) { |
58
|
81 |
|
return; |
59
|
|
|
} |
60
|
177 |
|
foreach ($this->items[$sec] as $key => $ast) { |
61
|
177 |
|
$this->items[$sec][$key]->policy = []; |
62
|
|
|
} |
63
|
|
|
} |
64
|
96 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* gets all rules in a policy. |
|
|
|
|
68
|
|
|
* |
69
|
|
|
* @param string $sec |
|
|
|
|
70
|
|
|
* @param string $ptype |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
9 |
|
public function getPolicy(string $sec, string $ptype): array |
75
|
|
|
{ |
76
|
9 |
|
return $this->items[$sec][$ptype]->policy; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* gets rules based on field filters from a policy. |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @param string $sec |
|
|
|
|
83
|
|
|
* @param string $ptype |
|
|
|
|
84
|
|
|
* @param int $fieldIndex |
|
|
|
|
85
|
|
|
* @param string ...$fieldValues |
|
|
|
|
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
15 |
|
public function getFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): array |
90
|
|
|
{ |
91
|
15 |
|
$res = []; |
92
|
|
|
|
93
|
15 |
|
foreach ($this->items[$sec][$ptype]->policy as $rule) { |
94
|
15 |
|
$matched = true; |
95
|
15 |
|
foreach ($fieldValues as $i => $fieldValue) { |
96
|
15 |
|
if ('' != $fieldValue && $rule[$fieldIndex + $i] != $fieldValue) { |
97
|
15 |
|
$matched = false; |
98
|
|
|
|
99
|
15 |
|
break; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
15 |
|
if ($matched) { |
104
|
15 |
|
$res[] = $rule; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
15 |
|
return $res; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* determines whether a model has the specified policy rule. |
|
|
|
|
113
|
|
|
* |
114
|
|
|
* @param string $sec |
|
|
|
|
115
|
|
|
* @param string $ptype |
|
|
|
|
116
|
|
|
* @param array $rule |
|
|
|
|
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
48 |
|
public function hasPolicy(string $sec, string $ptype, array $rule): bool |
121
|
|
|
{ |
122
|
48 |
|
if (!isset($this->items[$sec][$ptype])) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
48 |
|
return in_array($rule, $this->items[$sec][$ptype]->policy, true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* determines whether a model has any of the specified policies. If one is found we return false. |
|
|
|
|
131
|
|
|
* |
132
|
|
|
* @param string $sec |
|
|
|
|
133
|
|
|
* @param string $ptype |
|
|
|
|
134
|
|
|
* @param array $rules |
|
|
|
|
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
6 |
|
public function hasPolicies(string $sec, string $ptype, array $rules): bool |
139
|
|
|
{ |
140
|
6 |
|
if (!isset($this->items[$sec][$ptype])) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
6 |
|
foreach ($rules as $rule) { |
145
|
6 |
|
if (!in_array($rule, $this->items[$sec][$ptype]->policy, true)) { |
146
|
6 |
|
return false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
6 |
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* adds a policy rule to the model. |
|
|
|
|
155
|
|
|
* |
156
|
|
|
* @param string $sec |
|
|
|
|
157
|
|
|
* @param string $ptype |
|
|
|
|
158
|
|
|
* @param array $rule |
|
|
|
|
159
|
|
|
*/ |
|
|
|
|
160
|
54 |
|
public function addPolicy(string $sec, string $ptype, array $rule): void |
161
|
|
|
{ |
162
|
54 |
|
$this->items[$sec][$ptype]->policy[] = $rule; |
163
|
54 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* adds a policy rules to the model. |
|
|
|
|
167
|
|
|
* |
168
|
|
|
* @param string $sec |
|
|
|
|
169
|
|
|
* @param string $ptype |
|
|
|
|
170
|
|
|
* @param array $rules |
|
|
|
|
171
|
|
|
* |
172
|
|
|
* @throws CasbinException |
173
|
|
|
*/ |
|
|
|
|
174
|
6 |
|
public function addPolicies(string $sec, string $ptype, array $rules): void |
175
|
|
|
{ |
176
|
6 |
|
if (count(array_unique($rules, SORT_REGULAR), COUNT_RECURSIVE) !== count($rules, COUNT_RECURSIVE)) { |
177
|
3 |
|
throw new BatchOperationException('addPolicies error: $rules elements can not be duplicated.'); |
178
|
|
|
} |
179
|
|
|
|
180
|
6 |
|
$this->items[$sec][$ptype]->policy = array_merge($this->items[$sec][$ptype]->policy, $rules); |
181
|
6 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* removes a policy rule from the model. |
|
|
|
|
185
|
|
|
* |
186
|
|
|
* @param string $sec |
|
|
|
|
187
|
|
|
* @param string $ptype |
|
|
|
|
188
|
|
|
* @param array $rule |
|
|
|
|
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
30 |
|
public function removePolicy(string $sec, string $ptype, array $rule): bool |
193
|
|
|
{ |
194
|
30 |
|
if (!isset($this->items[$sec][$ptype])) { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
30 |
|
$offset = array_search($rule, $this->items[$sec][$ptype]->policy, true); |
199
|
|
|
|
200
|
30 |
|
if (false === $offset) { |
201
|
6 |
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
30 |
|
array_splice($this->items[$sec][$ptype]->policy, $offset, 1); |
|
|
|
|
205
|
|
|
|
206
|
30 |
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* removes a policy rules from the model. |
|
|
|
|
211
|
|
|
* |
212
|
|
|
* @param string $sec |
|
|
|
|
213
|
|
|
* @param string $ptype |
|
|
|
|
214
|
|
|
* @param array $rules |
|
|
|
|
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
6 |
|
public function removePolicies(string $sec, string $ptype, array $rules): bool |
219
|
|
|
{ |
220
|
6 |
|
if (!isset($this->items[$sec][$ptype])) { |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
6 |
|
$policy = $this->items[$sec][$ptype]->policy; |
225
|
|
|
|
226
|
6 |
|
foreach ($rules as $rule) { |
227
|
6 |
|
$offset = array_search($rule, $policy, true); |
228
|
|
|
|
229
|
6 |
|
if (false === $offset) { |
230
|
6 |
|
return false; |
231
|
|
|
} |
232
|
|
|
|
233
|
6 |
|
array_splice($policy, $offset, 1); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
6 |
|
$this->items[$sec][$ptype]->policy = $policy; |
237
|
|
|
|
238
|
6 |
|
return true; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* removes policy rules based on field filters from the model. |
|
|
|
|
243
|
|
|
* |
244
|
|
|
* @param string $sec |
|
|
|
|
245
|
|
|
* @param string $ptype |
|
|
|
|
246
|
|
|
* @param int $fieldIndex |
|
|
|
|
247
|
|
|
* @param string ...$fieldValues |
|
|
|
|
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
27 |
|
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): bool |
252
|
|
|
{ |
253
|
27 |
|
$tmp = []; |
254
|
27 |
|
$res = false; |
255
|
|
|
|
256
|
27 |
|
if (!isset($this->items[$sec][$ptype])) { |
257
|
|
|
return $res; |
258
|
|
|
} |
259
|
|
|
|
260
|
27 |
|
foreach ($this->items[$sec][$ptype]->policy as $rule) { |
261
|
27 |
|
$matched = true; |
262
|
27 |
|
foreach ($fieldValues as $i => $fieldValue) { |
263
|
27 |
|
if ('' != $fieldValue && $rule[$fieldIndex + $i] != $fieldValue) { |
264
|
18 |
|
$matched = false; |
265
|
|
|
|
266
|
24 |
|
break; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
27 |
|
if ($matched) { |
271
|
27 |
|
$res = true; |
272
|
|
|
} else { |
273
|
24 |
|
$tmp[] = $rule; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
27 |
|
$this->items[$sec][$ptype]->policy = $tmp; |
278
|
|
|
|
279
|
27 |
|
return $res; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* gets all values for a field for all rules in a policy, duplicated values are removed. |
|
|
|
|
284
|
|
|
* |
285
|
|
|
* @param string $sec |
|
|
|
|
286
|
|
|
* @param string $ptype |
|
|
|
|
287
|
|
|
* @param int $fieldIndex |
|
|
|
|
288
|
|
|
* |
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
9 |
|
public function getValuesForFieldInPolicy(string $sec, string $ptype, int $fieldIndex): array |
292
|
|
|
{ |
293
|
9 |
|
$values = []; |
294
|
|
|
|
295
|
9 |
|
if (!isset($this->items[$sec][$ptype])) { |
296
|
|
|
return $values; |
297
|
|
|
} |
298
|
|
|
|
299
|
9 |
|
foreach ($this->items[$sec][$ptype]->policy as $rule) { |
300
|
9 |
|
$values[] = $rule[$fieldIndex]; |
301
|
|
|
} |
302
|
|
|
|
303
|
9 |
|
Util::arrayRemoveDuplicates($values); |
304
|
|
|
|
305
|
9 |
|
return $values; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed. |
|
|
|
|
310
|
|
|
* |
311
|
|
|
* @param string $sec |
|
|
|
|
312
|
|
|
* @param int $fieldIndex |
|
|
|
|
313
|
|
|
* |
314
|
|
|
* @return array |
315
|
|
|
*/ |
316
|
6 |
|
public function getValuesForFieldInPolicyAllTypes(string $sec, int $fieldIndex): array |
317
|
|
|
{ |
318
|
6 |
|
$values = []; |
319
|
|
|
|
320
|
6 |
|
foreach ($this->items[$sec] as $key => $ptype) { |
321
|
6 |
|
$values = array_merge($values, $this->getValuesForFieldInPolicy($sec, $key, $fieldIndex)); |
322
|
|
|
} |
323
|
|
|
|
324
|
6 |
|
Util::arrayRemoveDuplicates($values); |
325
|
|
|
|
326
|
6 |
|
return $values; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|