|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mdm\admin\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\rbac\Item; |
|
7
|
|
|
use yii\helpers\Json; |
|
8
|
|
|
use yii\base\Model; |
|
9
|
|
|
use mdm\admin\components\Helper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is the model class for table "tbl_auth_item". |
|
13
|
|
|
* |
|
14
|
|
|
* @property string $name |
|
15
|
|
|
* @property integer $type |
|
16
|
|
|
* @property string $description |
|
17
|
|
|
* @property string $ruleName |
|
18
|
|
|
* @property string $data |
|
19
|
|
|
* |
|
20
|
|
|
* @property Item $item |
|
21
|
|
|
* |
|
22
|
|
|
* @author Misbahul D Munir <[email protected]> |
|
23
|
|
|
* @since 1.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class AuthItem extends Model |
|
26
|
|
|
{ |
|
27
|
|
|
public $name; |
|
28
|
|
|
public $type; |
|
29
|
|
|
public $description; |
|
30
|
|
|
public $ruleName; |
|
31
|
|
|
public $data; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Item |
|
34
|
|
|
*/ |
|
35
|
|
|
private $_item; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Initialize object |
|
39
|
|
|
* @param Item $item |
|
40
|
|
|
* @param array $config |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($item = null, $config = []) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->_item = $item; |
|
45
|
|
|
if ($item !== null) { |
|
46
|
|
|
$this->name = $item->name; |
|
47
|
|
|
$this->type = $item->type; |
|
48
|
|
|
$this->description = $item->description; |
|
49
|
|
|
$this->ruleName = $item->ruleName; |
|
50
|
|
|
$this->data = $item->data === null ? null : Json::encode($item->data); |
|
51
|
|
|
} |
|
52
|
|
|
parent::__construct($config); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function rules() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
[['ruleName'], 'checkRule'], |
|
62
|
|
|
[['name', 'type'], 'required'], |
|
63
|
|
|
[['name'], 'unique', 'when' => function() { |
|
64
|
|
|
return $this->isNewRecord || ($this->_item->name != $this->name); |
|
|
|
|
|
|
65
|
|
|
}], |
|
66
|
|
|
[['type'], 'integer'], |
|
67
|
|
|
[['description', 'data', 'ruleName'], 'default'], |
|
68
|
|
|
[['name'], 'string', 'max' => 64] |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Check role is unique |
|
74
|
|
|
*/ |
|
75
|
|
|
public function unique() |
|
76
|
|
|
{ |
|
77
|
|
|
$authManager = Yii::$app->authManager; |
|
78
|
|
|
$value = $this->name; |
|
79
|
|
|
if ($authManager->getRole($value) !== null || $authManager->getPermission($value) !== null) { |
|
80
|
|
|
$message = Yii::t('yii', '{attribute} "{value}" has already been taken.'); |
|
81
|
|
|
$params = [ |
|
82
|
|
|
'attribute' => $this->getAttributeLabel('name'), |
|
83
|
|
|
'value' => $value, |
|
84
|
|
|
]; |
|
85
|
|
|
$this->addError('name', Yii::$app->getI18n()->format($message, $params, Yii::$app->language)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Check for rule |
|
91
|
|
|
*/ |
|
92
|
|
|
public function checkRule() |
|
93
|
|
|
{ |
|
94
|
|
|
$name = $this->ruleName; |
|
95
|
|
|
if (!Yii::$app->getAuthManager()->getRule($name)) { |
|
96
|
|
|
try { |
|
97
|
|
|
$rule = Yii::createObject($name); |
|
98
|
|
|
if ($rule instanceof \yii\rbac\Rule) { |
|
99
|
|
|
$rule->name = $name; |
|
100
|
|
|
Yii::$app->getAuthManager()->add($rule); |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->addError('ruleName', Yii::t('rbac-admin', 'Invalid rule "{value}"', ['value' => $name])); |
|
103
|
|
|
} |
|
104
|
|
|
} catch (\Exception $exc) { |
|
105
|
|
|
$this->addError('ruleName', Yii::t('rbac-admin', 'Rule "{value}" does not exists', ['value' => $name])); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @inheritdoc |
|
112
|
|
|
*/ |
|
113
|
|
|
public function attributeLabels() |
|
114
|
|
|
{ |
|
115
|
|
|
return [ |
|
116
|
|
|
'name' => Yii::t('rbac-admin', 'Name'), |
|
117
|
|
|
'type' => Yii::t('rbac-admin', 'Type'), |
|
118
|
|
|
'description' => Yii::t('rbac-admin', 'Description'), |
|
119
|
|
|
'ruleName' => Yii::t('rbac-admin', 'Rule Name'), |
|
120
|
|
|
'data' => Yii::t('rbac-admin', 'Data'), |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Check if is new record. |
|
126
|
|
|
* @return boolean |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getIsNewRecord() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->_item === null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Find role |
|
135
|
|
|
* @param string $id |
|
136
|
|
|
* @return null|\self |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function find($id) |
|
139
|
|
|
{ |
|
140
|
|
|
$item = Yii::$app->authManager->getRole($id); |
|
141
|
|
|
if ($item !== null) { |
|
142
|
|
|
return new self($item); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return null; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Save role to [[\yii\rbac\authManager]] |
|
150
|
|
|
* @return boolean |
|
151
|
|
|
*/ |
|
152
|
|
|
public function save() |
|
153
|
|
|
{ |
|
154
|
|
|
if ($this->validate()) { |
|
155
|
|
|
$manager = Yii::$app->authManager; |
|
156
|
|
|
if ($this->_item === null) { |
|
157
|
|
|
if ($this->type == Item::TYPE_ROLE) { |
|
158
|
|
|
$this->_item = $manager->createRole($this->name); |
|
159
|
|
|
} else { |
|
160
|
|
|
$this->_item = $manager->createPermission($this->name); |
|
161
|
|
|
} |
|
162
|
|
|
$isNew = true; |
|
163
|
|
|
} else { |
|
164
|
|
|
$isNew = false; |
|
165
|
|
|
$oldName = $this->_item->name; |
|
166
|
|
|
} |
|
167
|
|
|
$this->_item->name = $this->name; |
|
168
|
|
|
$this->_item->description = $this->description; |
|
169
|
|
|
$this->_item->ruleName = $this->ruleName; |
|
170
|
|
|
$this->_item->data = $this->data === null || $this->data === '' ? null : Json::decode($this->data); |
|
171
|
|
|
if ($isNew) { |
|
172
|
|
|
$manager->add($this->_item); |
|
173
|
|
|
} else { |
|
174
|
|
|
$manager->update($oldName, $this->_item); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
Helper::invalidate(); |
|
177
|
|
|
return true; |
|
178
|
|
|
} else { |
|
179
|
|
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Adds an item as a child of another item. |
|
185
|
|
|
* @param array $items |
|
186
|
|
|
* @return int |
|
187
|
|
|
*/ |
|
188
|
|
View Code Duplication |
public function addChildren($items) |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
$manager = Yii::$app->getAuthManager(); |
|
191
|
|
|
$success = 0; |
|
192
|
|
|
if ($this->_item) { |
|
193
|
|
|
foreach ($items as $name) { |
|
194
|
|
|
$child = $manager->getPermission($name); |
|
195
|
|
|
if ($this->type == Item::TYPE_ROLE && $child === null) { |
|
196
|
|
|
$child = $manager->getRole($name); |
|
197
|
|
|
} |
|
198
|
|
|
try { |
|
199
|
|
|
$manager->addChild($this->_item, $child); |
|
200
|
|
|
$success++; |
|
201
|
|
|
} catch (\Exception $exc) { |
|
202
|
|
|
Yii::error($exc->getMessage(), __METHOD__); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
if ($success > 0) { |
|
207
|
|
|
Helper::invalidate(); |
|
208
|
|
|
} |
|
209
|
|
|
return $success; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Remove an item as a child of another item. |
|
214
|
|
|
* @param array $items |
|
215
|
|
|
* @return int |
|
216
|
|
|
*/ |
|
217
|
|
View Code Duplication |
public function removeChildren($items) |
|
|
|
|
|
|
218
|
|
|
{ |
|
219
|
|
|
$manager = Yii::$app->getAuthManager(); |
|
220
|
|
|
$success = 0; |
|
221
|
|
|
if ($this->_item !== null) { |
|
222
|
|
|
foreach ($items as $name) { |
|
223
|
|
|
$child = $manager->getPermission($name); |
|
224
|
|
|
if ($this->type == Item::TYPE_ROLE && $child === null) { |
|
225
|
|
|
$child = $manager->getRole($name); |
|
226
|
|
|
} |
|
227
|
|
|
try { |
|
228
|
|
|
$manager->removeChild($this->_item, $child); |
|
229
|
|
|
$success++; |
|
230
|
|
|
} catch (\Exception $exc) { |
|
231
|
|
|
Yii::error($exc->getMessage(), __METHOD__); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
if ($success > 0) { |
|
236
|
|
|
Helper::invalidate(); |
|
237
|
|
|
} |
|
238
|
|
|
return $success; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get items |
|
243
|
|
|
* @return array |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getItems() |
|
246
|
|
|
{ |
|
247
|
|
|
$manager = Yii::$app->getAuthManager(); |
|
248
|
|
|
$avaliable = []; |
|
249
|
|
|
if ($this->type == Item::TYPE_ROLE) { |
|
250
|
|
View Code Duplication |
foreach ($manager->getRoles() as $item) { |
|
|
|
|
|
|
251
|
|
|
$avaliable[$item->name] = [ |
|
252
|
|
|
'type' => 'role', |
|
253
|
|
|
'desc' => $item->description, |
|
254
|
|
|
]; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
foreach ($manager->getPermissions() as $item) { |
|
259
|
|
|
$avaliable[$item->name] = [ |
|
260
|
|
|
'type' => $item->name[0] == '/' ? 'route' : 'permission' |
|
261
|
|
|
]; |
|
262
|
|
|
if ($item->name[0] != '/') { |
|
263
|
|
|
$avaliable[$item->name]['desc'] = $item->description; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
$assigned = []; |
|
269
|
|
|
foreach ($manager->getChildren($this->_item->name) as $item) { |
|
270
|
|
|
$assigned[$item->name] = [ |
|
271
|
|
|
'type' => $item->type == 1 ? 'role' : ($item->name[0] == '/' ? 'route' : 'permission'), |
|
272
|
|
|
'desc' => $item->description, |
|
273
|
|
|
]; |
|
274
|
|
|
unset($avaliable[$item->name]); |
|
275
|
|
|
} |
|
276
|
|
|
unset($avaliable[$this->name]); |
|
277
|
|
|
return[ |
|
278
|
|
|
'avaliable' => $avaliable, |
|
279
|
|
|
'assigned' => $assigned |
|
280
|
|
|
]; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Get item |
|
285
|
|
|
* @return Item |
|
286
|
|
|
*/ |
|
287
|
|
|
public function getItem() |
|
288
|
|
|
{ |
|
289
|
|
|
return $this->_item; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Get type name |
|
294
|
|
|
* @param mixed $type |
|
295
|
|
|
* @return string|array |
|
296
|
|
|
*/ |
|
297
|
|
|
public static function getTypeName($type = null) |
|
298
|
|
|
{ |
|
299
|
|
|
$result = [ |
|
300
|
|
|
Item::TYPE_PERMISSION => 'Permission', |
|
301
|
|
|
Item::TYPE_ROLE => 'Role' |
|
302
|
|
|
]; |
|
303
|
|
|
if ($type === null) { |
|
304
|
|
|
return $result; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
return $result[$type]; |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.