1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace inblank\activeuser\controllers\backend; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use inblank\activeuser\models\User; |
7
|
|
|
use inblank\activeuser\models\UserSearch; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\web\NotFoundHttpException; |
10
|
|
|
use yii\filters\VerbFilter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* UserController implements the CRUD actions for User model. |
14
|
|
|
*/ |
15
|
|
|
class UserController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @inheritdoc |
19
|
|
|
*/ |
20
|
|
|
public function behaviors() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
'verbs' => [ |
24
|
|
|
'class' => VerbFilter::className(), |
25
|
|
|
'actions' => [ |
26
|
|
|
'delete' => ['POST'], |
27
|
|
|
'change-password' => ['POST'], |
28
|
|
|
'block' => ['POST'], |
29
|
|
|
], |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Lists all User models. |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function actionIndex() |
39
|
|
|
{ |
40
|
|
|
$searchModel = new UserSearch(); |
41
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
42
|
|
|
|
43
|
|
|
return $this->render('index', [ |
44
|
|
|
'searchModel' => $searchModel, |
45
|
|
|
'dataProvider' => $dataProvider, |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Displays a single User model. |
51
|
|
|
* @param integer $id |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function actionView($id) |
55
|
|
|
{ |
56
|
|
|
return $this->render('view', [ |
57
|
|
|
'model' => $this->findModel($id), |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Creates a new User model. |
63
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function actionCreate() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$model = new User(); |
69
|
|
|
|
70
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->create(false)) { |
71
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
72
|
|
|
} else { |
73
|
|
|
return $this->render('create', [ |
74
|
|
|
'model' => $model, |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Updates an existing User model. |
81
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
82
|
|
|
* @param integer $id |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function actionUpdate($id) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$model = $this->findModel($id); |
88
|
|
|
|
89
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
90
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
91
|
|
|
} else { |
92
|
|
|
return $this->render('update', [ |
93
|
|
|
'model' => $model, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Deletes an existing User model. |
100
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
101
|
|
|
* @param integer $id |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
public function actionDelete($id) |
105
|
|
|
{ |
106
|
|
|
$this->findModel($id)->delete(); |
107
|
|
|
|
108
|
|
|
return $this->redirect(['index']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Change user password |
113
|
|
|
* @param int $id user identifier |
114
|
|
|
* @return \yii\web\Response |
115
|
|
|
*/ |
116
|
|
|
public function actionChangePassword($id) |
117
|
|
|
{ |
118
|
|
|
$user = $this->findModel($id); |
119
|
|
|
$user->password = null; |
120
|
|
|
$user->newPassword(false); |
121
|
|
|
return $this->redirect(['view', 'id' => $id]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Block user |
126
|
|
|
* @param int $id user identifier |
127
|
|
|
* @return \yii\web\Response |
128
|
|
|
*/ |
129
|
|
|
public function actionBlock($id) |
130
|
|
|
{ |
131
|
|
|
$this->findModel($id)->block(false); |
132
|
|
|
return $this->redirect(['view', 'id' => $id]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Unblock user |
137
|
|
|
* @param int $id user identifier |
138
|
|
|
* @return \yii\web\Response |
139
|
|
|
*/ |
140
|
|
|
public function actionUnblock($id) |
141
|
|
|
{ |
142
|
|
|
$this->findModel($id)->unblock(false); |
143
|
|
|
return $this->redirect(['view', 'id' => $id]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Finds the User model based on its primary key value. |
148
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
149
|
|
|
* @param integer $id |
150
|
|
|
* @return User the loaded model |
151
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
152
|
|
|
*/ |
153
|
|
|
protected function findModel($id) |
154
|
|
|
{ |
155
|
|
|
if (($model = User::findOne($id)) !== null) { |
156
|
|
|
return $model; |
157
|
|
|
} else { |
158
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
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.