• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

php-casbin / php-casbin / 15422853167

03 Jun 2025 04:34PM UTC coverage: 94.307% (-0.2%) from 94.485%
15422853167

push

github

leeqvip
refactor: Upgrade phpstan to level 8

52 of 63 new or added lines in 8 files covered. (82.54%)

2 existing lines in 1 file now uncovered.

1938 of 2055 relevant lines covered (94.31%)

231.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.34
/src/Enforcer.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Casbin;
6

7
use Casbin\Constant\Constants;
8
use Casbin\Exceptions\CasbinException;
9
use Casbin\Exceptions\EmptyConditionException;
10
use Casbin\Exceptions\ObjConditionException;
11
use Casbin\Util\Util;
12

13
/**
14
 * Enforcer = ManagementEnforcer + RBAC API.
15
 *
16
 * @author techlee@qq.com
17
 */
18
class Enforcer extends ManagementEnforcer
19
{
20
    /**
21
     * Gets the roles that a user has.
22
     *
23
     * @param string $name
24
     * @param string ...$domain
25
     * @return string[]|null
26
     */
27
    public function getRolesForUser(string $name, string ...$domain): ?array
28
    {
29
        return isset($this->model['g']['g']) ? $this->model['g']['g']->rm?->getRoles($name, ...$domain) : [];
100✔
30
    }
31

32
    /**
33
     * Gets the users that has a role.
34
     *
35
     * @param string $name
36
     * @param string ...$domain
37
     *
38
     * @return string[]|null
39
     */
40
    public function getUsersForRole(string $name, string ...$domain): ?array
41
    {
42
        return isset($this->model['g']['g']) ? $this->model['g']['g']->rm?->getUsers($name, ...$domain) : [];
20✔
43
    }
44

45
    /**
46
     * Determines whether a user has a role.
47
     *
48
     * @param string $name
49
     * @param string $role
50
     * @param string ...$domain
51
     *
52
     * @return bool
53
     */
54
    public function hasRoleForUser(string $name, string $role, string ...$domain): bool
55
    {
56
        $roles = $this->getRolesForUser($name, ...$domain);
30✔
57

58
        return in_array($role, $roles ?? [], true);
30✔
59
    }
60

61
    /**
62
     * Adds a role for a user.
63
     * returns false if the user already has the role (aka not affected).
64
     *
65
     * @param string $user
66
     * @param string $role
67
     * @param string ...$domain
68
     * @return bool
69
     */
70
    public function addRoleForUser(string $user, string $role, string ...$domain): bool
71
    {
72
        return $this->addGroupingPolicy(...array_merge([$user, $role], $domain));
20✔
73
    }
74

75
    /**
76
     * @param string $user
77
     * @param string[] $roles
78
     * @param string ...$domain
79
     *
80
     * @return bool
81
     */
82
    public function addRolesForUser(string $user, array $roles, string ...$domain): bool
83
    {
84
        return $this->addGroupingPolicies(
10✔
85
            array_map(function ($role) use ($user, $domain) {
10✔
86
                return array_merge([$user, $role], $domain);
10✔
87
            }, $roles)
10✔
88
        );
10✔
89
    }
90

91
    /**
92
     * Deletes a role for a user.
93
     * returns false if the user does not have the role (aka not affected).
94
     *
95
     * @param string $user
96
     * @param string $role
97
     * @param string ...$domain
98
     *
99
     * @return bool
100
     */
101
    public function deleteRoleForUser(string $user, string $role, string ...$domain): bool
102
    {
103
        return $this->removeGroupingPolicy(...array_merge([$user, $role], $domain));
30✔
104
    }
105

106
    /**
107
     * Deletes all roles for a user.
108
     * Returns false if the user does not have any roles (aka not affected).
109
     *
110
     * @param string $user
111
     * @param string ...$domain
112
     *
113
     * @return bool
114
     * @throws CasbinException
115
     */
116
    public function deleteRolesForUser(string $user, string ...$domain): bool
117
    {
118
        if (count($domain) > 1) {
10✔
119
            throw new CasbinException('error: domain should be 1 parameter');
×
120
        }
121

122
        return $this->removeFilteredGroupingPolicy(0, ...array_merge([$user, ''], $domain));
10✔
123
    }
124

125
    /**
126
     * Deletes a user.
127
     * Returns false if the user does not exist (aka not affected).
128
     *
129
     * @param string $user
130
     *
131
     * @return bool
132
     */
133
    public function deleteUser(string $user): bool
134
    {
135
        $res1 = $this->removeFilteredGroupingPolicy(0, $user);
10✔
136

137
        $subIndex = $this->model->getFieldIndex('p', Constants::SUBJECT_INDEX);
10✔
138
        $res2 = $this->removeFilteredPolicy($subIndex, $user);
10✔
139

140
        return $res1 || $res2;
10✔
141
    }
142

143
    /**
144
     * Deletes a role.
145
     *
146
     * @param string $role
147
     * @return bool
148
     */
149
    public function deleteRole(string $role): bool
150
    {
151
        $res1 = $this->removeFilteredGroupingPolicy(1, $role);
10✔
152

153
        $subIndex = $this->model->getFieldIndex('p', Constants::SUBJECT_INDEX);
10✔
154
        $res2 = $this->removeFilteredPolicy($subIndex, $role);
10✔
155

156
        return $res1 || $res2;
10✔
157
    }
158

159
    /**
160
     * Deletes a permission.
161
     * Returns false if the permission does not exist (aka not affected).
162
     *
163
     * @param string ...$permission
164
     *
165
     * @return bool
166
     */
167
    public function deletePermission(string ...$permission): bool
168
    {
169
        return $this->removeFilteredPolicy(1, ...$permission);
20✔
170
    }
171

172
    /**
173
     * Adds a permission for a user or role.
174
     * Returns false if the user or role already has the permission (aka not affected).
175
     *
176
     * @param string $user
177
     * @param string ...$permission
178
     *
179
     * @return bool
180
     */
181
    public function addPermissionForUser(string $user, string ...$permission): bool
182
    {
183
        $params = array_merge([$user], $permission);
40✔
184

185
        return $this->addPolicy(...$params);
40✔
186
    }
187

188
    /**
189
     * AddPermissionsForUser adds multiple permissions for a user or role.
190
     * Returns false if the user or role already has one of the permissions (aka not affected).
191
     *
192
     * @param string $user
193
     * @param array ...$permissions
194
     * @return bool
195
     */
196
    public function addPermissionsForUser(string $user, array ...$permissions): bool
197
    {
198
        $rules = [];
10✔
199
        foreach ($permissions as $permission) {
10✔
200
            $rules[] = array_merge([$user], $permission);
10✔
201
        }
202
        return $this->addPolicies($rules);
10✔
203
    }
204

205
    /**
206
     * Deletes a permission for a user or role.
207
     * Returns false if the user or role does not have the permission (aka not affected).
208
     *
209
     * @param string $user
210
     * @param string ...$permission
211
     *
212
     * @return bool
213
     */
214
    public function deletePermissionForUser(string $user, string ...$permission): bool
215
    {
216
        $params = array_merge([$user], $permission);
10✔
217

218
        return $this->removePolicy(...$params);
10✔
219
    }
220

221
    /**
222
     * Deletes permissions for a user or role.
223
     * Returns false if the user or role does not have any permissions (aka not affected).
224
     *
225
     * @param string $user
226
     *
227
     * @return bool
228
     */
229
    public function deletePermissionsForUser(string $user): bool
230
    {
231
        $subIndex = $this->model->getFieldIndex('p', Constants::SUBJECT_INDEX);
20✔
232
        return $this->removeFilteredPolicy($subIndex, $user);
10✔
233
    }
234

235
    /**
236
     * Gets permissions for a user or role.
237
     *
238
     * @param string $user
239
     * @param string ...$domain
240
     *
241
     * @return array
242
     */
243
    public function getPermissionsForUser(string $user, string ...$domain): array
244
    {
245
        $permission = [];
50✔
246
        foreach ($this->model['p'] ?? [] as $ptype => $assertion) {
50✔
247
            $args = [];
50✔
248
            $subIndex = $this->model->getFieldIndex('p', Constants::SUBJECT_INDEX);
50✔
249
            $args[$subIndex] = $user;
50✔
250
            if (count($domain) > 0) {
50✔
251
                $domIndex = $this->model->getFieldIndex($ptype, Constants::DOMAIN_INDEX);
10✔
252
                $args[$domIndex] = $domain[0];
10✔
253
            }
254
            $perm = $this->getFilteredPolicy(0, ...$args);
50✔
255
            $permission = array_merge($permission, $perm);
50✔
256
        }
257
        return $permission;
50✔
258
    }
259

260
    /**
261
     * Determines whether a user has a permission.
262
     *
263
     * @param string $user
264
     * @param string ...$permission
265
     *
266
     * @return bool
267
     */
268
    public function hasPermissionForUser(string $user, string ...$permission): bool
269
    {
270
        $params = array_merge([$user], $permission);
10✔
271

272
        return $this->hasPolicy($params);
10✔
273
    }
274

275
    /**
276
     * Gets implicit roles that a user has.
277
     * Compared to getRolesForUser(), this function retrieves indirect roles besides direct roles.
278
     * For example:
279
     * g, alice, role:admin
280
     * g, role:admin, role:user.
281
     *
282
     * getRolesForUser("alice") can only get: ["role:admin"].
283
     * But getImplicitRolesForUser("alice") will get: ["role:admin", "role:user"].
284
     *
285
     * @param string $name
286
     * @param string ...$domain
287
     *
288
     * @return array
289
     */
290
    public function getImplicitRolesForUser(string $name, string ...$domain): array
291
    {
292
        $res = [];
40✔
293
        $roleSet = [];
40✔
294
        $roleSet[$name] = true;
40✔
295

296
        $q = [];
40✔
297
        $q[] = $name;
40✔
298

299
        for (; count($q) > 0;) {
40✔
300
            $name = $q[0];
40✔
301
            $q = array_slice($q, 1);
40✔
302

303
            foreach ($this->rmMap as $rm) {
40✔
304
                $roles = $rm->getRoles($name, ...$domain);
40✔
305
                foreach ($roles as $r) {
40✔
306
                    if (!isset($roleSet[$r])) {
40✔
307
                        $res[] = $r;
40✔
308
                        $q[] = $r;
40✔
309
                        $roleSet[$r] = true;
40✔
310
                    }
311
                }
312
            }
313
        }
314

315
        return $res;
40✔
316
    }
317

318
    /**
319
     * GetImplicitUsersForRole gets implicit users for a role.
320
     *
321
     * @param string $name
322
     * @param string ...$domain
323
     * @return array
324
     */
325
    public function getImplicitUsersForRole(string $name, string ...$domain): array
326
    {
327
        $res = [];
20✔
328
        $roleSet = [];
20✔
329
        $roleSet[$name] = true;
20✔
330

331
        $q = [];
20✔
332
        $q[] = $name;
20✔
333

334
        for (; count($q) > 0;) {
20✔
335
            $name = $q[0];
20✔
336
            $q = array_slice($q, 1);
20✔
337

338
            foreach ($this->rmMap as $rm) {
20✔
339
                $roles = $rm->getUsers($name, ...$domain);
20✔
340
                foreach ($roles as $r) {
20✔
341
                    if (!isset($roleSet[$r])) {
20✔
342
                        $res[] = $r;
20✔
343
                        $q[] = $r;
20✔
344
                        $roleSet[$r] = true;
20✔
345
                    }
346
                }
347
            }
348
        }
349
        return $res;
20✔
350
    }
351

352
    /**
353
     * GetDomainsForUser gets all domains that a subject inherits.
354
     *
355
     * @param string $user
356
     *
357
     * @return string[]
358
     */
359
    public function getDomainsForUser(string $user): array
360
    {
361
        $domains = [];
10✔
362
        foreach ($this->rmMap as $rm) {
10✔
363
            $res = $rm->getDomains($user);
10✔
364
            $domains = array_merge($domains, $res);
10✔
365
        }
366

367
        return $domains;
10✔
368
    }
369

370
    /**
371
     * GetImplicitResourcesForUser returns all policies that user obtaining in domain
372
     *
373
     * @param string $user
374
     * @param string ...$domain
375
     * @return array
376
     */
377
    public function getImplicitResourcesForUser(string $user, string ...$domain): array
378
    {
379
        $permissions = $this->getImplicitPermissionsForUser($user, ...$domain);
10✔
380

381
        $res = [];
10✔
382
        foreach ($permissions as $permission) {
10✔
383
            if ($permission[0] == $user) {
10✔
384
                $res[] = $permission;
10✔
385
                continue;
10✔
386
            }
387
            $resLocal = [[$user]];
10✔
388
            $tokensLength = count($permission);
10✔
389
            $t = [[]];
10✔
390
            foreach (array_slice($permission, 1) as $token) {
10✔
391
                $tokens = $this->getImplicitUsersForRole($token, ...$domain);
10✔
392
                $tokens[] = $token;
10✔
393
                $t[] = $tokens;
10✔
394
            }
395

396
            for ($i = 1; $i < $tokensLength; $i++) {
10✔
397
                $n = [];
10✔
398
                foreach ($t[$i] as $tokens) {
10✔
399
                    foreach ($resLocal as $policy) {
10✔
400
                        $temp = [];
10✔
401
                        $temp = array_merge($temp, $policy);
10✔
402
                        $temp[] = $tokens;
10✔
403
                        $n[] = $temp;
10✔
404
                    }
405
                }
406
                $resLocal = $n;
10✔
407
            }
408
            $res = array_merge($res, $resLocal);
10✔
409
        }
410
        return $res;
10✔
411
    }
412

413
    /**
414
     * Gets implicit permissions for a user or role.
415
     * Compared to getPermissionsForUser(), this function retrieves permissions for inherited roles.
416
     * For example:
417
     * p, admin, data1, read
418
     * p, alice, data2, read
419
     * g, alice, admin.
420
     *
421
     * getPermissionsForUser("alice") can only get: [["alice", "data2", "read"]].
422
     * But getImplicitPermissionsForUser("alice") will get: [["admin", "data1", "read"], ["alice", "data2", "read"]].
423
     *
424
     * @param string $user
425
     * @param string ...$domain
426
     *
427
     * @return array
428
     * @throws CasbinException
429
     */
430
    public function getImplicitPermissionsForUser(string $user, string ...$domain): array
431
    {
432
        $roles = array_merge(
30✔
433
            [$user],
30✔
434
            $this->getImplicitRolesForUser($user, ...$domain)
30✔
435
        );
30✔
436

437
        $len = count($domain);
30✔
438
        if ($len > 1) {
30✔
439
            throw new CasbinException('error: domain should be 1 parameter');
×
440
        }
441

442
        $res = [];
30✔
443
        foreach ($roles as $role) {
30✔
444
            if (1 == $len) {
30✔
445
                $permissions = $this->getPermissionsForUserInDomain($role, $domain[0]);
10✔
446
            } else {
447
                $permissions = $this->getPermissionsForUser($role);
30✔
448
            }
449

450
            $res = array_merge($res, $permissions);
30✔
451
        }
452

453
        return $res;
30✔
454
    }
455

456
    /**
457
     * Gets implicit users for a permission.
458
     * For example:
459
     * p, admin, data1, read
460
     * p, bob, data1, read
461
     * g, alice, admin
462
     * getImplicitUsersForPermission("data1", "read") will get: ["alice", "bob"].
463
     * Note: only users will be returned, roles (2nd arg in "g") will be excluded.
464
     *
465
     * @param string ...$permission
466
     *
467
     * @return array
468
     * @throws CasbinException
469
     */
470
    public function getImplicitUsersForPermission(string ...$permission): array
471
    {
472
        $pSubjects = $this->getAllSubjects();
10✔
473
        $gInherit = $this->model->getValuesForFieldInPolicyAllTypes("g", 1);
10✔
474
        $gSubjects = $this->model->getValuesForFieldInPolicyAllTypes("g", 0);
10✔
475

476
        $subjects = array_merge($pSubjects, $gSubjects);
10✔
477
        Util::ArrayRemoveDuplicates($subjects);
10✔
478

479
        $subjects = array_diff($subjects, $gInherit);
10✔
480

481
        $res = [];
10✔
482
        foreach ($subjects as $user) {
10✔
483
            $req = $permission;
10✔
484
            array_unshift($req, $user);
10✔
485
            $allowed = $this->enforce(...$req);
10✔
486

487
            if ($allowed) {
10✔
488
                $res[] = $user;
10✔
489
            }
490
        }
491

492
        return $res;
10✔
493
    }
494

495
    /**
496
     * Convert permissions to string as a hash to deduplicate.
497
     *
498
     * @param array $permissions
499
     *
500
     * @return array
501
     */
502
    private function removeDumplicatePermissions(array $permissions): array
503
    {
504
        $permissionsSet = [];
20✔
505
        $res = [];
20✔
506

507
        foreach ($permissions as $permission) {
20✔
508
            $permissionStr = Util::arrayToString($permission);
20✔
509

510
            if (isset($permissionsSet[$permissionStr])) {
20✔
511
                continue;
10✔
512
            }
513

514
            $permissionsSet[$permissionStr] = true;
20✔
515
            $res[] = $permission;
20✔
516
        }
517
        return $res;
20✔
518
    }
519

520
    /**
521
     * GetAllowedObjectConditions returns a string array of object conditions that the user can access.
522
     * For example: conditions, err := e.GetAllowedObjectConditions("alice", "read", "r.obj.")
523
     * Note:
524
     *
525
     * 0. prefix: You can customize the prefix of the object conditions, and "r.obj." is commonly used as a prefix.
526
     * After removing the prefix, the remaining part is the condition of the object.
527
     * If there is an obj policy that does not meet the prefix requirement, an ObjConditionException will be thrown.
528
     *
529
     * 1. If the 'objectConditions' array is empty, an EmptyConditionException will be thrown.
530
     * This error is thrown because some data adapters' ORM return full table data by default
531
     * when they receive an empty condition, which tends to behave contrary to expectations.(e.g. DBALAdapter)
532
     * If you are using an adapter that does not behave like this, you can choose to ignore this error.
533
     *
534
     * @param string $user
535
     * @param string $action
536
     * @param string $prefix
537
     *
538
     * @return array
539
     * @throws ObjConditionException
540
     * @throws EmptyConditionException
541
     */
542
    public function getAllowedObjectConditions(string $user, string $action, string $prefix): array
543
    {
544
        $permission = $this->getImplicitPermissionsForUser($user);
10✔
545

546
        $objectConditions = [];
10✔
547
        foreach ($permission as $policy) {
10✔
548
            if ($policy[2] == $action) {
10✔
549
                if (!str_starts_with($policy[1], $prefix)) {
10✔
550
                    throw new ObjConditionException('need to meet the prefix required by the object condition');
10✔
551
                }
552

553
                $objectConditions[] = substr($policy[1], strlen($prefix));
10✔
554
            }
555
        }
556

557
        if (empty($objectConditions)) {
10✔
558
            throw new EmptyConditionException('GetAllowedObjectConditions have an empty condition');
10✔
559
        }
560

561
        return $objectConditions;
10✔
562
    }
563

564
    /**
565
     * GetImplicitUsersForResource return implicit user based on resource.
566
     * For example:
567
     * p, alice, data1, read
568
     * p, bob, data2, write
569
     * p, data2_admin, data2, read
570
     * p, data2_admin, data2, write
571
     * g, alice, data2_admin
572
     * GetImplicitUsersForResource("data2") will return [[bob data2 write] [alice data2 read] [alice data2 write]]
573
     * GetImplicitUsersForResource("data1") will return [[alice data1 read]]
574
     * Note: only users will be returned, roles (2nd arg in "g") will be excluded.
575
     *
576
     * @param string $resource
577
     *
578
     * @return array
579
     */
580
    public function getImplicitUsersForResource(string $resource): array
581
    {
582
        $permissions = [];
10✔
583
        $subIndex = $this->model->getFieldIndex('p', Constants::SUBJECT_INDEX);
10✔
584
        $objIndex = $this->model->getFieldIndex('p', Constants::OBJECT_INDEX);
10✔
585
        $rm = $this->getRoleManager();
10✔
586

587
        $roles = $this->getAllRoles();
10✔
588
        $isRole = array_flip($roles);
10✔
589

590
        if (!isset($this->model['p']['p'])) {
10✔
NEW
591
            return $permissions;
×
592
        }
593

594
        foreach ($this->model['p']['p']->policy as $rule) {
10✔
595
            $obj = $rule[$objIndex];
10✔
596
            if ($obj != $resource) {
10✔
597
                continue;
10✔
598
            }
599

600
            $sub = $rule[$subIndex];
10✔
601

602
            if (!isset($isRole[$sub])) {
10✔
603
                $permissions[] = $rule;
10✔
604
            } else {
605
                $users = $rm->getUsers($sub);
10✔
606

607
                foreach ($users as $user) {
10✔
608
                    $implicitRule = array_merge([], $rule);
10✔
609
                    $implicitRule[$subIndex] = $user;
10✔
610
                    $permissions[] = $implicitRule;
10✔
611
                }
612
            }
613
        }
614

615
        return $this->removeDumplicatePermissions($permissions);
10✔
616
    }
617

618
    /**
619
     * GetImplicitUsersForResourceByDomain return implicit user based on resource and domain.
620
     * Compared to GetImplicitUsersForResource, domain is supported.
621
     *
622
     * @param string $resource
623
     * @param string $domain
624
     *
625
     * @return array
626
     */
627
    public function getImplicitUsersForResourceByDomain(string $resource, string $domain): array
628
    {
629
        $permissions = [];
10✔
630
        $subIndex = $this->model->getFieldIndex('p', Constants::SUBJECT_INDEX);
10✔
631
        $objIndex = $this->model->getFieldIndex('p', Constants::OBJECT_INDEX);
10✔
632
        $domIndex = $this->model->getFieldIndex('p', Constants::DOMAIN_INDEX);
10✔
633
        $rm = $this->getRoleManager();
10✔
634

635
        $roles = $this->getAllRolesByDomain($domain);
10✔
636
        $isRole = array_flip($roles);
10✔
637

638
        if (!isset($this->model['p']['p'])) {
10✔
NEW
639
            return $permissions;
×
640
        }
641

642
        foreach ($this->model['p']['p']->policy as $rule) {
10✔
643
            $obj = $rule[$objIndex];
10✔
644
            if ($obj != $resource) {
10✔
645
                continue;
10✔
646
            }
647

648
            $sub = $rule[$subIndex];
10✔
649

650
            if (!isset($isRole[$sub])) {
10✔
651
                $permissions[] = $rule;
×
652
            } else {
653
                if ($rule[$domIndex] != $domain) {
10✔
654
                    continue;
10✔
655
                }
656

657
                $users = $rm->getUsers($sub, $domain);
10✔
658
                foreach ($users as $user) {
10✔
659
                    $implicitRule = array_merge([], $rule);
10✔
660
                    $implicitRule[$subIndex] = $user;
10✔
661
                    $permissions[] = $implicitRule;
10✔
662
                }
663
            }
664
        }
665

666
        return $this->removeDumplicatePermissions($permissions);
10✔
667
    }
668

669
    /**
670
     * GetAllUsersByDomain would get all users associated with the domain.
671
     *
672
     * @param string $domain
673
     * @return string[]
674
     * @throws CasbinException
675
     */
676
    public function getAllUsersByDomain(string $domain): array
677
    {
678
        $m = [];
10✔
679
        $g = $this->model['g']['g'] ?? null;
10✔
680
        $p = $this->model['p']['p'] ?? null;
10✔
681
        $users = [];
10✔
682
        $index = $this->model->getFieldIndex('p', Constants::DOMAIN_INDEX);
10✔
683

684
        $getUser = function (int $index, ?array $policies, string $domain, array $m): array {
10✔
685
            if (is_null($policies) || count($policies) == 0 || count($policies[0]) <= $index) {
10✔
UNCOV
686
                return [];
×
687
            }
688
            $res = [];
10✔
689
            foreach ($policies as $policy) {
10✔
690
                $ok = isset($m[$policy[0]]);
10✔
691
                if ($policy[$index] == $domain && !$ok) {
10✔
692
                    $res[] = $policy[0];
10✔
693
                    $m[$policy[0]] = [];
10✔
694
                }
695
            }
696
            return $res;
10✔
697
        };
10✔
698

699
        $users = array_merge($users, $getUser(2, $g?->policy, $domain, $m));
10✔
700
        $users = array_merge($users, $getUser($index, $p?->policy, $domain, $m));
10✔
701
        return $users;
10✔
702
    }
703

704
    /**
705
     * Gets the users that has a role inside a domain. Add by Gordon.
706
     *
707
     * @param string $name
708
     * @param string $domain
709
     *
710
     * @return array|null
711
     */
712
    public function getUsersForRoleInDomain(string $name, string $domain): ?array
713
    {
714
        return isset($this->model['g']['g']) ? $this->model['g']['g']->rm?->getUsers($name, $domain) : [];
10✔
715
    }
716

717
    /**
718
     * Gets the roles that a user has inside a domain.
719
     *
720
     * @param string $name
721
     * @param string $domain
722
     *
723
     * @return array|null
724
     */
725
    public function getRolesForUserInDomain(string $name, string $domain): ?array
726
    {
727
        return isset($this->model['g']['g']) ? $this->model['g']['g']->rm?->getRoles($name, $domain) : [];
10✔
728
    }
729

730
    /**
731
     * Gets permissions for a user or role inside a domain.
732
     *
733
     * @param string $name
734
     * @param string $domain
735
     *
736
     * @return array
737
     */
738
    public function getPermissionsForUserInDomain(string $name, string $domain): array
739
    {
740
        return $this->getFilteredPolicy(0, $name, $domain);
20✔
741
    }
742

743
    /**
744
     * Adds a role for a user inside a domain.
745
     * returns false if the user already has the role (aka not affected).
746
     *
747
     * @param string $user
748
     * @param string $role
749
     * @param string $domain
750
     *
751
     * @return bool
752
     */
753
    public function addRoleForUserInDomain(string $user, string $role, string $domain): bool
754
    {
755
        return $this->addGroupingPolicy($user, $role, $domain);
30✔
756
    }
757

758
    /**
759
     * Deletes a role for a user inside a domain.
760
     * Returns false if the user does not have the role (aka not affected).
761
     *
762
     * @param string $user
763
     * @param string $role
764
     * @param string $domain
765
     *
766
     * @return bool
767
     */
768
    public function deleteRoleForUserInDomain(string $user, string $role, string $domain): bool
769
    {
770
        return $this->removeGroupingPolicy($user, $role, $domain);
20✔
771
    }
772

773
    /**
774
     * DeleteRolesForUserInDomain deletes all roles for a user inside a domain.
775
     * Returns false if the user does not have any roles (aka not affected).
776
     *
777
     * @param string $user
778
     * @param string $domain
779
     *
780
     * @return bool
781
     */
782
    public function deleteRolesForUserInDomain(string $user, string $domain): bool
783
    {
784
        $roles = isset($this->model['g']['g']) ? $this->model['g']['g']->rm?->getRoles($user, $domain) : [];
10✔
785

786
        $rules = [];
10✔
787
        foreach ($roles ?? [] as $role) {
10✔
788
            $rules[] = [$user, $role, $domain];
10✔
789
        }
790

791
        return $this->removeGroupingPolicies($rules);
10✔
792
    }
793

794
    /**
795
     * DeleteAllUsersByDomain would delete all users associated with the domain.
796
     *
797
     * @param string $domain
798
     * @return bool
799
     */
800
    public function deleteAllUsersByDomain(string $domain): bool
801
    {
802
        $g = $this->model['g']['g'] ?? null;
20✔
803
        $p = $this->model['p']['p'] ?? null;
20✔
804
        $index = $this->model->getFieldIndex('p', Constants::DOMAIN_INDEX);
20✔
805

806
        $getUser = function (int $index, ?array $policies, string $domain): array {
20✔
807
            if (is_null($policies) || count($policies) == 0 || count($policies[0]) <= $index) {
20✔
UNCOV
808
                return [];
×
809
            }
810
            $res = [];
20✔
811
            foreach ($policies as $policy) {
20✔
812
                if ($policy[$index] == $domain) {
20✔
813
                    $res[] = $policy;
20✔
814
                }
815
            }
816
            return $res;
20✔
817
        };
20✔
818

819
        $users = $getUser(2, $g?->policy, $domain);
20✔
820
        $this->removeGroupingPolicies($users);
20✔
821
        $users = $getUser($index, $p?->policy, $domain);
20✔
822
        $this->removePolicies($users);
20✔
823
        return true;
20✔
824
    }
825

826
    /**
827
     * DeleteDomains would delete all associated users and roles.
828
     * It would delete all domains if parameter is not provided.
829
     *
830
     * @param string ...$domains
831
     * @return bool
832
     */
833
    public function deleteDomains(string ...$domains): bool
834
    {
835
        if (count($domains) == 0) {
10✔
836
            $this->clearPolicy();
10✔
837
            return true;
10✔
838
        }
839
        foreach ($domains as $domain) {
10✔
840
            $this->deleteAllUsersByDomain($domain);
10✔
841
        }
842
        return true;
10✔
843
    }
844

845
    /**
846
     * GetAllDomains would get all domains.
847
     *
848
     * @return array
849
     */
850
    public function getAllDomains(): array
851
    {
852
        return $this->getRoleManager()->getAllDomains();
20✔
853
    }
854

855
    /**
856
     * GetAllRolesByDomain would get all roles associated with the domain.
857
     * Note: Not applicable to Domains with inheritance relationship  (implicit roles)
858
     *
859
     * @param string $domain
860
     *
861
     * @return array
862
     */
863
    public function getAllRolesByDomain(string $domain): array
864
    {
865
        $g = $this->model['g']['g'] ?? null;
20✔
866
        $policies = $g ? $g->policy : [];
20✔
867
        $roles = [];
20✔
868
        $existMap = [];
20✔
869

870
        foreach ($policies as $policy) {
20✔
871
            if ($policy[count($policy) - 1] == $domain) {
20✔
872
                $role = $policy[count($policy) - 2];
20✔
873
                if (!isset($existMap[$role])) {
20✔
874
                    $roles[] = $role;
20✔
875
                    $existMap[$role] = true;
20✔
876
                }
877
            }
878
        }
879

880
        return $roles;
20✔
881
    }
882
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc