|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* RBAC implementation for HiPanel |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/hipanel-rbac |
|
7
|
|
|
* @package hipanel-rbac |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hipanel\rbac; |
|
13
|
|
|
|
|
14
|
|
|
use yii\base\InvalidParamException; |
|
15
|
|
|
use yii\rbac\Item; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* HiPanel AuthManager. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class AuthManager extends \yii\rbac\PhpManager |
|
23
|
|
|
{ |
|
24
|
|
|
public $itemFile = '@hipanel/rbac/files/items.php'; |
|
25
|
|
|
public $ruleFile = '@hipanel/rbac/files/rules.php'; |
|
26
|
|
|
public $assignmentFile = '@hipanel/rbac/files/assignments.php'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set permission. |
|
30
|
|
|
* @param string $name |
|
31
|
|
|
* @param string $description |
|
32
|
|
|
* @return Item |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function setPermission($name, $description = null) |
|
35
|
|
|
{ |
|
36
|
3 |
|
return $this->setItem('permission', $name, $description); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set role. |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* @param string $description |
|
43
|
|
|
* @return Item |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function setRole($name, $description = null) |
|
46
|
|
|
{ |
|
47
|
3 |
|
return $this->setItem('role', $name, $description); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set item by type and name. |
|
52
|
|
|
* Created if not exists else updates. |
|
53
|
|
|
* @param string $type |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* @param string $description |
|
56
|
|
|
* @return Item |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function setItem($type, $name, $description = null) |
|
59
|
|
|
{ |
|
60
|
3 |
|
$item = $this->getItem($name) ?: $this->createItem($type, $name); |
|
61
|
3 |
|
if ($description) { |
|
|
|
|
|
|
62
|
|
|
$item->description = $description; |
|
63
|
|
|
} |
|
64
|
3 |
|
$this->add($item); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
3 |
|
return $item; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create item by type and name. |
|
71
|
|
|
* @param string $type |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* @throws InvalidParamException |
|
74
|
|
|
* @return Item |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function createItem($type, $name) |
|
77
|
|
|
{ |
|
78
|
3 |
|
if ('role' === $type) { |
|
79
|
3 |
|
return $this->createRole($name); |
|
80
|
3 |
|
} elseif ('permission' === $type) { |
|
81
|
3 |
|
return $this->createPermission($name); |
|
82
|
|
|
} else { |
|
83
|
|
|
throw new InvalidParamException('Creating unsupported item type: ' . $type); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set child. |
|
89
|
|
|
* @param string|Item $parent |
|
90
|
|
|
* @param string|Item $child |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function setChild($parent, $child) |
|
94
|
|
|
{ |
|
95
|
3 |
View Code Duplication |
if (is_string($parent)) { |
|
|
|
|
|
|
96
|
3 |
|
$name = $parent; |
|
97
|
3 |
|
$parent = $this->getItem($parent); |
|
98
|
3 |
|
if (is_null($parent)) { |
|
99
|
|
|
throw new InvalidParamException("Unknown parent:$name at setChild"); |
|
100
|
|
|
} |
|
101
|
3 |
|
} |
|
102
|
3 |
View Code Duplication |
if (is_string($child)) { |
|
|
|
|
|
|
103
|
3 |
|
$name = $child; |
|
104
|
3 |
|
$child = $this->getItem($child); |
|
105
|
3 |
|
if (is_null($child)) { |
|
106
|
|
|
throw new InvalidParamException("Unknown child:$name at setChild"); |
|
107
|
|
|
} |
|
108
|
3 |
|
} |
|
109
|
3 |
|
if (isset($this->children[$parent->name][$child->name])) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
3 |
|
return $this->addChild($parent, $child); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
6 |
|
public function setAssignment($role, $userId) |
|
117
|
|
|
{ |
|
118
|
6 |
View Code Duplication |
if (is_string($role)) { |
|
|
|
|
|
|
119
|
6 |
|
$name = $role; |
|
120
|
6 |
|
$role = $this->getRole($role); |
|
121
|
6 |
|
if (is_null($role)) { |
|
122
|
|
|
throw new InvalidParamException("Unknown role:$name at setAssignment"); |
|
123
|
|
|
} |
|
124
|
6 |
|
} |
|
125
|
6 |
|
if (isset($this->assignments[$userId][$role->name])) { |
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
6 |
|
return $this->assign($role, $userId); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
6 |
|
public function getRoles() |
|
133
|
|
|
{ |
|
134
|
6 |
|
return $this->getItems(Item::TYPE_ROLE); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getPermissions() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->getItems(Item::TYPE_PERMISSION); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function getAllAssignments() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->assignments; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* We don't keep all the assignments, only basic. |
|
149
|
|
|
* @see forceSaveAssignments |
|
150
|
|
|
*/ |
|
151
|
6 |
|
protected function saveAssignments() |
|
152
|
|
|
{ |
|
153
|
6 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Create only basic assignments before saving. |
|
157
|
|
|
*/ |
|
158
|
3 |
|
public function saveBasicAssignments() |
|
159
|
|
|
{ |
|
160
|
3 |
|
parent::saveAssignments(); |
|
|
|
|
|
|
161
|
3 |
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: