1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\rbac\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\InvalidArgumentException; |
7
|
|
|
use yii\data\ArrayDataProvider; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\filters\VerbFilter; |
10
|
|
|
use yii\filters\AccessControl; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\web\IdentityInterface; |
13
|
|
|
use yii\web\NotFoundHttpException; |
14
|
|
|
use yii\web\BadRequestHttpException; |
15
|
|
|
use yii\base\Action; |
16
|
|
|
use yii\web\Response; |
17
|
|
|
use Exception; |
18
|
|
|
use modules\rbac\models\Assignment; |
19
|
|
|
use modules\users\models\User; |
20
|
|
|
use modules\rbac\Module; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class AssignController |
24
|
|
|
* @package modules\rbac\controllers |
25
|
|
|
*/ |
26
|
|
|
class AssignController extends Controller |
27
|
|
|
{ |
28
|
|
|
/** @var User */ |
29
|
|
|
private $user; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Action $action |
33
|
|
|
* @return bool |
34
|
|
|
* @throws InvalidConfigException |
35
|
|
|
* @throws BadRequestHttpException |
36
|
|
|
*/ |
37
|
|
|
public function beforeAction($action) |
38
|
|
|
{ |
39
|
|
|
if (empty(Yii::$app->controller->module->params['userClass'])) { |
40
|
|
|
throw new InvalidConfigException( |
41
|
|
|
Module::translate( |
42
|
|
|
'module', |
43
|
|
|
'You must specify the User class in the module settings.' |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
$this->user = new Yii::$app->controller->module->params['userClass'](); |
48
|
|
|
if (!($this->user instanceof IdentityInterface)) { |
49
|
|
|
throw new InvalidArgumentException( |
50
|
|
|
Module::translate( |
51
|
|
|
'module', |
52
|
|
|
'Class {:userClassName} does not implement interface yii\web\IdentityInterface.', |
53
|
|
|
[ |
54
|
|
|
':userClassName' => get_class($this->user) |
55
|
|
|
] |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
return parent::beforeAction($action); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function behaviors() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'access' => [ |
70
|
|
|
'class' => AccessControl::class, |
71
|
|
|
'rules' => [ |
72
|
|
|
[ |
73
|
|
|
'allow' => true, |
74
|
|
|
'roles' => ['managerRbac'] |
75
|
|
|
] |
76
|
|
|
] |
77
|
|
|
], |
78
|
|
|
'verbs' => [ |
79
|
|
|
'class' => VerbFilter::class, |
80
|
|
|
'actions' => [ |
81
|
|
|
'revoke' => ['POST'] |
82
|
|
|
] |
83
|
|
|
] |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function actionIndex() |
91
|
|
|
{ |
92
|
|
|
$assignModel = new Assignment(); |
93
|
|
|
$userModel = $this->user; |
94
|
|
|
$users = $userModel::find()->all(); |
95
|
|
|
$dataProvider = new ArrayDataProvider([ |
96
|
|
|
'allModels' => $users, |
97
|
|
|
'sort' => [ |
98
|
|
|
'attributes' => ['username', 'role'] |
99
|
|
|
], |
100
|
|
|
'pagination' => [ |
101
|
|
|
'defaultPageSize' => 25 |
102
|
|
|
] |
103
|
|
|
]); |
104
|
|
|
return $this->render('index', [ |
105
|
|
|
'dataProvider' => $dataProvider, |
106
|
|
|
'assignModel' => $assignModel |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string|int $id |
112
|
|
|
* @return mixed |
113
|
|
|
* @throws NotFoundHttpException |
114
|
|
|
*/ |
115
|
|
|
public function actionView($id) |
116
|
|
|
{ |
117
|
|
|
$assignModel = new Assignment(); |
118
|
|
|
return $this->render('view', [ |
119
|
|
|
'model' => $this->findModel($id), |
120
|
|
|
'assignModel' => $assignModel |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string|int $id |
126
|
|
|
* @return string|Response |
127
|
|
|
* @throws NotFoundHttpException |
128
|
|
|
* @throws Exception |
129
|
|
|
*/ |
130
|
|
|
public function actionUpdate($id) |
131
|
|
|
{ |
132
|
|
|
$model = new Assignment([ |
133
|
|
|
'user' => $this->findModel($id) |
134
|
|
|
]); |
135
|
|
|
if ($model->load(Yii::$app->request->post())) { |
136
|
|
|
$auth = Yii::$app->authManager; |
137
|
|
|
$role = $auth->getRole($model->role); |
138
|
|
|
// отвязываем роли если есть |
139
|
|
|
if ($auth->getRolesByUser($model->user->id)) { |
140
|
|
|
$auth->revokeAll($model->user->id); |
141
|
|
|
} |
142
|
|
|
// Привязываем новую роль |
143
|
|
|
if ($auth->assign($role, $model->user->id)) { |
144
|
|
|
return $this->redirect(['view', 'id' => $model->user->id]); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
$model->role = $model->getRoleUser($id); |
148
|
|
|
return $this->render('update', [ |
149
|
|
|
'model' => $model |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string|int $id |
155
|
|
|
* @return Response |
156
|
|
|
* @throws NotFoundHttpException |
157
|
|
|
*/ |
158
|
|
|
public function actionRevoke($id) |
159
|
|
|
{ |
160
|
|
|
$model = $this->findModel($id); |
161
|
|
|
$auth = Yii::$app->authManager; |
162
|
|
|
/** @var yii\web\Session $session */ |
163
|
|
|
$session = Yii::$app->session; |
164
|
|
|
if ($auth->getRolesByUser($model->id)) { |
165
|
|
|
if ($auth->revokeAll($model->id)) { |
166
|
|
|
$session->setFlash( |
167
|
|
|
'success', |
168
|
|
|
Module::translate( |
169
|
|
|
'module', |
170
|
|
|
'User "{:username}" successfully unassigned.', |
171
|
|
|
[ |
172
|
|
|
':username' => $model->username |
173
|
|
|
] |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
} else { |
177
|
|
|
$session->setFlash( |
178
|
|
|
'error', |
179
|
|
|
Module::translate( |
180
|
|
|
'module', |
181
|
|
|
'Error!' |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} else { |
186
|
|
|
$session->setFlash( |
187
|
|
|
'warning', |
188
|
|
|
Module::translate( |
189
|
|
|
'module', |
190
|
|
|
'User "{:username}" is not attached to any role!', |
191
|
|
|
[ |
192
|
|
|
':username' => $model->username |
193
|
|
|
] |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
return $this->redirect(['index']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Finds the User model based on its primary key value. |
202
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
203
|
|
|
* @param string|int $id |
204
|
|
|
* @return User the loaded model |
205
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
206
|
|
|
*/ |
207
|
|
|
protected function findModel($id) |
208
|
|
|
{ |
209
|
|
|
$userModel = $this->user; |
210
|
|
|
if (($model = $userModel::findOne(['id' => $id])) !== null) { |
211
|
|
|
return $model; |
212
|
|
|
} |
213
|
|
|
throw new NotFoundHttpException( |
214
|
|
|
Module::translate( |
215
|
|
|
'module', |
216
|
|
|
'The requested page does not exist.' |
217
|
|
|
) |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|