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-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\rbac; |
12
|
|
|
|
13
|
|
|
use yii\base\InvalidConfigException; |
14
|
|
|
use yii\rbac\Assignment; |
15
|
|
|
use yii\rbac\Item; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Smart setters for AuthManager. |
19
|
|
|
* |
20
|
|
|
* @author Andrii Vasyliev <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
trait SetterTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Set permission. |
26
|
|
|
* @param string $name |
27
|
|
|
* @param string $description |
28
|
|
|
* @return Item |
29
|
|
|
*/ |
30
|
|
|
public function setPermission($name, $description = null) |
31
|
13 |
|
{ |
32
|
|
|
$permission = $this->getPermission($name) ?: $this->createPermission($name); |
|
|
|
|
33
|
13 |
|
if ($description) { |
34
|
13 |
|
$permission->description = $description; |
35
|
|
|
} |
36
|
|
|
$this->add($permission); |
|
|
|
|
37
|
13 |
|
|
38
|
|
|
return $permission; |
39
|
13 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set role. |
43
|
|
|
* @param string $name |
44
|
|
|
* @param string $description |
45
|
|
|
* @return Item |
46
|
|
|
*/ |
47
|
|
|
public function setRole($name, $description = null) |
48
|
13 |
|
{ |
49
|
|
|
$role = $this->getRole($name) ?: $this->createRole($name); |
|
|
|
|
50
|
13 |
|
if ($description) { |
51
|
13 |
|
$role->description = $description; |
52
|
|
|
} |
53
|
|
|
$this->add($role); |
54
|
13 |
|
|
55
|
|
|
return $role; |
56
|
13 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set child. |
60
|
|
|
* @param string|Item $parent |
61
|
|
|
* @param string|Item $child |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function setChild($parent, $child) |
65
|
13 |
|
{ |
66
|
|
|
if (is_string($parent)) { |
67
|
13 |
|
$name = $parent; |
68
|
13 |
|
$parent = $this->getItem($parent); |
|
|
|
|
69
|
13 |
|
if (is_null($parent)) { |
70
|
13 |
|
throw new InvalidConfigException("Unknown parent:$name at setChild"); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
if (is_string($child)) { |
74
|
13 |
|
$name = $child; |
75
|
|
|
$child = $this->getItem($child); |
76
|
|
|
if (is_null($child)) { |
77
|
|
|
throw new InvalidConfigException("Unknown child:$name at setChild"); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (isset($this->children[$parent->name][$child->name])) { |
81
|
13 |
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->addChild($parent, $child); |
|
|
|
|
85
|
13 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Assigns an item (role or permission) to a user. |
89
|
|
|
* @param string|Item $item |
90
|
|
|
* @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
91
|
|
|
* @throws \Exception when given wrong item name |
92
|
|
|
* @return Assignment|null the assignment object or `null` when assignment was not found by name |
93
|
|
|
*/ |
94
|
|
|
public function setAssignment($item, $userId) |
95
|
26 |
|
{ |
96
|
|
|
try { |
97
|
|
|
if (is_string($item)) { |
98
|
26 |
|
$item = $this->findItem($item); |
99
|
26 |
|
} |
100
|
|
|
} catch (InvalidConfigException $e) { |
101
|
|
|
$this->warning('Role or permission "' . $item . '" does not exist'); |
102
|
|
|
|
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (isset($this->assignments[$userId][$item->name])) { |
107
|
26 |
|
return $this->assignments[$userId][$item->name]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->assign($item, $userId); |
|
|
|
|
111
|
26 |
|
} |
112
|
|
|
|
113
|
|
|
protected function findItem($name, $description = null) |
114
|
26 |
|
{ |
115
|
|
|
$item = $this->getItem($name); |
116
|
26 |
|
if ($item) { |
117
|
26 |
|
return $item; |
118
|
26 |
|
} |
119
|
|
|
if (strncmp($name, 'deny:', 5) === 0) { |
120
|
|
|
return $this->setPermission($name, $description); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
throw new InvalidConfigException("Unknown item:$name at findItem"); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Assigns items to a user. |
128
|
|
|
* @param string|array $items |
129
|
|
|
* @param string|integer $userId |
130
|
|
|
*/ |
131
|
|
|
public function setAssignments($items, $userId) |
132
|
10 |
|
{ |
133
|
|
|
if (is_string($items)) { |
134
|
10 |
|
$items = explode(',', $items); |
135
|
10 |
|
} |
136
|
|
|
foreach ($items as $item) { |
137
|
10 |
|
$this->setAssignment($item, $userId); |
138
|
10 |
|
} |
139
|
|
|
} |
140
|
10 |
|
|
141
|
|
|
/** |
142
|
|
|
* Returns all assignments in the system. |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getAllAssignments() |
146
|
|
|
{ |
147
|
|
|
return $this->assignments; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns all items in the system. |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function getAllItems() |
155
|
26 |
|
{ |
156
|
|
|
return $this->items; |
157
|
26 |
|
} |
158
|
|
|
|
159
|
|
|
protected function warning(string $message) |
160
|
|
|
{ |
161
|
|
|
return class_exists('Yii') ? \Yii::warning($message) : \yii\helpers\Yii::warning($message); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|