1 | <?php |
||||||
2 | /** |
||||||
3 | * RBAC implementation for HiPanel |
||||||
4 | * |
||||||
5 | * @link https://github.com/hiqdev/hipanel-rbac |
||||||
6 | * @package hipanel-rbac |
||||||
7 | * @license BSD-3-Clause |
||||||
8 | * @copyright Copyright (c) 2016-2020, HiQDev (http://hiqdev.com/) |
||||||
9 | */ |
||||||
10 | |||||||
11 | namespace hipanel\rbac; |
||||||
12 | |||||||
13 | use Exception; |
||||||
14 | use hiqdev\yii\compat\yii; |
||||||
15 | use yii\rbac\Assignment; |
||||||
16 | use yii\rbac\Item; |
||||||
17 | |||||||
18 | /** |
||||||
19 | * Smart setters for AuthManager. |
||||||
20 | * |
||||||
21 | * @author Andrii Vasyliev <[email protected]> |
||||||
22 | */ |
||||||
23 | trait SetterTrait |
||||||
24 | { |
||||||
25 | /** |
||||||
26 | * Set permission. |
||||||
27 | * @param string $name |
||||||
28 | * @param string $description |
||||||
29 | * @return Item |
||||||
30 | */ |
||||||
31 | 1 | public function setPermission($name, $description = null) |
|||||
32 | { |
||||||
33 | 1 | $permission = $this->getPermission($name) ?: $this->createPermission($name); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
createPermission() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
34 | 1 | if ($description) { |
|||||
35 | 1 | $permission->description = $description; |
|||||
36 | } |
||||||
37 | 1 | $this->add($permission); |
|||||
0 ignored issues
–
show
It seems like
add() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
38 | |||||||
39 | 1 | return $permission; |
|||||
40 | } |
||||||
41 | |||||||
42 | /** |
||||||
43 | * Set role. |
||||||
44 | * @param string $name |
||||||
45 | * @param string $description |
||||||
46 | * @return Item |
||||||
47 | */ |
||||||
48 | 1 | public function setRole($name, $description = null) |
|||||
49 | { |
||||||
50 | 1 | $role = $this->getRole($name) ?: $this->createRole($name); |
|||||
0 ignored issues
–
show
It seems like
getRole() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
createRole() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
51 | 1 | if ($description) { |
|||||
52 | 1 | $role->description = $description; |
|||||
53 | } |
||||||
54 | 1 | $this->add($role); |
|||||
55 | |||||||
56 | 1 | return $role; |
|||||
57 | } |
||||||
58 | |||||||
59 | /** |
||||||
60 | * Set child. |
||||||
61 | * @param string|Item $parent |
||||||
62 | * @param string|Item $child |
||||||
63 | * @return bool |
||||||
64 | */ |
||||||
65 | 1 | public function setChild($parent, $child) |
|||||
66 | { |
||||||
67 | 1 | if (is_string($parent)) { |
|||||
68 | 1 | $name = $parent; |
|||||
69 | 1 | $parent = $this->getItem($parent); |
|||||
0 ignored issues
–
show
The method
getItem() does not exist on hipanel\rbac\SetterTrait . Did you maybe mean getAllItems() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
70 | 1 | if (is_null($parent)) { |
|||||
71 | throw new Exception("Unknown parent:$name at setChild"); |
||||||
72 | } |
||||||
73 | } |
||||||
74 | 1 | if (is_string($child)) { |
|||||
75 | $name = $child; |
||||||
76 | $child = $this->getItem($child); |
||||||
77 | if (is_null($child)) { |
||||||
78 | throw new Exception("Unknown child:$name at setChild"); |
||||||
79 | } |
||||||
80 | } |
||||||
81 | 1 | if (isset($this->children[$parent->name][$child->name])) { |
|||||
82 | 1 | return false; |
|||||
83 | } |
||||||
84 | |||||||
85 | 1 | return $this->addChild($parent, $child); |
|||||
0 ignored issues
–
show
It seems like
addChild() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
86 | } |
||||||
87 | |||||||
88 | /** |
||||||
89 | * Assigns an item (role or permission) to a user. |
||||||
90 | * @param string|Item $item |
||||||
91 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||||||
92 | * @throws Exception when given wrong item name |
||||||
93 | * @return Assignment|null the assignment object or `null` when assignment was not found by name |
||||||
94 | */ |
||||||
95 | 31 | public function setAssignment($item, $userId) |
|||||
96 | { |
||||||
97 | try { |
||||||
98 | 31 | if (is_string($item)) { |
|||||
99 | 31 | $item = $this->findItem($item); |
|||||
100 | } |
||||||
101 | } catch (Exception $e) { |
||||||
102 | yii::warning('Role or permission "' . $item . '" does not exist'); |
||||||
103 | |||||||
104 | return null; |
||||||
105 | } |
||||||
106 | |||||||
107 | 31 | if (isset($this->assignments[$userId][$item->name])) { |
|||||
108 | return $this->assignments[$userId][$item->name]; |
||||||
109 | } |
||||||
110 | |||||||
111 | 31 | return $this->assign($item, $userId); |
|||||
0 ignored issues
–
show
It seems like
assign() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
112 | } |
||||||
113 | |||||||
114 | 31 | protected function findItem($name, $description = null) |
|||||
115 | { |
||||||
116 | 31 | $item = $this->getItem($name); |
|||||
117 | 31 | if ($item) { |
|||||
118 | 31 | return $item; |
|||||
119 | } |
||||||
120 | if (strncmp($name, 'deny:', 5) === 0) { |
||||||
121 | return $this->setPermission($name, $description); |
||||||
122 | } |
||||||
123 | |||||||
124 | throw new Exception("Unknown item:$name at findItem"); |
||||||
125 | } |
||||||
126 | |||||||
127 | /** |
||||||
128 | * Assigns items to a user. |
||||||
129 | * @param string|array $items |
||||||
130 | * @param string|integer $userId |
||||||
131 | */ |
||||||
132 | 10 | public function setAssignments($items, $userId) |
|||||
133 | { |
||||||
134 | 10 | if (is_string($items)) { |
|||||
135 | 10 | $items = explode(',', $items); |
|||||
136 | } |
||||||
137 | 10 | foreach ($items as $item) { |
|||||
138 | 10 | $this->setAssignment($item, $userId); |
|||||
139 | } |
||||||
140 | 10 | } |
|||||
141 | |||||||
142 | /** |
||||||
143 | * Returns all assignments in the system. |
||||||
144 | * @return array |
||||||
145 | */ |
||||||
146 | public function getAllAssignments() |
||||||
147 | { |
||||||
148 | return $this->assignments; |
||||||
149 | } |
||||||
150 | |||||||
151 | /** |
||||||
152 | * Returns all items in the system. |
||||||
153 | * @return array |
||||||
154 | */ |
||||||
155 | 31 | public function getAllItems() |
|||||
156 | { |
||||||
157 | 31 | return $this->items; |
|||||
158 | } |
||||||
159 | } |
||||||
160 |