1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mdm\admin\components; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use mdm\admin\models\AuthItem; |
7
|
|
|
use mdm\admin\models\searchs\AuthItem as AuthItemSearch; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\web\NotFoundHttpException; |
10
|
|
|
use yii\base\NotSupportedException; |
11
|
|
|
use yii\filters\VerbFilter; |
12
|
|
|
use yii\rbac\Item; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* AuthItemController implements the CRUD actions for AuthItem model. |
16
|
|
|
* |
17
|
|
|
* @property integer $type |
18
|
|
|
* @property array $labels |
19
|
|
|
* |
20
|
|
|
* @author Misbahul D Munir <[email protected]> |
21
|
|
|
* @since 1.0 |
22
|
|
|
*/ |
23
|
|
|
class ItemController extends Controller |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function behaviors() |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
'verbs' => [ |
33
|
|
|
'class' => VerbFilter::className(), |
34
|
|
|
'actions' => [ |
35
|
|
|
'delete' => ['post'], |
36
|
|
|
'assign' => ['post'], |
37
|
|
|
'remove' => ['post'], |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Lists all AuthItem models. |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$searchModel = new AuthItemSearch(['type' => $this->type]); |
50
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); |
51
|
|
|
|
52
|
|
|
return $this->render('index', [ |
53
|
|
|
'dataProvider' => $dataProvider, |
54
|
|
|
'searchModel' => $searchModel, |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Displays a single AuthItem model. |
60
|
|
|
* @param string $id |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function actionView($id) |
64
|
|
|
{ |
65
|
|
|
$model = $this->findModel($id); |
66
|
|
|
|
67
|
|
|
return $this->render('view', ['model' => $model]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Creates a new AuthItem model. |
72
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function actionCreate() |
76
|
|
|
{ |
77
|
|
|
$model = new AuthItem(null); |
78
|
|
|
$model->type = $this->type; |
79
|
|
|
if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) { |
80
|
|
|
return $this->redirect(['view', 'id' => $model->name]); |
81
|
|
|
} else { |
82
|
|
|
return $this->render('create', ['model' => $model]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Updates an existing AuthItem model. |
88
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
89
|
|
|
* @param string $id |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function actionUpdate($id) |
93
|
|
|
{ |
94
|
|
|
$model = $this->findModel($id); |
95
|
|
|
if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) { |
96
|
|
|
return $this->redirect(['view', 'id' => $model->name]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->render('update', ['model' => $model]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Deletes an existing AuthItem model. |
104
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
105
|
|
|
* @param string $id |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function actionDelete($id) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$model = $this->findModel($id); |
111
|
|
|
Configs::authManager()->remove($model->item); |
|
|
|
|
112
|
|
|
Helper::invalidate(); |
113
|
|
|
|
114
|
|
|
return $this->redirect(['index']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Assign items |
119
|
|
|
* @param string $id |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function actionAssign($id) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$items = Yii::$app->getRequest()->post('items', []); |
125
|
|
|
$model = $this->findModel($id); |
126
|
|
|
$success = $model->addChildren($items); |
127
|
|
|
Yii::$app->getResponse()->format = 'json'; |
128
|
|
|
|
129
|
|
|
return array_merge($model->getItems(), ['success' => $success]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Assign items |
134
|
|
|
* @param string $id |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function actionGetUsers($id) |
138
|
|
|
{ |
139
|
|
|
$page = Yii::$app->getRequest()->get('page', 0); |
140
|
|
|
$model = $this->findModel($id); |
141
|
|
|
Yii::$app->getResponse()->format = 'json'; |
142
|
|
|
|
143
|
|
|
return array_merge($model->getUsers($page)); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Assign or remove items |
148
|
|
|
* @param string $id |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function actionRemove($id) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$items = Yii::$app->getRequest()->post('items', []); |
154
|
|
|
$model = $this->findModel($id); |
155
|
|
|
$success = $model->removeChildren($items); |
156
|
|
|
Yii::$app->getResponse()->format = 'json'; |
157
|
|
|
|
158
|
|
|
return array_merge($model->getItems(), ['success' => $success]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
public function getViewPath() |
165
|
|
|
{ |
166
|
|
|
return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'item'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Label use in view |
171
|
|
|
* @throws NotSupportedException |
172
|
|
|
*/ |
173
|
|
|
public function labels() |
174
|
|
|
{ |
175
|
|
|
throw new NotSupportedException(get_class($this) . ' does not support labels().'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Type of Auth Item. |
180
|
|
|
* @return integer |
181
|
|
|
*/ |
182
|
|
|
public function getType() |
183
|
|
|
{ |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Finds the AuthItem model based on its primary key value. |
189
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
190
|
|
|
* @param string $id |
191
|
|
|
* @return AuthItem the loaded model |
192
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
193
|
|
|
*/ |
194
|
|
|
protected function findModel($id) |
195
|
|
|
{ |
196
|
|
|
$auth = Configs::authManager(); |
197
|
|
|
$item = $this->type === Item::TYPE_ROLE ? $auth->getRole($id) : $auth->getPermission($id); |
198
|
|
|
if ($item) { |
199
|
|
|
return new AuthItem($item); |
200
|
|
|
} else { |
201
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.