1 | <?php |
||
2 | |||
3 | namespace Potievdev\SlimRbac\Component; |
||
4 | |||
5 | use Doctrine\ORM\Query\QueryException; |
||
6 | use Potievdev\SlimRbac\Models\RepositoryRegistry; |
||
7 | |||
8 | class RbacAccessChecker |
||
9 | { |
||
10 | /** @var RepositoryRegistry $repositoryRegistry */ |
||
11 | private $repositoryRegistry; |
||
12 | |||
13 | /** |
||
14 | * @param RepositoryRegistry $repositoryRegistry |
||
15 | */ |
||
16 | public function __construct(RepositoryRegistry $repositoryRegistry) |
||
17 | { |
||
18 | $this->repositoryRegistry = $repositoryRegistry; |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Checks access status. |
||
23 | * |
||
24 | * @throws QueryException |
||
25 | */ |
||
26 | public function hasAccess(string $userId, string $permissionName): bool |
||
27 | { |
||
28 | /** @var integer $permissionId */ |
||
29 | $permissionId = $this->repositoryRegistry |
||
30 | ->getPermissionRepository() |
||
31 | ->getPermissionIdByName($permissionName); |
||
32 | |||
33 | if ($permissionId === null) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
34 | return false; |
||
35 | } |
||
36 | |||
37 | /** @var integer[] $rootRoleIds */ |
||
38 | $rootRoleIds = $this->repositoryRegistry |
||
39 | ->getUserRoleRepository() |
||
40 | ->getUserRoleIds($userId); |
||
41 | |||
42 | if (count($rootRoleIds) == 0) { |
||
43 | return false; |
||
44 | } |
||
45 | |||
46 | $allRoleIds = $this->repositoryRegistry |
||
47 | ->getRoleHierarchyRepository() |
||
48 | ->getAllRoleIdsHierarchy($rootRoleIds); |
||
49 | |||
50 | return $this->repositoryRegistry |
||
51 | ->getRolePermissionRepository() |
||
52 | ->isPermissionAssigned($permissionId, $allRoleIds); |
||
53 | } |
||
54 | } |