1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mdm\admin\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use mdm\admin\models\form\Login; |
7
|
|
|
use mdm\admin\models\form\PasswordResetRequest; |
8
|
|
|
use mdm\admin\models\form\ResetPassword; |
9
|
|
|
use mdm\admin\models\form\Signup; |
10
|
|
|
use mdm\admin\models\form\ChangePassword; |
11
|
|
|
use mdm\admin\models\User; |
12
|
|
|
use mdm\admin\models\searchs\User as UserSearch; |
13
|
|
|
use yii\base\InvalidParamException; |
14
|
|
|
use yii\web\BadRequestHttpException; |
15
|
|
|
use yii\web\Controller; |
16
|
|
|
use yii\filters\VerbFilter; |
17
|
|
|
use yii\web\NotFoundHttpException; |
18
|
|
|
use yii\base\UserException; |
19
|
|
|
use yii\mail\BaseMailer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* User controller |
23
|
|
|
*/ |
24
|
|
|
class UserController extends Controller |
25
|
|
|
{ |
26
|
|
|
private $_oldMailPath; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
'verbs' => [ |
35
|
|
|
'class' => VerbFilter::className(), |
36
|
|
|
'actions' => [ |
37
|
|
|
'delete' => ['post'], |
38
|
|
|
'logout' => ['post'], |
39
|
|
|
'activate' => ['post'], |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function beforeAction($action) |
49
|
|
|
{ |
50
|
|
|
if (parent::beforeAction($action)) { |
51
|
|
|
if (Yii::$app->has('mailer') && ($mailer = Yii::$app->getMailer()) instanceof BaseMailer) { |
52
|
|
|
/* @var $mailer BaseMailer */ |
53
|
|
|
$this->_oldMailPath = $mailer->getViewPath(); |
54
|
|
|
$mailer->setViewPath('@mdm/admin/mail'); |
55
|
|
|
} |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function afterAction($action, $result) |
65
|
|
|
{ |
66
|
|
|
if ($this->_oldMailPath !== null) { |
67
|
|
|
Yii::$app->getMailer()->setViewPath($this->_oldMailPath); |
68
|
|
|
} |
69
|
|
|
return parent::afterAction($action, $result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Lists all User models. |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$searchModel = new UserSearch(); |
79
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
80
|
|
|
|
81
|
|
|
return $this->render('index', [ |
82
|
|
|
'searchModel' => $searchModel, |
83
|
|
|
'dataProvider' => $dataProvider, |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Displays a single User model. |
89
|
|
|
* @param integer $id |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function actionView($id) |
93
|
|
|
{ |
94
|
|
|
return $this->render('view', [ |
95
|
|
|
'model' => $this->findModel($id), |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Deletes an existing User model. |
101
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
102
|
|
|
* @param integer $id |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function actionDelete($id) |
106
|
|
|
{ |
107
|
|
|
$this->findModel($id)->delete(); |
|
|
|
|
108
|
|
|
|
109
|
|
|
return $this->redirect(['index']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Login |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function actionLogin() |
117
|
|
|
{ |
118
|
|
|
if (!Yii::$app->getUser()->isGuest) { |
|
|
|
|
119
|
|
|
return $this->goHome(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$model = new Login(); |
123
|
|
|
if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) { |
124
|
|
|
return $this->goBack(); |
125
|
|
|
} else { |
126
|
|
|
return $this->render('login', [ |
127
|
|
|
'model' => $model, |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Logout |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function actionLogout() |
137
|
|
|
{ |
138
|
|
|
Yii::$app->getUser()->logout(); |
|
|
|
|
139
|
|
|
|
140
|
|
|
return $this->goHome(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Signup new user |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function actionSignup() |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$model = new Signup(); |
150
|
|
|
if ($model->load(Yii::$app->getRequest()->post())) { |
151
|
|
|
if ($user = $model->signup()) { |
|
|
|
|
152
|
|
|
return $this->goHome(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->render('signup', [ |
157
|
|
|
'model' => $model, |
158
|
|
|
]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Request reset password |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function actionRequestPasswordReset() |
166
|
|
|
{ |
167
|
|
|
$model = new PasswordResetRequest(); |
168
|
|
|
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) { |
169
|
|
|
if ($model->sendEmail()) { |
170
|
|
|
Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.'); |
|
|
|
|
171
|
|
|
|
172
|
|
|
return $this->goHome(); |
173
|
|
|
} else { |
174
|
|
|
Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.'); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this->render('requestPasswordResetToken', [ |
179
|
|
|
'model' => $model, |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Reset password |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function actionResetPassword($token) |
188
|
|
|
{ |
189
|
|
|
try { |
190
|
|
|
$model = new ResetPassword($token); |
191
|
|
|
} catch (InvalidParamException $e) { |
192
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate() && $model->resetPassword()) { |
196
|
|
|
Yii::$app->getSession()->setFlash('success', 'New password was saved.'); |
|
|
|
|
197
|
|
|
|
198
|
|
|
return $this->goHome(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this->render('resetPassword', [ |
202
|
|
|
'model' => $model, |
203
|
|
|
]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Reset password |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
public function actionChangePassword() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$model = new ChangePassword(); |
213
|
|
|
if ($model->load(Yii::$app->getRequest()->post()) && $model->change()) { |
214
|
|
|
return $this->goHome(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->render('change-password', [ |
218
|
|
|
'model' => $model, |
219
|
|
|
]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Activate new user |
224
|
|
|
* @param integer $id |
225
|
|
|
* @return type |
226
|
|
|
* @throws UserException |
227
|
|
|
* @throws NotFoundHttpException |
228
|
|
|
*/ |
229
|
|
|
public function actionActivate($id) |
230
|
|
|
{ |
231
|
|
|
/* @var $user User */ |
232
|
|
|
$user = $this->findModel($id); |
233
|
|
|
if ($user->status == User::STATUS_INACTIVE) { |
234
|
|
|
$user->status = User::STATUS_ACTIVE; |
235
|
|
|
if ($user->save()) { |
236
|
|
|
return $this->goHome(); |
237
|
|
|
} else { |
238
|
|
|
$errors = $user->firstErrors; |
239
|
|
|
throw new UserException(reset($errors)); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
return $this->goHome(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Finds the User model based on its primary key value. |
247
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
248
|
|
|
* @param integer $id |
249
|
|
|
* @return User the loaded model |
250
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
251
|
|
|
*/ |
252
|
|
|
protected function findModel($id) |
253
|
|
|
{ |
254
|
|
|
if (($model = User::findOne($id)) !== null) { |
255
|
|
|
return $model; |
256
|
|
|
} else { |
257
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
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.