1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\rbac\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\data\ArrayDataProvider; |
7
|
|
|
use yii\web\Controller; |
8
|
|
|
use yii\filters\AccessControl; |
9
|
|
|
use yii\filters\VerbFilter; |
10
|
|
|
use yii\web\BadRequestHttpException; |
11
|
|
|
use yii\widgets\ActiveForm; |
12
|
|
|
use yii\web\Response; |
13
|
|
|
use Exception; |
14
|
|
|
use modules\rbac\models\Role; |
15
|
|
|
use modules\rbac\Module; |
16
|
|
|
use modules\rbac\traits\ModuleTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class RolesController |
20
|
|
|
* @package modules\rbac\controllers |
21
|
|
|
*/ |
22
|
|
|
class RolesController extends Controller |
23
|
|
|
{ |
24
|
|
|
use ModuleTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function behaviors() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'access' => [ |
34
|
|
|
'class' => AccessControl::class, |
35
|
|
|
'rules' => [ |
36
|
|
|
[ |
37
|
|
|
'allow' => true, |
38
|
|
|
'roles' => ['managerRbac'] |
39
|
|
|
] |
40
|
|
|
] |
41
|
|
|
], |
42
|
|
|
'verbs' => [ |
43
|
|
|
'class' => VerbFilter::class, |
44
|
|
|
'actions' => [ |
45
|
|
|
'delete' => ['POST'] |
46
|
|
|
] |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Lists all Role models. |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function actionIndex() |
56
|
|
|
{ |
57
|
|
|
$auth = Yii::$app->authManager; |
58
|
|
|
$dataProvider = new ArrayDataProvider([ |
59
|
|
|
'allModels' => $auth->getRoles(), |
60
|
|
|
'sort' => [ |
61
|
|
|
'attributes' => ['name', 'description', 'ruleName'] |
62
|
|
|
], |
63
|
|
|
'pagination' => [ |
64
|
|
|
'defaultPageSize' => 25 |
65
|
|
|
] |
66
|
|
|
]); |
67
|
|
|
return $this->render('index', [ |
68
|
|
|
'dataProvider' => $dataProvider |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Displays a single Role model. |
74
|
|
|
* @param string|int $id |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function actionView($id) |
78
|
|
|
{ |
79
|
|
|
$auth = Yii::$app->authManager; |
80
|
|
|
/** @var $role */ |
81
|
|
|
$role = $auth->getRole($id); |
82
|
|
|
|
83
|
|
|
$model = new Role(['name' => $role->name]); |
84
|
|
|
return $this->render('view', [ |
85
|
|
|
'role' => $role, |
86
|
|
|
'model' => $model |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Creates Role a new Role model. |
92
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
93
|
|
|
* @return array|string|Response |
94
|
|
|
* @throws Exception |
95
|
|
|
*/ |
96
|
|
|
public function actionCreate() |
97
|
|
|
{ |
98
|
|
|
$model = new Role(['scenario' => Role::SCENARIO_CREATE]); |
99
|
|
|
$model->isNewRecord = true; |
100
|
|
|
|
101
|
|
|
$load = $model->load(Yii::$app->request->post()); |
102
|
|
|
if ($load && $model->validate()) { |
103
|
|
|
$auth = Yii::$app->authManager; |
104
|
|
|
$role = $auth->createRole($model->name); |
105
|
|
|
$role->description = $model->description; |
106
|
|
|
if ($auth->add($role)) { |
107
|
|
|
return $this->redirect(['view', 'id' => $model->name]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $this->render('create', [ |
111
|
|
|
'model' => $model |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array|bool |
117
|
|
|
*/ |
118
|
|
|
public function actionAjaxValidateForm() |
119
|
|
|
{ |
120
|
|
|
$model = new Role(['scenario' => Role::SCENARIO_CREATE]); |
121
|
|
|
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
122
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
123
|
|
|
return ActiveForm::validate($model); |
124
|
|
|
} |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Updates an existing Role model. |
130
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
131
|
|
|
* @param string|int $id |
132
|
|
|
* @return string|Response |
133
|
|
|
* @throws Exception |
134
|
|
|
*/ |
135
|
|
|
public function actionUpdate($id) |
136
|
|
|
{ |
137
|
|
|
$auth = Yii::$app->authManager; |
138
|
|
|
/** @var $role */ |
139
|
|
|
$role = $auth->getRole($id); |
140
|
|
|
|
141
|
|
|
$model = new Role([ |
142
|
|
|
'scenario' => Role::SCENARIO_UPDATE, |
143
|
|
|
'name' => $role->name, |
144
|
|
|
'description' => $role->description |
145
|
|
|
]); |
146
|
|
|
if ($model->load(Yii::$app->request->post())) { |
147
|
|
|
$role->description = $model->description; |
148
|
|
|
if ($auth->update($id, $role)) { |
149
|
|
|
return $this->redirect(['view', 'id' => $id]); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
return $this->render('update', [ |
153
|
|
|
'model' => $model |
154
|
|
|
]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Привязываем роль |
159
|
|
|
* @return Response |
160
|
|
|
* @throws BadRequestHttpException |
161
|
|
|
* @throws Exception |
162
|
|
|
*/ |
163
|
|
|
public function actionAddRoles() |
164
|
|
|
{ |
165
|
|
|
$model = new Role([ |
166
|
|
|
'scenario' => Role::SCENARIO_UPDATE |
167
|
|
|
]); |
168
|
|
|
if ($model->load(Yii::$app->request->post())) { |
169
|
|
|
$auth = Yii::$app->authManager; |
170
|
|
|
/** @var $role */ |
171
|
|
|
$role = $auth->getRole($model->name); |
172
|
|
|
foreach ($model->itemsRoles as $value) { |
173
|
|
|
/** @var $add */ |
174
|
|
|
$add = $auth->getRole($value); |
175
|
|
|
// Проверяем, не является добовляемая роль родителем? |
176
|
|
|
$result = self::detectLoop($role, $add); |
177
|
|
|
if (!$result) { |
178
|
|
|
$auth->addChild($role, $add); |
179
|
|
|
} else { |
180
|
|
|
/** @var yii\web\Session $session */ |
181
|
|
|
$session = Yii::$app->session; |
182
|
|
|
$session->setFlash( |
183
|
|
|
'error', |
184
|
|
|
Module::translate( |
185
|
|
|
'module', |
186
|
|
|
'The role of the "{:parent}" is the parent of the "{:role}"!', |
187
|
|
|
[ |
188
|
|
|
':parent' => $add->name, |
189
|
|
|
':role' => $role->name |
190
|
|
|
] |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-roles']); |
196
|
|
|
} |
197
|
|
|
throw new BadRequestHttpException(Module::translate('module', 'Not a valid request to the method!')); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Отзываем роль |
202
|
|
|
* @return array|Response |
203
|
|
|
* @throws BadRequestHttpException |
204
|
|
|
*/ |
205
|
|
|
public function actionRemoveRoles() |
206
|
|
|
{ |
207
|
|
|
$model = new Role([ |
208
|
|
|
'scenario' => Role::SCENARIO_UPDATE |
209
|
|
|
]); |
210
|
|
|
if ($model->load(Yii::$app->request->post())) { |
211
|
|
|
$auth = Yii::$app->authManager; |
212
|
|
|
$role = $auth->getRole($model->name); |
213
|
|
|
foreach ($model->rolesByRole as $value) { |
214
|
|
|
$remove = $auth->getRole($value); |
215
|
|
|
$auth->removeChild($role, $remove); |
216
|
|
|
} |
217
|
|
|
return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-roles']); |
218
|
|
|
} |
219
|
|
|
throw new BadRequestHttpException(Module::translate('module', 'Not a valid request to the method!')); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Привязываем разрешение |
224
|
|
|
* @return array|Response |
225
|
|
|
* @throws BadRequestHttpException |
226
|
|
|
* @throws Exception |
227
|
|
|
*/ |
228
|
|
|
public function actionAddPermissions() |
229
|
|
|
{ |
230
|
|
|
$model = new Role([ |
231
|
|
|
'scenario' => Role::SCENARIO_UPDATE |
232
|
|
|
]); |
233
|
|
|
if ($model->load(Yii::$app->request->post())) { |
234
|
|
|
$auth = Yii::$app->authManager; |
235
|
|
|
/** @var $role */ |
236
|
|
|
$role = $auth->getRole($model->name); |
237
|
|
|
self::addChild($model->itemsPermissions, $role); |
238
|
|
|
return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-permissions']); |
239
|
|
|
} |
240
|
|
|
throw new BadRequestHttpException(Module::translate('module', 'Not a valid request to the method!')); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Отзываем разрешение |
245
|
|
|
* @return array|Response |
246
|
|
|
* @throws BadRequestHttpException |
247
|
|
|
*/ |
248
|
|
|
public function actionRemovePermissions() |
249
|
|
|
{ |
250
|
|
|
$model = new Role([ |
251
|
|
|
'scenario' => Role::SCENARIO_UPDATE |
252
|
|
|
]); |
253
|
|
|
if ($model->load(Yii::$app->request->post())) { |
254
|
|
|
$auth = Yii::$app->authManager; |
255
|
|
|
$role = $auth->getRole($model->name); |
256
|
|
|
foreach ($model->permissionsByRole as $value) { |
257
|
|
|
$remove = $auth->getPermission($value); |
258
|
|
|
$auth->removeChild($role, $remove); |
259
|
|
|
} |
260
|
|
|
return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-permissions']); |
261
|
|
|
} |
262
|
|
|
throw new BadRequestHttpException(Module::translate('module', 'Not a valid request to the method!')); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Deletes an existing Role model. |
267
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
268
|
|
|
* @param string|int $id |
269
|
|
|
* @return Response |
270
|
|
|
*/ |
271
|
|
|
public function actionDelete($id) |
272
|
|
|
{ |
273
|
|
|
$auth = Yii::$app->authManager; |
274
|
|
|
/** @var $role */ |
275
|
|
|
$role = $auth->getRole($id); |
276
|
|
|
/** @var yii\web\Session $session */ |
277
|
|
|
$session = Yii::$app->session; |
278
|
|
|
if ($auth->remove($role)) { |
279
|
|
|
$session->setFlash( |
280
|
|
|
'success', |
281
|
|
|
Module::translate( |
282
|
|
|
'module', |
283
|
|
|
'The role "{:name}" have been successfully deleted.', |
284
|
|
|
[ |
285
|
|
|
':name' => $role->name |
286
|
|
|
] |
287
|
|
|
) |
288
|
|
|
); |
289
|
|
|
} else { |
290
|
|
|
$session->setFlash('error', Module::translate('module', 'Error!')); |
291
|
|
|
} |
292
|
|
|
return $this->redirect(['index']); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|