1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Casbin; |
6
|
|
|
|
7
|
|
|
use Casbin\Constant\Constants; |
8
|
|
|
use Closure; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ManagementEnforcer = InternalEnforcer + Management API. |
12
|
|
|
* |
13
|
|
|
* @author [email protected] |
|
|
|
|
14
|
|
|
*/ |
|
|
|
|
15
|
|
|
class ManagementEnforcer extends InternalEnforcer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Gets the list of subjects that show up in the current policy. |
19
|
|
|
* |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
9 |
|
public function getAllSubjects(): array |
23
|
|
|
{ |
24
|
9 |
|
return $this->model->getValuesForFieldInPolicyAllTypesByName('p', Constants::SUBJECT_INDEX); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Gets the list of subjects that show up in the current named policy. |
29
|
|
|
* |
30
|
|
|
* @param string $ptype |
|
|
|
|
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
3 |
|
public function getAllNamedSubjects(string $ptype): array |
35
|
|
|
{ |
36
|
3 |
|
$fieldIndex = $this->model->getFieldIndex('p', Constants::SUBJECT_INDEX); |
37
|
3 |
|
return $this->model->getValuesForFieldInPolicy('p', $ptype, $fieldIndex); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets the list of objects that show up in the current policy. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
6 |
|
public function getAllObjects(): array |
46
|
|
|
{ |
47
|
6 |
|
return $this->model->getValuesForFieldInPolicyAllTypesByName('p', Constants::OBJECT_INDEX); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the list of objects that show up in the current named policy. |
52
|
|
|
* |
53
|
|
|
* @param string $ptype |
|
|
|
|
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
3 |
|
public function getAllNamedObjects(string $ptype): array |
58
|
|
|
{ |
59
|
3 |
|
$fieldIndex = $this->model->getFieldIndex('p', Constants::OBJECT_INDEX); |
60
|
3 |
|
return $this->model->getValuesForFieldInPolicy('p', $ptype, $fieldIndex); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the list of actions that show up in the current policy. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
6 |
|
public function getAllActions(): array |
69
|
|
|
{ |
70
|
6 |
|
return $this->model->getValuesForFieldInPolicyAllTypesByName('p', Constants::ACTION_INDEX); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the list of actions that show up in the current named policy. |
75
|
|
|
* |
76
|
|
|
* @param string $ptype |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
3 |
|
public function getAllNamedActions(string $ptype): array |
81
|
|
|
{ |
82
|
3 |
|
$fieldIndex = $this->model->getFieldIndex('p', Constants::ACTION_INDEX); |
83
|
3 |
|
return $this->model->getValuesForFieldInPolicy('p', $ptype, $fieldIndex); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets the list of roles that show up in the current policy. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
9 |
|
public function getAllRoles(): array |
92
|
|
|
{ |
93
|
9 |
|
return $this->model->getValuesForFieldInPolicyAllTypes('g', 1); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the list of roles that show up in the current named policy. |
98
|
|
|
* |
99
|
|
|
* @param string $ptype |
|
|
|
|
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
3 |
|
public function getAllNamedRoles(string $ptype): array |
104
|
|
|
{ |
105
|
3 |
|
return $this->model->getValuesForFieldInPolicy('g', $ptype, 1); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Gets all the authorization rules in the policy. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
12 |
|
public function getPolicy(): array |
114
|
|
|
{ |
115
|
12 |
|
return $this->getNamedPolicy('p'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets all the authorization rules in the policy, field filters can be specified. |
120
|
|
|
* |
121
|
|
|
* @param int $fieldIndex |
|
|
|
|
122
|
|
|
* @param string ...$fieldValues |
|
|
|
|
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
21 |
|
public function getFilteredPolicy(int $fieldIndex, string ...$fieldValues): array |
127
|
|
|
{ |
128
|
21 |
|
return $this->getFilteredNamedPolicy('p', $fieldIndex, ...$fieldValues); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Gets all the authorization rules in the named policy. |
133
|
|
|
* |
134
|
|
|
* @param string $ptype |
|
|
|
|
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
12 |
|
public function getNamedPolicy(string $ptype): array |
139
|
|
|
{ |
140
|
12 |
|
return $this->model->getPolicy('p', $ptype); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Gets all the authorization rules in the named policy, field filters can be specified. |
145
|
|
|
* |
146
|
|
|
* @param string $ptype |
|
|
|
|
147
|
|
|
* @param int $fieldIndex |
|
|
|
|
148
|
|
|
* @param string ...$fieldValues |
|
|
|
|
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
21 |
|
public function getFilteredNamedPolicy(string $ptype, int $fieldIndex, string ...$fieldValues): array |
153
|
|
|
{ |
154
|
21 |
|
return $this->model->getFilteredPolicy('p', $ptype, $fieldIndex, ...$fieldValues); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gets all the role inheritance rules in the policy. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
9 |
|
public function getGroupingPolicy(): array |
163
|
|
|
{ |
164
|
9 |
|
return $this->getNamedGroupingPolicy('g'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Gets all the role inheritance rules in the policy, field filters can be specified. |
169
|
|
|
* |
170
|
|
|
* @param int $fieldIndex |
|
|
|
|
171
|
|
|
* @param string ...$fieldValues |
|
|
|
|
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
3 |
|
public function getFilteredGroupingPolicy(int $fieldIndex, string ...$fieldValues): array |
176
|
|
|
{ |
177
|
3 |
|
return $this->getFilteredNamedGroupingPolicy('g', $fieldIndex, ...$fieldValues); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Gets all the role inheritance rules in the policy. |
182
|
|
|
* |
183
|
|
|
* @param string $ptype |
|
|
|
|
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
9 |
|
public function getNamedGroupingPolicy(string $ptype): array |
188
|
|
|
{ |
189
|
9 |
|
return $this->model->getPolicy('g', $ptype); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Gets all the role inheritance rules in the policy, field filters can be specified. |
194
|
|
|
* |
195
|
|
|
* @param string $ptype |
|
|
|
|
196
|
|
|
* @param int $fieldIndex |
|
|
|
|
197
|
|
|
* @param string ...$fieldValues |
|
|
|
|
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
3 |
|
public function getFilteredNamedGroupingPolicy(string $ptype, int $fieldIndex, string ...$fieldValues): array |
202
|
|
|
{ |
203
|
3 |
|
return $this->model->getFilteredPolicy('g', $ptype, $fieldIndex, ...$fieldValues); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Determines whether an authorization rule exists. |
208
|
|
|
* |
209
|
|
|
* @param mixed ...$params |
|
|
|
|
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
27 |
|
public function hasPolicy(...$params): bool |
214
|
|
|
{ |
215
|
27 |
|
return $this->hasNamedPolicy('p', ...$params); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Determines whether a named authorization rule exists. |
220
|
|
|
* |
221
|
|
|
* @param string $ptype |
|
|
|
|
222
|
|
|
* @param mixed ...$params |
|
|
|
|
223
|
|
|
* |
224
|
|
|
* @return bool |
225
|
|
|
*/ |
226
|
27 |
|
public function hasNamedPolicy(string $ptype, ...$params): bool |
227
|
|
|
{ |
228
|
27 |
|
if (1 == count($params) && is_array($params[0])) { |
229
|
6 |
|
$params = $params[0]; |
230
|
|
|
} |
231
|
|
|
|
232
|
27 |
|
return $this->model->hasPolicy('p', $ptype, $params); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* AddPolicy adds an authorization rule to the current policy. |
237
|
|
|
* If the rule already exists, the function returns false and the rule will not be added. |
238
|
|
|
* Otherwise the function returns true by adding the new rule. |
239
|
|
|
* |
240
|
|
|
* @param mixed ...$params |
|
|
|
|
241
|
|
|
* |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
27 |
|
public function addPolicy(...$params): bool |
245
|
|
|
{ |
246
|
27 |
|
return $this->addNamedPolicy('p', ...$params); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* AddPolicies adds authorization rules to the current policy. |
251
|
|
|
* If the rule already exists, the function returns false for the corresponding rule and the rule will not be added. |
252
|
|
|
* Otherwise the function returns true for the corresponding rule by adding the new rule. |
253
|
|
|
* |
254
|
|
|
* @param string[][] $rules |
|
|
|
|
255
|
|
|
* |
256
|
|
|
* @return bool |
257
|
|
|
* @throws Exceptions\CasbinException |
258
|
|
|
*/ |
259
|
18 |
|
public function addPolicies(array $rules): bool |
260
|
|
|
{ |
261
|
18 |
|
return $this->addNamedPolicies('p', $rules); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* AddPoliciesEx adds authorization rules to the current policy. |
266
|
|
|
* If the rule already exists, the rule will not be added. |
267
|
|
|
* But unlike AddPolicies, other non-existent rules are added instead of returning false directly. |
268
|
|
|
* |
269
|
|
|
* @param string[][] $rules |
|
|
|
|
270
|
|
|
* |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
3 |
|
public function addPoliciesEx(array $rules): bool |
274
|
|
|
{ |
275
|
3 |
|
return $this->addNamedPoliciesEx('p', $rules); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* AddNamedPolicy adds an authorization rule to the current named policy. |
280
|
|
|
* If the rule already exists, the function returns false and the rule will not be added. |
281
|
|
|
* Otherwise the function returns true by adding the new rule. |
282
|
|
|
* |
283
|
|
|
* @param string $ptype |
|
|
|
|
284
|
|
|
* @param mixed ...$params |
|
|
|
|
285
|
|
|
* |
286
|
|
|
* @return bool |
287
|
|
|
*/ |
288
|
27 |
|
public function addNamedPolicy(string $ptype, ...$params): bool |
289
|
|
|
{ |
290
|
27 |
|
if (1 == count($params) && is_array($params[0])) { |
291
|
3 |
|
$params = $params[0]; |
292
|
|
|
} |
293
|
|
|
|
294
|
27 |
|
return $this->addPolicyInternal('p', $ptype, $params); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* AddNamedPolicies adds authorization rules to the current named policy. |
299
|
|
|
* If the rule already exists, the function returns false for the corresponding rule and the rule will not be added. |
300
|
|
|
* Otherwise the function returns true for the corresponding by adding the new rule. |
301
|
|
|
* |
302
|
|
|
* @param string $ptype |
|
|
|
|
303
|
|
|
* @param string[][] $rules |
|
|
|
|
304
|
|
|
* |
305
|
|
|
* @return bool |
306
|
|
|
* @throws Exceptions\CasbinException |
307
|
|
|
*/ |
308
|
18 |
|
public function addNamedPolicies(string $ptype, array $rules): bool |
309
|
|
|
{ |
310
|
18 |
|
return $this->addPoliciesInternal('p', $ptype, $rules, false); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* AddNamedPoliciesEx adds authorization rules to the current named policy. |
315
|
|
|
* If the rule already exists, the rule will not be added. |
316
|
|
|
* But unlike AddNamedPolicies, other non-existent rules are added instead of returning false directly. |
317
|
|
|
* |
318
|
|
|
* @param string $ptype |
|
|
|
|
319
|
|
|
* @param string[][] $rules |
|
|
|
|
320
|
|
|
* |
321
|
|
|
* @return bool |
322
|
|
|
*/ |
323
|
3 |
|
public function addNamedPoliciesEx(string $ptype, array $rules): bool |
324
|
|
|
{ |
325
|
3 |
|
return $this->addPoliciesInternal('p', $ptype, $rules, true); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Removes an authorization rule from the current policy. |
330
|
|
|
* |
331
|
|
|
* @param mixed ...$params |
|
|
|
|
332
|
|
|
* |
333
|
|
|
* @return bool |
334
|
|
|
*/ |
335
|
15 |
|
public function removePolicy(...$params): bool |
336
|
|
|
{ |
337
|
15 |
|
return $this->removeNamedPolicy('p', ...$params); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Removes an authorization rules from the current policy. |
342
|
|
|
* |
343
|
|
|
* @param array $rules |
|
|
|
|
344
|
|
|
* |
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
9 |
|
public function removePolicies(array $rules): bool |
348
|
|
|
{ |
349
|
9 |
|
return $this->removeNamedPolicies('p', $rules); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Removes an authorization rule from the current policy. |
354
|
|
|
* |
355
|
|
|
* @param string[] $oldRule |
|
|
|
|
356
|
|
|
* @param string[] $newRule |
|
|
|
|
357
|
|
|
* |
358
|
|
|
* @return bool |
359
|
|
|
*/ |
360
|
3 |
|
public function updatePolicy(array $oldRule, array $newRule): bool |
361
|
|
|
{ |
362
|
3 |
|
return $this->updateNamedPolicy("p", $oldRule, $newRule); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Updates an authorization rule from the current policy. |
367
|
|
|
* |
368
|
|
|
* @param string $ptype |
|
|
|
|
369
|
|
|
* @param string[] $oldRule |
|
|
|
|
370
|
|
|
* @param string[] $newRule |
|
|
|
|
371
|
|
|
* |
372
|
|
|
* @return bool |
373
|
|
|
*/ |
374
|
3 |
|
public function updateNamedPolicy(string $ptype, array $oldRule, array $newRule): bool |
375
|
|
|
{ |
376
|
3 |
|
return $this->updatePolicyInternal("p", $ptype, $oldRule, $newRule); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* UpdatePolicies updates authorization rules from the current policies. |
381
|
|
|
* |
382
|
|
|
* @param string[][] $oldPolices |
|
|
|
|
383
|
|
|
* @param string[][] $newPolicies |
|
|
|
|
384
|
|
|
* @return boolean |
|
|
|
|
385
|
|
|
*/ |
386
|
3 |
|
public function updatePolicies(array $oldPolices, array $newPolicies): bool |
387
|
|
|
{ |
388
|
3 |
|
return $this->updateNamedPolicies("p", $oldPolices, $newPolicies); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Updates authorization rules from the current policy. |
393
|
|
|
* |
394
|
|
|
* @param string $ptype |
|
|
|
|
395
|
|
|
* @param string[][] $oldPolices |
|
|
|
|
396
|
|
|
* @param string[][] $newPolicies |
|
|
|
|
397
|
|
|
* @return boolean |
|
|
|
|
398
|
|
|
*/ |
399
|
3 |
|
public function updateNamedPolicies(string $ptype, array $oldPolices, array $newPolicies): bool |
400
|
|
|
{ |
401
|
3 |
|
return $this->updatePoliciesInternal("p", $ptype, $oldPolices, $newPolicies); |
402
|
|
|
} |
403
|
|
|
|
404
|
6 |
|
public function updateFilteredPolicies(array $newPolicies, int $fieldIndex, string ...$fieldValues): bool |
|
|
|
|
405
|
|
|
{ |
406
|
6 |
|
return $this->updateFilteredNamedPolicies("p", $newPolicies, $fieldIndex, ...$fieldValues); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Undocumented function |
411
|
|
|
* |
412
|
|
|
* @param string $ptype |
|
|
|
|
413
|
|
|
* @param array $newPolicies |
|
|
|
|
414
|
|
|
* @param integer $fieldIndex |
|
|
|
|
415
|
|
|
* @param string ...$fieldValues |
|
|
|
|
416
|
|
|
* @return boolean |
|
|
|
|
417
|
|
|
*/ |
418
|
6 |
|
public function updateFilteredNamedPolicies(string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): bool |
419
|
|
|
{ |
420
|
6 |
|
return $this->updateFilteredPoliciesInternal("p", $ptype, $newPolicies, $fieldIndex, ...$fieldValues); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Removes an authorization rule from the current policy, field filters can be specified. |
425
|
|
|
* |
426
|
|
|
* @param int $fieldIndex |
|
|
|
|
427
|
|
|
* @param string ...$fieldValues |
|
|
|
|
428
|
|
|
* |
429
|
|
|
* @return bool |
430
|
|
|
*/ |
431
|
18 |
|
public function removeFilteredPolicy(int $fieldIndex, string ...$fieldValues): bool |
432
|
|
|
{ |
433
|
18 |
|
return $this->removeFilteredNamedPolicy('p', $fieldIndex, ...$fieldValues); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Removes an authorization rule from the current named policy. |
438
|
|
|
* |
439
|
|
|
* @param string $ptype |
|
|
|
|
440
|
|
|
* @param mixed ...$params |
|
|
|
|
441
|
|
|
* |
442
|
|
|
* @return bool |
443
|
|
|
*/ |
444
|
15 |
|
public function removeNamedPolicy(string $ptype, ...$params): bool |
445
|
|
|
{ |
446
|
15 |
|
if (1 == count($params) && is_array($params[0])) { |
447
|
3 |
|
$params = $params[0]; |
448
|
|
|
} |
449
|
|
|
|
450
|
15 |
|
return $this->removePolicyInternal('p', $ptype, $params); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Removes an authorization rules from the current named policy. |
455
|
|
|
* |
456
|
|
|
* @param string $ptype |
|
|
|
|
457
|
|
|
* @param array $rules |
|
|
|
|
458
|
|
|
* |
459
|
|
|
* @return bool |
460
|
|
|
*/ |
461
|
9 |
|
public function removeNamedPolicies(string $ptype, array $rules): bool |
462
|
|
|
{ |
463
|
9 |
|
return $this->removePoliciesInternal('p', $ptype, $rules); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Removes an authorization rule from the current named policy, field filters can be specified. |
468
|
|
|
* |
469
|
|
|
* @param string $ptype |
|
|
|
|
470
|
|
|
* @param int $fieldIndex |
|
|
|
|
471
|
|
|
* @param string ...$fieldValues |
|
|
|
|
472
|
|
|
* |
473
|
|
|
* @return bool |
474
|
|
|
*/ |
475
|
18 |
|
public function removeFilteredNamedPolicy(string $ptype, int $fieldIndex, string ...$fieldValues): bool |
476
|
|
|
{ |
477
|
18 |
|
return $this->removeFilteredPolicyInternal('p', $ptype, $fieldIndex, ...$fieldValues); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Determines whether a role inheritance rule exists. |
482
|
|
|
* |
483
|
|
|
* @param mixed ...$params |
|
|
|
|
484
|
|
|
* |
485
|
|
|
* @return bool |
486
|
|
|
*/ |
487
|
3 |
|
public function hasGroupingPolicy(...$params): bool |
488
|
|
|
{ |
489
|
3 |
|
return $this->hasNamedGroupingPolicy('g', ...$params); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Determines whether a named role inheritance rule exists. |
494
|
|
|
* |
495
|
|
|
* @param string $ptype |
|
|
|
|
496
|
|
|
* @param mixed ...$params |
|
|
|
|
497
|
|
|
* |
498
|
|
|
* @return bool |
499
|
|
|
*/ |
500
|
3 |
|
public function hasNamedGroupingPolicy(string $ptype, ...$params): bool |
501
|
|
|
{ |
502
|
3 |
|
if (1 == count($params) && is_array($params[0])) { |
503
|
3 |
|
$params = $params[0]; |
504
|
|
|
} |
505
|
|
|
|
506
|
3 |
|
return $this->model->hasPolicy('g', $ptype, $params); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* AddGroupingPolicy adds a role inheritance rule to the current policy. |
511
|
|
|
* If the rule already exists, the function returns false and the rule will not be added. |
512
|
|
|
* Otherwise the function returns true by adding the new rule. |
513
|
|
|
* |
514
|
|
|
* @param mixed ...$params |
|
|
|
|
515
|
|
|
* |
516
|
|
|
* @return bool |
517
|
|
|
*/ |
518
|
21 |
|
public function addGroupingPolicy(...$params): bool |
519
|
|
|
{ |
520
|
21 |
|
return $this->addNamedGroupingPolicy('g', ...$params); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* AddGroupingPolicy adds a role inheritance rules to the current policy. |
525
|
|
|
* If the rule already exists, the function returns false and the rule will not be added. |
526
|
|
|
* Otherwise the function returns true by adding the new rule. |
527
|
|
|
* |
528
|
|
|
* @param array $rules |
|
|
|
|
529
|
|
|
* |
530
|
|
|
* @return bool |
531
|
|
|
*/ |
532
|
6 |
|
public function addGroupingPolicies(array $rules): bool |
533
|
|
|
{ |
534
|
6 |
|
return $this->addNamedGroupingPolicies('g', $rules); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* AddGroupingPolicyEx adds a role inheritance rules to the current policy. |
539
|
|
|
* If the rule already exists, the rule will not be added. |
540
|
|
|
* But unlike AddGroupingPolicy, other non-existent rules are added instead of returning false directly. |
541
|
|
|
* |
542
|
|
|
* @param array $rules |
|
|
|
|
543
|
|
|
* |
544
|
|
|
* @return bool |
545
|
|
|
*/ |
546
|
3 |
|
public function addGroupingPoliciesEx(array $rules): bool |
547
|
|
|
{ |
548
|
3 |
|
return $this->addNamedGroupingPoliciesEx('g', $rules); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* AddNamedGroupingPolicy adds a named role inheritance rule to the current policy. |
553
|
|
|
* If the rule already exists, the function returns false and the rule will not be added. |
554
|
|
|
* Otherwise the function returns true by adding the new rule. |
555
|
|
|
* |
556
|
|
|
* @param string $ptype |
|
|
|
|
557
|
|
|
* @param mixed ...$params |
|
|
|
|
558
|
|
|
* |
559
|
|
|
* @return bool |
560
|
|
|
*/ |
561
|
21 |
|
public function addNamedGroupingPolicy(string $ptype, ...$params): bool |
562
|
|
|
{ |
563
|
21 |
|
if (1 == count($params) && is_array($params[0])) { |
564
|
3 |
|
$params = $params[0]; |
565
|
|
|
} |
566
|
|
|
|
567
|
21 |
|
$ruleAdded = $this->addPolicyInternal('g', $ptype, $params); |
568
|
|
|
|
569
|
21 |
|
return $ruleAdded; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* AddNamedGroupingPolicy adds a named role inheritance rules to the current policy. |
574
|
|
|
* If the rule already exists, the function returns false and the rule will not be added. |
575
|
|
|
* Otherwise the function returns true by adding the new rule. |
576
|
|
|
* |
577
|
|
|
* @param string $ptype |
|
|
|
|
578
|
|
|
* @param array $rules |
|
|
|
|
579
|
|
|
* |
580
|
|
|
* @return bool |
581
|
|
|
*/ |
582
|
6 |
|
public function addNamedGroupingPolicies(string $ptype, array $rules): bool |
583
|
|
|
{ |
584
|
6 |
|
return $this->addPoliciesInternal('g', $ptype, $rules, false); |
585
|
|
|
} |
586
|
|
|
|
587
|
3 |
|
public function addNamedGroupingPoliciesEx(string $ptype, array $rules): bool |
|
|
|
|
588
|
|
|
{ |
589
|
3 |
|
return $this->addPoliciesInternal('g', $ptype, $rules, true); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Removes a role inheritance rule from the current policy. |
594
|
|
|
* |
595
|
|
|
* @param mixed ...$params |
|
|
|
|
596
|
|
|
* |
597
|
|
|
* @return bool |
598
|
|
|
*/ |
599
|
18 |
|
public function removeGroupingPolicy(...$params): bool |
600
|
|
|
{ |
601
|
18 |
|
return $this->removeNamedGroupingPolicy('g', ...$params); |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Removes a role inheritance rules from the current policy. |
606
|
|
|
* |
607
|
|
|
* @param array $rules |
|
|
|
|
608
|
|
|
* |
609
|
|
|
* @return bool |
610
|
|
|
*/ |
611
|
12 |
|
public function removeGroupingPolicies(array $rules): bool |
612
|
|
|
{ |
613
|
12 |
|
return $this->removeNamedGroupingPolicies('g', $rules); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Removes a role inheritance rule from the current policy, field filters can be specified. |
618
|
|
|
* |
619
|
|
|
* @param int $fieldIndex |
|
|
|
|
620
|
|
|
* @param string ...$fieldValues |
|
|
|
|
621
|
|
|
* |
622
|
|
|
* @return bool |
623
|
|
|
*/ |
624
|
12 |
|
public function removeFilteredGroupingPolicy(int $fieldIndex, string ...$fieldValues): bool |
625
|
|
|
{ |
626
|
12 |
|
return $this->removeFilteredNamedGroupingPolicy('g', $fieldIndex, ...$fieldValues); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Removes a role inheritance rule from the current named policy. |
631
|
|
|
* |
632
|
|
|
* @param string $ptype |
|
|
|
|
633
|
|
|
* @param mixed ...$params |
|
|
|
|
634
|
|
|
* |
635
|
|
|
* @return bool |
636
|
|
|
*/ |
637
|
18 |
|
public function removeNamedGroupingPolicy(string $ptype, ...$params): bool |
638
|
|
|
{ |
639
|
18 |
|
if (1 == count($params) && is_array($params[0])) { |
640
|
3 |
|
$params = $params[0]; |
641
|
|
|
} |
642
|
|
|
|
643
|
18 |
|
$ruleRemoved = $this->removePolicyInternal('g', $ptype, $params); |
644
|
|
|
|
645
|
18 |
|
if ($this->autoBuildRoleLinks) { |
646
|
18 |
|
$this->buildRoleLinks(); |
647
|
|
|
} |
648
|
|
|
|
649
|
18 |
|
return $ruleRemoved; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Removes a role inheritance rules from the current named policy. |
654
|
|
|
* |
655
|
|
|
* @param string $ptype |
|
|
|
|
656
|
|
|
* @param array $rules |
|
|
|
|
657
|
|
|
* |
658
|
|
|
* @return bool |
659
|
|
|
*/ |
660
|
12 |
|
public function removeNamedGroupingPolicies(string $ptype, array $rules): bool |
661
|
|
|
{ |
662
|
12 |
|
$ruleRemoved = $this->removePoliciesInternal('g', $ptype, $rules); |
663
|
|
|
|
664
|
12 |
|
if ($this->autoBuildRoleLinks) { |
665
|
12 |
|
$this->buildRoleLinks(); |
666
|
|
|
} |
667
|
|
|
|
668
|
12 |
|
return $ruleRemoved; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* Removes a role inheritance rule from the current named policy, field filters can be specified. |
673
|
|
|
* |
674
|
|
|
* @param string $ptype |
|
|
|
|
675
|
|
|
* @param int $fieldIndex |
|
|
|
|
676
|
|
|
* @param string ...$fieldValues |
|
|
|
|
677
|
|
|
* |
678
|
|
|
* @return bool |
679
|
|
|
*/ |
680
|
12 |
|
public function removeFilteredNamedGroupingPolicy(string $ptype, int $fieldIndex, string ...$fieldValues): bool |
681
|
|
|
{ |
682
|
12 |
|
$ruleRemoved = $this->removeFilteredPolicyInternal('g', $ptype, $fieldIndex, ...$fieldValues); |
683
|
|
|
|
684
|
12 |
|
if ($this->autoBuildRoleLinks) { |
685
|
12 |
|
$this->buildRoleLinks(); |
686
|
|
|
} |
687
|
|
|
|
688
|
12 |
|
return $ruleRemoved; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
/** |
692
|
|
|
* Adds a customized function. |
693
|
|
|
* |
694
|
|
|
* @param string $name |
|
|
|
|
695
|
|
|
* @param Closure $func |
|
|
|
|
696
|
|
|
*/ |
|
|
|
|
697
|
|
|
public function addFunction(string $name, Closure $func): void |
698
|
|
|
{ |
699
|
|
|
$this->fm->addFunction($name, $func); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* Adds authorization rule to the current policy. |
704
|
|
|
* If the rule already exists, the function returns false and the rule will not be added. |
705
|
|
|
* Otherwise the function returns true by adding the new rule. |
706
|
|
|
* |
707
|
|
|
* @param string $sec |
|
|
|
|
708
|
|
|
* @param string $ptype |
|
|
|
|
709
|
|
|
* @param string[] $params |
|
|
|
|
710
|
|
|
* |
711
|
|
|
* @return void |
712
|
|
|
*/ |
713
|
3 |
|
public function selfAddPolicy(string $sec, string $ptype, array $params): void |
714
|
|
|
{ |
715
|
3 |
|
$this->addPolicyWithoutNotifyInternal($sec, $ptype, $params); |
716
|
2 |
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Adds authorization rules to the current policy. |
720
|
|
|
* If the rule already exists, the function returns false for the corresponding rule and the rule will not be added. |
721
|
|
|
* Otherwise the function returns true for the corresponding rule by adding the new rule. |
722
|
|
|
* |
723
|
|
|
* @param string $sec |
|
|
|
|
724
|
|
|
* @param string $ptype |
|
|
|
|
725
|
|
|
* @param string[][] $params |
|
|
|
|
726
|
|
|
* |
727
|
|
|
* @return bool |
728
|
|
|
*/ |
729
|
3 |
|
public function selfAddPolices(string $sec, string $ptype, array $params): bool |
730
|
|
|
{ |
731
|
3 |
|
return $this->addPoliciesWithoutNotifyInternal($sec, $ptype, $params, false); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* Adds authorization rules to the current named policy with autoNotifyWatcher disabled. |
736
|
|
|
* If the rule already exists, the rule will not be added. |
737
|
|
|
* But unlike SelfAddPolicies, other non-existent rules are added instead of returning false directly |
738
|
|
|
* |
739
|
|
|
* @param string $sec |
|
|
|
|
740
|
|
|
* @param string $ptype |
|
|
|
|
741
|
|
|
* @param string[][] $params |
|
|
|
|
742
|
|
|
* |
743
|
|
|
* @return bool |
744
|
|
|
*/ |
745
|
6 |
|
public function selfAddPolicesEx(string $sec, string $ptype, array $params): bool |
746
|
|
|
{ |
747
|
6 |
|
return $this->addPoliciesWithoutNotifyInternal($sec, $ptype, $params, true); |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
/** |
751
|
|
|
* Gets the index for a given ptype and field. |
752
|
|
|
* |
753
|
|
|
* @param string $ptype |
|
|
|
|
754
|
|
|
* @param string $field |
|
|
|
|
755
|
|
|
* |
756
|
|
|
* @return int $fieldIndex |
757
|
|
|
* @throws Exceptions\CasbinException |
758
|
|
|
*/ |
759
|
3 |
|
public function getFieldIndex(string $ptype, string $field): int |
760
|
|
|
{ |
761
|
3 |
|
return $this->model->getFieldIndex($ptype, $field); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Sets the index for a given ptype and field. |
766
|
|
|
* |
767
|
|
|
* @param string $ptype |
|
|
|
|
768
|
|
|
* @param string $field |
|
|
|
|
769
|
|
|
* @param int $index |
|
|
|
|
770
|
|
|
*/ |
|
|
|
|
771
|
3 |
|
public function setFieldIndex(string $ptype, string $field, int $index): void |
772
|
|
|
{ |
773
|
3 |
|
$this->model->setFieldIndex($ptype, $field, $index); |
774
|
2 |
|
} |
775
|
|
|
} |
776
|
|
|
|