1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Casbin\Model; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
use Casbin\Constant\Constants; |
9
|
|
|
use Casbin\Exceptions\CasbinException; |
10
|
|
|
use Casbin\Log\Log; |
11
|
|
|
use Casbin\Rbac\RoleManager; |
12
|
|
|
use Casbin\Util\Util; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Policy. |
16
|
|
|
* |
17
|
|
|
* @package Casbin\Model |
|
|
|
|
18
|
|
|
* @implements ArrayAccess<string, array<string, Assertion>> |
19
|
|
|
* @author [email protected] |
|
|
|
|
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
abstract class Policy implements ArrayAccess |
22
|
|
|
{ |
23
|
|
|
public const POLICY_ADD = 0; |
24
|
|
|
|
25
|
|
|
public const POLICY_REMOVE = 1; |
26
|
|
|
|
27
|
|
|
const DEFAULT_SEP = ","; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* All of the Model items. |
31
|
|
|
* |
32
|
|
|
* @var array<string, array<string, Assertion>> |
33
|
|
|
*/ |
34
|
|
|
protected $items = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* BuildIncrementalRoleLinks provides incremental build the role inheritance relations. |
38
|
|
|
* |
39
|
|
|
* @param RoleManager[] $rmMap |
|
|
|
|
40
|
|
|
* @param integer $op |
|
|
|
|
41
|
|
|
* @param string $sec |
|
|
|
|
42
|
|
|
* @param string $ptype |
|
|
|
|
43
|
|
|
* @param string[][] $rules |
|
|
|
|
44
|
|
|
* @return void |
|
|
|
|
45
|
|
|
*/ |
46
|
39 |
|
public function buildIncrementalRoleLinks(array $rmMap, int $op, string $sec, string $ptype, array $rules): void |
47
|
|
|
{ |
48
|
39 |
|
if ($sec == "g") { |
49
|
39 |
|
$this->items[$sec][$ptype]->buildIncrementalRoleLinks($rmMap[$ptype], $op, $rules); |
50
|
|
|
} |
51
|
26 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initializes the roles in RBAC. |
55
|
|
|
* |
56
|
|
|
* @param RoleManager[] $rmMap |
|
|
|
|
57
|
|
|
* @throws CasbinException |
|
|
|
|
58
|
|
|
*/ |
|
|
|
|
59
|
330 |
|
public function buildRoleLinks(array $rmMap): void |
60
|
|
|
{ |
61
|
330 |
|
if (!isset($this->items['g'])) { |
62
|
105 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
228 |
|
foreach ($this->items['g'] as $ptype => $ast) { |
66
|
228 |
|
$rm = $rmMap[$ptype]; |
67
|
228 |
|
$ast->buildRoleLinks($rm); |
68
|
|
|
} |
69
|
152 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Prints the policy to log. |
73
|
|
|
*/ |
|
|
|
|
74
|
330 |
|
public function printPolicy(): void |
75
|
|
|
{ |
76
|
330 |
|
Log::logPrint('Policy:'); |
77
|
|
|
|
78
|
330 |
|
foreach (['p', 'g'] as $sec) { |
79
|
330 |
|
if (!isset($this->items[$sec])) { |
80
|
105 |
|
return; |
81
|
|
|
} |
82
|
330 |
|
foreach ($this->items[$sec] as $key => $ast) { |
83
|
330 |
|
Log::logPrint($key, ': ', $ast->value, ': ', $ast->policy); |
84
|
|
|
} |
85
|
|
|
} |
86
|
152 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Clears all current policy. |
90
|
|
|
*/ |
|
|
|
|
91
|
354 |
|
public function clearPolicy(): void |
92
|
|
|
{ |
93
|
354 |
|
foreach (['p', 'g'] as $sec) { |
94
|
354 |
|
if (!isset($this->items[$sec])) { |
95
|
120 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
354 |
|
foreach ($this->items[$sec] as $key => $ast) { |
99
|
354 |
|
$this->items[$sec][$key]->policy = []; |
100
|
354 |
|
$this->items[$sec][$key]->policyMap = []; |
101
|
|
|
} |
102
|
|
|
} |
103
|
158 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets all rules in a policy. |
107
|
|
|
* |
108
|
|
|
* @param string $sec |
|
|
|
|
109
|
|
|
* @param string $ptype |
|
|
|
|
110
|
|
|
* |
111
|
|
|
* @return string[][] |
112
|
|
|
*/ |
113
|
21 |
|
public function getPolicy(string $sec, string $ptype): array |
114
|
|
|
{ |
115
|
21 |
|
return $this->items[$sec][$ptype]->policy; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets rules based on field filters from a policy. |
120
|
|
|
* |
121
|
|
|
* @param string $sec |
|
|
|
|
122
|
|
|
* @param string $ptype |
|
|
|
|
123
|
|
|
* @param int $fieldIndex |
|
|
|
|
124
|
|
|
* @param string ...$fieldValues |
|
|
|
|
125
|
|
|
* |
126
|
|
|
* @return string[][] |
127
|
|
|
*/ |
128
|
21 |
|
public function getFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): array |
129
|
|
|
{ |
130
|
21 |
|
$res = []; |
131
|
|
|
|
132
|
21 |
|
foreach ($this->items[$sec][$ptype]->policy as $rule) { |
133
|
21 |
|
$matched = true; |
134
|
21 |
|
foreach ($fieldValues as $i => $fieldValue) { |
135
|
21 |
|
if ('' != $fieldValue && $rule[$fieldIndex + intval($i)] != $fieldValue) { |
136
|
21 |
|
$matched = false; |
137
|
|
|
|
138
|
21 |
|
break; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
21 |
|
if ($matched) { |
143
|
21 |
|
$res[] = $rule; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
21 |
|
return $res; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determines whether a model has the specified policy rule. |
152
|
|
|
* |
153
|
|
|
* @param string $sec |
|
|
|
|
154
|
|
|
* @param string $ptype |
|
|
|
|
155
|
|
|
* @param string[] $rule |
|
|
|
|
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
111 |
|
public function hasPolicy(string $sec, string $ptype, array $rule): bool |
160
|
|
|
{ |
161
|
111 |
|
if (!isset($this->items[$sec][$ptype])) { |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
111 |
|
return isset($this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $rule)]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Determines whether a model has any of the specified policies. If one is found we return true. |
170
|
|
|
* |
171
|
|
|
* @param string $sec |
|
|
|
|
172
|
|
|
* @param string $ptype |
|
|
|
|
173
|
|
|
* @param string[][] $rules |
|
|
|
|
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
45 |
|
public function hasPolicies(string $sec, string $ptype, array $rules): bool |
178
|
|
|
{ |
179
|
45 |
|
foreach ($rules as $rule) { |
180
|
45 |
|
if ($this->hasPolicy($sec, $ptype, $rule)) { |
181
|
37 |
|
return true; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
36 |
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Adds a policy rule to the model. |
190
|
|
|
* |
191
|
|
|
* @param string $sec |
|
|
|
|
192
|
|
|
* @param string $ptype |
|
|
|
|
193
|
|
|
* @param string[] $rule |
|
|
|
|
194
|
|
|
*/ |
|
|
|
|
195
|
99 |
|
public function addPolicy(string $sec, string $ptype, array $rule): void |
196
|
|
|
{ |
197
|
99 |
|
$assertion = &$this->items[$sec][$ptype]; |
198
|
99 |
|
$assertion->policy[] = $rule; |
199
|
99 |
|
$assertion->policyMap[implode(self::DEFAULT_SEP, $rule)] = count($this->items[$sec][$ptype]->policy) - 1; |
200
|
|
|
|
201
|
99 |
|
$hasPriority = isset($assertion->fieldIndexMap[Constants::PRIORITY_INDEX]); |
202
|
99 |
|
if ($sec == 'p' && $hasPriority) { |
203
|
6 |
|
$idxInsert = $rule[$assertion->fieldIndexMap[Constants::PRIORITY_INDEX]]; |
204
|
6 |
|
for ($i = count($assertion->policy) - 1; $i > 0; $i--) { |
205
|
6 |
|
$idx = $assertion->policy[$i - 1][$assertion->fieldIndexMap[Constants::PRIORITY_INDEX]]; |
206
|
6 |
|
if ($idx > $idxInsert) { |
207
|
6 |
|
$assertion->policy[$i] = $assertion->policy[$i - 1]; |
208
|
6 |
|
$assertion->policyMap[implode(self::DEFAULT_SEP, $assertion->policy[$i - 1])]++; |
209
|
|
|
} else { |
210
|
6 |
|
break; |
211
|
|
|
} |
212
|
|
|
} |
213
|
6 |
|
$assertion->policy[$i] = $rule; |
214
|
6 |
|
$assertion->policyMap[implode(self::DEFAULT_SEP, $rule)] = $i; |
215
|
|
|
} |
216
|
66 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Adds a policy rules to the model. |
220
|
|
|
* |
221
|
|
|
* @param string $sec |
|
|
|
|
222
|
|
|
* @param string $ptype |
|
|
|
|
223
|
|
|
* @param string[][] $rules |
|
|
|
|
224
|
|
|
*/ |
|
|
|
|
225
|
36 |
|
public function addPolicies(string $sec, string $ptype, array $rules): void |
226
|
|
|
{ |
227
|
36 |
|
$this->addPoliciesWithAffected($sec, $ptype, $rules); |
228
|
24 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Adds policy rules to the model, and returns affected rules. |
232
|
|
|
* |
233
|
|
|
* @param string $sec |
|
|
|
|
234
|
|
|
* @param string $ptype |
|
|
|
|
235
|
|
|
* @param string[][] $rules |
|
|
|
|
236
|
|
|
* |
237
|
|
|
* @return string[][] |
238
|
|
|
*/ |
239
|
36 |
|
public function addPoliciesWithAffected(string $sec, string $ptype, array $rules): array |
240
|
|
|
{ |
241
|
36 |
|
$affected = []; |
242
|
|
|
|
243
|
36 |
|
foreach ($rules as $rule) { |
244
|
36 |
|
$hashKey = implode(self::DEFAULT_SEP, $rule); |
245
|
36 |
|
if (isset($this->items[$sec][$ptype]->policyMap[$hashKey])) { |
246
|
12 |
|
continue; |
247
|
|
|
} |
248
|
|
|
|
249
|
36 |
|
$affected[] = $rule; |
250
|
36 |
|
$this->addPolicy($sec, $ptype, $rule); |
251
|
|
|
} |
252
|
|
|
|
253
|
36 |
|
return $affected; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Updates a policy rule from the model. |
258
|
|
|
* |
259
|
|
|
* @param string $sec |
|
|
|
|
260
|
|
|
* @param string $ptype |
|
|
|
|
261
|
|
|
* @param string[] $oldRule |
|
|
|
|
262
|
|
|
* @param string[] $newRule |
|
|
|
|
263
|
|
|
* |
264
|
|
|
* @return bool |
265
|
|
|
*/ |
266
|
6 |
|
public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newRule): bool |
267
|
|
|
{ |
268
|
6 |
|
$oldPolicy = implode(self::DEFAULT_SEP, $oldRule); |
269
|
6 |
|
if (!isset($this->items[$sec][$ptype]->policyMap[$oldPolicy])) { |
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
6 |
|
$index = $this->items[$sec][$ptype]->policyMap[$oldPolicy]; |
274
|
6 |
|
$this->items[$sec][$ptype]->policy[$index] = $newRule; |
275
|
6 |
|
unset($this->items[$sec][$ptype]->policyMap[$oldPolicy]); |
276
|
6 |
|
$this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $newRule)] = $index; |
277
|
|
|
|
278
|
6 |
|
return true; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* UpdatePolicies updates a policy rule from the model. |
283
|
|
|
* |
284
|
|
|
* @param string $sec |
|
|
|
|
285
|
|
|
* @param string $ptype |
|
|
|
|
286
|
|
|
* @param string[][] $oldRules |
|
|
|
|
287
|
|
|
* @param string[][] $newRules |
|
|
|
|
288
|
|
|
* @return boolean |
|
|
|
|
289
|
|
|
*/ |
290
|
6 |
|
public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): bool |
291
|
|
|
{ |
292
|
6 |
|
$modifiedRuleIndex = []; |
293
|
|
|
|
294
|
6 |
|
$newIndex = 0; |
295
|
6 |
|
foreach ($oldRules as $oldIndex => $oldRule) { |
296
|
6 |
|
$oldPolicy = implode(self::DEFAULT_SEP, $oldRule); |
297
|
6 |
|
$index = $this->items[$sec][$ptype]->policyMap[$oldPolicy] ?? null; |
298
|
6 |
|
if (is_null($index)) { |
299
|
|
|
// rollback |
300
|
6 |
|
foreach ($modifiedRuleIndex as $index => $oldNewIndex) { |
301
|
3 |
|
$this->items[$sec][$ptype]->policy[$index] = $oldRules[$oldNewIndex[0]]; |
302
|
3 |
|
$oldPolicy = implode(self::DEFAULT_SEP, $oldRules[$oldNewIndex[0]]); |
303
|
3 |
|
$newPolicy = implode(self::DEFAULT_SEP, $newRules[$oldNewIndex[1]]); |
304
|
3 |
|
unset($this->items[$sec][$ptype]->policyMap[$newPolicy]); |
305
|
3 |
|
$this->items[$sec][$ptype]->policyMap[$oldPolicy] = $index; |
306
|
|
|
} |
307
|
6 |
|
return false; |
308
|
|
|
} |
309
|
|
|
|
310
|
6 |
|
$this->items[$sec][$ptype]->policy[$index] = $newRules[$newIndex]; |
311
|
6 |
|
unset($this->items[$sec][$ptype]->policyMap[$oldPolicy]); |
312
|
6 |
|
$this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $newRules[$newIndex])] = $index; |
313
|
6 |
|
$modifiedRuleIndex[$index] = [$oldIndex, $newIndex]; |
314
|
6 |
|
$newIndex++; |
315
|
|
|
} |
316
|
|
|
|
317
|
6 |
|
return true; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Removes a policy rule from the model. |
322
|
|
|
* |
323
|
|
|
* @param string $sec |
|
|
|
|
324
|
|
|
* @param string $ptype |
|
|
|
|
325
|
|
|
* @param array $rule |
|
|
|
|
326
|
|
|
* |
327
|
|
|
* @return bool |
328
|
|
|
*/ |
329
|
51 |
|
public function removePolicy(string $sec, string $ptype, array $rule): bool |
330
|
|
|
{ |
331
|
51 |
|
if (!isset($this->items[$sec][$ptype])) { |
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
51 |
|
$hashKey = implode(self::DEFAULT_SEP, $rule); |
336
|
51 |
|
if (!isset($this->items[$sec][$ptype]->policyMap[$hashKey])) { |
337
|
9 |
|
return false; |
338
|
|
|
} |
339
|
|
|
|
340
|
51 |
|
$index = $this->items[$sec][$ptype]->policyMap[$hashKey]; |
341
|
51 |
|
array_splice($this->items[$sec][$ptype]->policy, $index, 1); |
342
|
|
|
|
343
|
51 |
|
unset($this->items[$sec][$ptype]->policyMap[$hashKey]); |
344
|
|
|
|
345
|
51 |
|
$count = count($this->items[$sec][$ptype]->policy); |
346
|
51 |
|
for ($i = $index; $i < $count; $i++) { |
347
|
39 |
|
$this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $this->items[$sec][$ptype]->policy[$i])] = $i; |
348
|
|
|
} |
349
|
|
|
|
350
|
51 |
|
return true; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Removes a policy rules from the model. |
355
|
|
|
* |
356
|
|
|
* @param string $sec |
|
|
|
|
357
|
|
|
* @param string $ptype |
|
|
|
|
358
|
|
|
* @param string[][] $rules |
|
|
|
|
359
|
|
|
* |
360
|
|
|
* @return bool |
361
|
|
|
*/ |
362
|
21 |
|
public function removePolicies(string $sec, string $ptype, array $rules): bool |
363
|
|
|
{ |
364
|
21 |
|
if (!isset($this->items[$sec][$ptype])) { |
365
|
|
|
return false; |
366
|
|
|
} |
367
|
|
|
|
368
|
21 |
|
foreach ($rules as $rule) { |
369
|
21 |
|
$this->removePolicy($sec, $ptype, $rule); |
370
|
|
|
} |
371
|
|
|
|
372
|
21 |
|
return true; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Removes policy rules based on field filters from the model. |
377
|
|
|
* |
378
|
|
|
* @param string $sec |
|
|
|
|
379
|
|
|
* @param string $ptype |
|
|
|
|
380
|
|
|
* @param int $fieldIndex |
|
|
|
|
381
|
|
|
* @param string ...$fieldValues |
|
|
|
|
382
|
|
|
* |
383
|
|
|
* If more than one rule is removed, return the removed rule array, otherwise return false |
384
|
|
|
* @return string[][]|false |
|
|
|
|
385
|
|
|
*/ |
386
|
27 |
|
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues) |
387
|
|
|
{ |
388
|
27 |
|
$tmp = []; |
389
|
27 |
|
$effects = []; |
390
|
27 |
|
$res = false; |
391
|
|
|
|
392
|
27 |
|
if (!isset($this->items[$sec][$ptype])) { |
393
|
3 |
|
return $res; |
394
|
|
|
} |
395
|
|
|
|
396
|
27 |
|
$this->items[$sec][$ptype]->policyMap = []; |
397
|
|
|
|
398
|
27 |
|
foreach ($this->items[$sec][$ptype]->policy as $index => $rule) { |
399
|
27 |
|
$matched = true; |
400
|
27 |
|
foreach ($fieldValues as $i => $fieldValue) { |
401
|
27 |
|
if ('' != $fieldValue && $rule[$fieldIndex + intval($i)] != $fieldValue) { |
402
|
21 |
|
$matched = false; |
403
|
25 |
|
break; |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
27 |
|
if ($matched) { |
408
|
27 |
|
$effects[] = $rule; |
409
|
|
|
} else { |
410
|
21 |
|
$tmp[] = $rule; |
411
|
25 |
|
$this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $rule)] = count($tmp) - 1; |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|
415
|
27 |
|
if (count($tmp) != count($this->items[$sec][$ptype]->policy)) { |
416
|
27 |
|
$this->items[$sec][$ptype]->policy = $tmp; |
417
|
27 |
|
$res = true; |
418
|
|
|
} |
419
|
|
|
|
420
|
27 |
|
return $res ? $effects : false; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Gets all values for a field for all rules in a policy, duplicated values are removed. |
425
|
|
|
* |
426
|
|
|
* @param string $sec |
|
|
|
|
427
|
|
|
* @param string $ptype |
|
|
|
|
428
|
|
|
* @param int $fieldIndex |
|
|
|
|
429
|
|
|
* |
430
|
|
|
* @return string[] |
431
|
|
|
*/ |
432
|
27 |
|
public function getValuesForFieldInPolicy(string $sec, string $ptype, int $fieldIndex): array |
433
|
|
|
{ |
434
|
27 |
|
$values = []; |
435
|
|
|
|
436
|
27 |
|
if (!isset($this->items[$sec][$ptype])) { |
437
|
6 |
|
return $values; |
438
|
|
|
} |
439
|
|
|
|
440
|
27 |
|
foreach ($this->items[$sec][$ptype]->policy as $rule) { |
441
|
27 |
|
$values[] = $rule[$fieldIndex]; |
442
|
|
|
} |
443
|
|
|
|
444
|
27 |
|
Util::arrayRemoveDuplicates($values); |
445
|
|
|
|
446
|
27 |
|
return $values; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed. |
451
|
|
|
* |
452
|
|
|
* @param string $sec |
|
|
|
|
453
|
|
|
* @param int $fieldIndex |
|
|
|
|
454
|
|
|
* |
455
|
|
|
* @return string[] |
456
|
|
|
*/ |
457
|
12 |
|
public function getValuesForFieldInPolicyAllTypes(string $sec, int $fieldIndex): array |
458
|
|
|
{ |
459
|
12 |
|
$values = []; |
460
|
|
|
|
461
|
12 |
|
foreach ($this->items[$sec] as $key => $ptype) { |
462
|
12 |
|
$values = array_merge($values, $this->getValuesForFieldInPolicy($sec, $key, $fieldIndex)); |
463
|
|
|
} |
464
|
|
|
|
465
|
12 |
|
Util::arrayRemoveDuplicates($values); |
466
|
|
|
|
467
|
12 |
|
return $values; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed. |
472
|
|
|
* |
473
|
|
|
* @param string $sec |
|
|
|
|
474
|
|
|
* @param string $field |
|
|
|
|
475
|
|
|
* |
476
|
|
|
* @return array<string> |
477
|
|
|
* @throws CasbinException |
478
|
|
|
*/ |
479
|
9 |
|
public function getValuesForFieldInPolicyAllTypesByName(string $sec, string $field): array |
480
|
|
|
{ |
481
|
9 |
|
$values = []; |
482
|
|
|
|
483
|
9 |
|
foreach ($this->items[$sec] as $ptype => $rules) { |
484
|
9 |
|
$index = $this->getFieldIndex($ptype, $field); |
485
|
9 |
|
$v = $this->getValuesForFieldInPolicy($sec, $ptype, $index); |
486
|
|
|
|
487
|
9 |
|
$values = array_merge($values, $v); |
488
|
|
|
} |
489
|
|
|
|
490
|
9 |
|
Util::arrayRemoveDuplicates($values); |
491
|
|
|
|
492
|
9 |
|
return $values; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Gets the index for a given ptype and field. |
497
|
|
|
* |
498
|
|
|
* @param string $ptype |
|
|
|
|
499
|
|
|
* @param string $field |
|
|
|
|
500
|
|
|
* |
501
|
|
|
* @return int $fieldIndex |
502
|
|
|
* @throws CasbinException |
503
|
|
|
*/ |
504
|
330 |
|
public function getFieldIndex(string $ptype, string $field): int |
505
|
|
|
{ |
506
|
330 |
|
$assertion = &$this->items['p'][$ptype]; |
507
|
330 |
|
if (isset($assertion->fieldIndexMap[$field])) { |
508
|
39 |
|
return $assertion->fieldIndexMap[$field]; |
509
|
|
|
} |
510
|
330 |
|
$pattern = $ptype . '_' . $field; |
511
|
330 |
|
$index = -1; |
512
|
330 |
|
foreach ($assertion->tokens as $i => $token) { |
513
|
330 |
|
if ($token == $pattern) { |
514
|
66 |
|
$index = $i; |
515
|
242 |
|
break; |
516
|
|
|
} |
517
|
|
|
} |
518
|
330 |
|
if ($index == -1) { |
519
|
327 |
|
throw new CasbinException($field . ' index is not set, please use enforcer.SetFieldIndex() to set index'); |
520
|
|
|
} |
521
|
66 |
|
$assertion->fieldIndexMap[$field] = $index; |
522
|
66 |
|
return $index; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Sets the index for a given ptype and field. |
527
|
|
|
* |
528
|
|
|
* @param string $ptype |
|
|
|
|
529
|
|
|
* @param string $field |
|
|
|
|
530
|
|
|
* @param int $index |
|
|
|
|
531
|
|
|
*/ |
|
|
|
|
532
|
3 |
|
public function setFieldIndex(string $ptype, string $field, int $index): void |
533
|
|
|
{ |
534
|
3 |
|
$assertion = &$this->items['p'][$ptype]; |
535
|
3 |
|
$assertion->fieldIndexMap[$field] = $index; |
536
|
2 |
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Determine if the given Model option exists. |
540
|
|
|
* |
541
|
|
|
* @param string $offset |
|
|
|
|
542
|
|
|
* |
543
|
|
|
* @return bool |
544
|
|
|
*/ |
545
|
363 |
|
public function offsetExists($offset): bool |
546
|
|
|
{ |
547
|
363 |
|
return isset($this->items[$offset]); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Get a Model option. |
552
|
|
|
* |
553
|
|
|
* @param string $offset |
|
|
|
|
554
|
|
|
* |
555
|
|
|
* @return array<string, Assertion>|null |
556
|
|
|
*/ |
557
|
363 |
|
public function offsetGet($offset): ?array |
558
|
|
|
{ |
559
|
363 |
|
return $this->items[$offset] ?? null; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Set a Model option. |
564
|
|
|
* |
565
|
|
|
* @param string $offset |
|
|
|
|
566
|
|
|
* @param array<string, Assertion> $value |
|
|
|
|
567
|
|
|
*/ |
|
|
|
|
568
|
327 |
|
public function offsetSet($offset, $value): void |
569
|
|
|
{ |
570
|
327 |
|
$this->items[$offset] = $value; |
571
|
218 |
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Unset a Model option. |
575
|
|
|
* |
576
|
|
|
* @param string $offset |
|
|
|
|
577
|
|
|
*/ |
|
|
|
|
578
|
|
|
public function offsetUnset($offset): void |
579
|
|
|
{ |
580
|
|
|
unset($this->items[$offset]); |
581
|
|
|
} |
582
|
|
|
} |
583
|
|
|
|