|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\admin\models\forms; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Exception; |
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
use app\models\entity\AuthItem; |
|
9
|
|
|
|
|
10
|
|
|
class AuthItemForm extends \yii\base\Model |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
public $name; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public $description; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
public $roles; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
public $permissions; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
public $created_at; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
public $updated_at; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var \app\models\entity\AuthItem |
|
38
|
|
|
*/ |
|
39
|
|
|
private $model; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array The validation rules |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function rules() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
2 |
|
[['name', 'description'], 'required'], |
|
48
|
|
|
[['roles', 'permissions'], 'safe'], |
|
49
|
|
|
|
|
50
|
2 |
|
['name', 'unique', 'targetClass' => AuthItem::class, 'filter' => function ($query) { |
|
51
|
|
|
if (!$this->model()->isNewRecord) { |
|
52
|
|
|
$query->andWhere(['not', ['name' => $this->model()->name]]); |
|
53
|
|
|
} |
|
54
|
2 |
|
}], |
|
55
|
|
|
['name', 'string', 'max' => 64], |
|
56
|
|
|
['name', 'match', 'pattern' => '/^[a-z]\w*$/i'], |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function attributeLabels() |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
2 |
|
'name' => Yii::t('app', 'Title'), |
|
67
|
2 |
|
'description' => Yii::t('app', 'Description'), |
|
68
|
2 |
|
'roles' => Yii::t('app', 'Inherit role'), |
|
69
|
2 |
|
'permissions' => Yii::t('app', 'Permissions'), |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function attributeHints() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return [ |
|
79
|
|
|
'name' => Yii::t('app', 'Only latin letters'), |
|
80
|
1 |
|
'roles' => Yii::t('app', 'The role will receive all the privileges of the selected role'), |
|
81
|
|
|
'permissions' => Yii::t('app', 'Selected sections will be available for editing'), |
|
82
|
1 |
|
]; |
|
83
|
1 |
|
} |
|
84
|
1 |
|
|
|
85
|
1 |
|
/** |
|
86
|
|
|
* Set model |
|
87
|
1 |
|
* |
|
88
|
1 |
|
* @param AuthItem $model |
|
89
|
1 |
|
*/ |
|
90
|
|
|
public function setModel(AuthItem $model): void |
|
91
|
1 |
|
{ |
|
92
|
1 |
|
$this->model = $model; |
|
93
|
1 |
|
|
|
94
|
1 |
|
$this->name = $model->name; |
|
95
|
|
|
$this->description = $model->description; |
|
96
|
|
|
$this->created_at = $model->created_at; |
|
97
|
|
|
$this->updated_at = $model->updated_at; |
|
98
|
|
|
|
|
99
|
|
|
$permissions = Yii::$app->authManager->getPermissionsByRole($this->name); |
|
100
|
|
|
$this->permissions = ArrayHelper::index($permissions, 'name', []); |
|
101
|
2 |
|
$this->permissions = array_keys($this->permissions); |
|
102
|
|
|
|
|
103
|
2 |
|
$roles = Yii::$app->authManager->getChildren($this->name); |
|
104
|
1 |
|
$this->roles = ArrayHelper::index($roles, 'name', []); |
|
105
|
|
|
$this->roles = array_keys($this->roles); |
|
106
|
|
|
} |
|
107
|
2 |
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get model |
|
110
|
|
|
* |
|
111
|
|
|
* @return AuthItem |
|
112
|
|
|
*/ |
|
113
|
|
|
public function model(): AuthItem |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->model === null) { |
|
116
|
|
|
$this->model = new AuthItem(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->model; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Save auth item |
|
124
|
|
|
* |
|
125
|
|
|
* @throws Exception |
|
126
|
|
|
* @return AuthItem |
|
127
|
|
|
*/ |
|
128
|
|
|
public function save(): AuthItem |
|
129
|
|
|
{ |
|
130
|
|
|
$model = $this->model(); |
|
131
|
|
|
|
|
132
|
|
|
$model->name = $this->name; |
|
133
|
|
|
$model->description = $this->description; |
|
134
|
|
|
$model->type = \yii\rbac\Item::TYPE_ROLE; |
|
135
|
2 |
|
|
|
136
|
|
|
if (!$model->save()) { |
|
137
|
2 |
|
throw new Exception(Yii::t('app', 'An error occurred while saving authItem')); |
|
138
|
2 |
|
} |
|
139
|
2 |
|
|
|
140
|
2 |
|
if (!$model->isSuperUser()) { |
|
141
|
|
|
$this->assignRolesAndPermissions(); |
|
142
|
|
|
} |
|
143
|
2 |
|
|
|
144
|
|
|
return $model; |
|
145
|
2 |
|
} |
|
146
|
2 |
|
|
|
147
|
|
|
public function permissionsList(): array |
|
148
|
2 |
|
{ |
|
149
|
|
|
$list = Yii::$app->authManager->getPermissions(); |
|
150
|
|
|
return ArrayHelper::map($list, 'name', function ($row) { |
|
151
|
|
|
return Yii::t('app', $row->description); |
|
152
|
|
|
}); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function rolesList(): array |
|
156
|
|
|
{ |
|
157
|
|
|
$list = Yii::$app->authManager->getRoles(); |
|
158
|
|
|
unset($list[$this->model->name]); |
|
159
|
|
|
|
|
160
|
|
|
return ArrayHelper::map($list, 'name', 'description'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function assignRolesAndPermissions(): void |
|
164
|
|
|
{ |
|
165
|
|
|
$auth = Yii::$app->authManager; |
|
166
|
|
|
|
|
167
|
|
|
$role = $auth->getRole($this->model->name); |
|
168
|
|
|
$roles = $auth->getRoles(); |
|
169
|
|
|
$permissions = $auth->getPermissions(); |
|
170
|
|
|
|
|
171
|
|
|
$auth->removeChildren($role); |
|
172
|
|
|
|
|
173
|
|
|
if (is_array($this->roles)) { |
|
174
|
|
|
foreach ($this->roles as $r) { |
|
175
|
|
|
$auth->addChild($role, $roles[$r]); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (is_array($this->permissions)) { |
|
180
|
|
|
$currPermissions = ArrayHelper::index( |
|
181
|
|
|
$auth->getPermissionsByRole($this->model->name), |
|
182
|
|
|
'name', |
|
183
|
|
|
[] |
|
184
|
|
|
); |
|
185
|
|
|
foreach ($this->permissions as $permission) { |
|
186
|
|
|
if (!array_key_exists($permission, $currPermissions)) { |
|
187
|
|
|
$auth->addChild($role, $permissions[$permission]); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|