Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
PermissionHandlingTrait | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
12 | |
100.00% |
1 / 1 |
setPermissions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addPermissions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
addPermission | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
removePermission | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
getPermissions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasPermission | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Account |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Account; |
16 | |
17 | /** |
18 | * Permission handling trait. |
19 | * |
20 | * @package phpOMS\Account |
21 | * @license OMS License 2.0 |
22 | * @link https://jingga.app |
23 | * @since 1.0.0 |
24 | */ |
25 | trait PermissionHandlingTrait |
26 | { |
27 | /** |
28 | * Permissions. |
29 | * |
30 | * @var PermissionAbstract[] |
31 | * @since 1.0.0 |
32 | */ |
33 | public array $permissions = []; |
34 | |
35 | /** |
36 | * Set permissions. |
37 | * |
38 | * The method accepts an array of permissions. All existing permissions are replaced. |
39 | * |
40 | * @param PermissionAbstract[] $permissions Permissions |
41 | * |
42 | * @return void |
43 | * |
44 | * @since 1.0.0 |
45 | */ |
46 | public function setPermissions(array $permissions) : void |
47 | { |
48 | $this->permissions = $permissions; |
49 | } |
50 | |
51 | /** |
52 | * Add permissions. |
53 | * |
54 | * Adds permissions |
55 | * |
56 | * @param array<array|PermissionAbstract> $permissions Array of permissions to add |
57 | * |
58 | * @return void |
59 | * |
60 | * @since 1.0.0 |
61 | */ |
62 | public function addPermissions(array $permissions) : void |
63 | { |
64 | foreach ($permissions as $permission) { |
65 | if (\is_array($permission)) { |
66 | $this->permissions = \array_merge($this->permissions, $permission); |
67 | } else { |
68 | $this->permissions[] = $permission; |
69 | } |
70 | } |
71 | } |
72 | |
73 | /** |
74 | * Add permission. |
75 | * |
76 | * Adds a single permission |
77 | * |
78 | * @param PermissionAbstract $permission Permission to add |
79 | * |
80 | * @return void |
81 | * |
82 | * @since 1.0.0 |
83 | */ |
84 | public function addPermission(PermissionAbstract $permission) : void |
85 | { |
86 | $this->permissions[] = $permission; |
87 | } |
88 | |
89 | /** |
90 | * Remove permission. |
91 | * |
92 | * @param PermissionAbstract $permission Permission to remove |
93 | * |
94 | * @return void |
95 | * |
96 | * @since 1.0.0 |
97 | */ |
98 | public function removePermission(PermissionAbstract $permission) : void |
99 | { |
100 | foreach ($this->permissions as $key => $p) { |
101 | if ($p->isEqual($permission)) { |
102 | unset($this->permissions[$key]); |
103 | } |
104 | } |
105 | } |
106 | |
107 | /** |
108 | * Get permissions. |
109 | * |
110 | * @return PermissionAbstract[] |
111 | * |
112 | * @since 1.0.0 |
113 | */ |
114 | public function getPermissions() : array |
115 | { |
116 | return $this->permissions; |
117 | } |
118 | |
119 | /** |
120 | * Has permissions. |
121 | * |
122 | * Checks if the permission is defined |
123 | * |
124 | * @param int $permission Permission to check |
125 | * @param null|int $unit Unit Unit to check (null if all are acceptable) |
126 | * @param null|int $app App App to check (null if all are acceptable) |
127 | * @param null|string $module Module Module to check (null if all are acceptable) |
128 | * @param null|int $category Type (e.g. customer) (null if all are acceptable) |
129 | * @param null|int $element (e.g. customer id) (null if all are acceptable) |
130 | * @param null|int $component (e.g. address) (null if all are acceptable) |
131 | * |
132 | * @return bool Returns true if the permission is set, false otherwise |
133 | * |
134 | * @since 1.0.0 |
135 | */ |
136 | public function hasPermission( |
137 | int $permission, |
138 | int $unit = null, |
139 | int $app = null, |
140 | string $module = null, |
141 | int $category = null, |
142 | int $element = null, |
143 | int $component = null |
144 | ) : bool |
145 | { |
146 | foreach ($this->permissions as $p) { |
147 | if ($p->hasPermission($permission, $unit, $app, $module, $category, $element, $component)) { |
148 | return true; |
149 | } |
150 | } |
151 | |
152 | return false; |
153 | } |
154 | } |