|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Potievdev\SlimRbac\Models\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\Query\QueryException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* UserRoleRepository |
|
10
|
|
|
* |
|
11
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
|
12
|
|
|
* repository methods below. |
|
13
|
|
|
*/ |
|
14
|
|
|
class RoleHierarchyRepository extends EntityRepository |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Finding child identifier in roles three where $parentRoleId is in the top of three |
|
18
|
|
|
* @throws QueryException |
|
19
|
|
|
*/ |
|
20
|
|
|
public function hasChildRoleId(int $parentRoleId, int $findingChildId): bool |
|
21
|
|
|
{ |
|
22
|
|
|
$childIds = $this->getChildIds([$parentRoleId]); |
|
23
|
|
|
|
|
24
|
|
|
if (count($childIds) > 0) { |
|
25
|
|
|
|
|
26
|
|
|
if (in_array($findingChildId, $childIds)) { |
|
27
|
|
|
return true; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
foreach ($childIds as $childId) { |
|
31
|
|
|
|
|
32
|
|
|
if ($this->hasChildRoleId($childId, $findingChildId) == true) { |
|
|
|
|
|
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param integer[] $rootRoleIds |
|
44
|
|
|
* @return integer[] |
|
45
|
|
|
* @throws QueryException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getAllRoleIdsHierarchy(array $rootRoleIds): array |
|
48
|
|
|
{ |
|
49
|
|
|
$childRoleIds = $this->getAllChildRoleIds($rootRoleIds); |
|
50
|
|
|
|
|
51
|
|
|
return array_merge($rootRoleIds, $childRoleIds); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns all hierarchically child role ids for given parent role ids. |
|
56
|
|
|
* |
|
57
|
|
|
* @param integer[] $parentIds |
|
58
|
|
|
* @return integer[] |
|
59
|
|
|
* @throws QueryException |
|
60
|
|
|
*/ |
|
61
|
|
|
private function getAllChildRoleIds(array $parentIds): array |
|
62
|
|
|
{ |
|
63
|
|
|
$allChildIds = []; |
|
64
|
|
|
|
|
65
|
|
|
while (count($parentIds) > 0) { |
|
66
|
|
|
$parentIds = $this->getChildIds($parentIds); |
|
67
|
|
|
$allChildIds = array_merge($allChildIds, $parentIds); |
|
68
|
|
|
}; |
|
69
|
|
|
|
|
70
|
|
|
return $allChildIds; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns array of child role ids for given parent role ids. |
|
75
|
|
|
* |
|
76
|
|
|
* @param integer[] $parentIds |
|
77
|
|
|
* @return integer[] |
|
78
|
|
|
* @throws QueryException |
|
79
|
|
|
*/ |
|
80
|
|
|
private function getChildIds(array $parentIds): array |
|
81
|
|
|
{ |
|
82
|
|
|
$qb = $this->createQueryBuilder('roleHierarchy'); |
|
83
|
|
|
|
|
84
|
|
|
$qb->select('roleHierarchy.childRoleId') |
|
85
|
|
|
->where($qb->expr()->in( 'roleHierarchy.parentRoleId', $parentIds)) |
|
86
|
|
|
->indexBy('roleHierarchy', 'roleHierarchy.childRoleId'); |
|
87
|
|
|
|
|
88
|
|
|
$childRoleIds = $qb->getQuery()->getArrayResult(); |
|
89
|
|
|
|
|
90
|
|
|
return array_keys($childRoleIds); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.