1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\users\controllers\frontend; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\Controller; |
7
|
|
|
use modules\users\models\User; |
8
|
|
|
use modules\users\models\SignupForm; |
9
|
|
|
use modules\users\models\LoginForm; |
10
|
|
|
use modules\users\models\EmailConfirmForm; |
11
|
|
|
use modules\users\models\ResetPasswordForm; |
12
|
|
|
use modules\users\models\PasswordResetRequestForm; |
13
|
|
|
use yii\base\InvalidParamException; |
14
|
|
|
use yii\web\BadRequestHttpException; |
15
|
|
|
use yii\filters\VerbFilter; |
16
|
|
|
use yii\web\NotFoundHttpException; |
17
|
|
|
use modules\users\Module; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DefaultController |
21
|
|
|
* @package modules\users\controllers\frontend |
22
|
|
|
*/ |
23
|
|
|
class DefaultController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function behaviors() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
'verbs' => [ |
32
|
|
|
'class' => VerbFilter::className(), |
33
|
|
|
'actions' => [ |
34
|
|
|
'logout' => ['post'], |
35
|
|
|
'delete' => ['post'], |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
* @throws NotFoundHttpException |
44
|
|
|
*/ |
45
|
|
|
public function actionIndex() |
46
|
|
|
{ |
47
|
|
|
$model = $this->findModel(); |
48
|
|
|
return $this->render('index', [ |
49
|
|
|
'model' => $model, |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Logs in a user. |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function actionLogin() |
59
|
|
|
{ |
60
|
|
|
if (!Yii::$app->user->isGuest) { |
61
|
|
|
return $this->processGoHome(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$model = new LoginForm(); |
65
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
66
|
|
|
return $this->goBack(); |
67
|
|
|
} else { |
68
|
|
|
return $this->render('login', [ |
69
|
|
|
'model' => $model, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Logs out the current user. |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function actionLogout() |
80
|
|
|
{ |
81
|
|
|
Yii::$app->user->logout(); |
82
|
|
|
return $this->processGoHome(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Signs user up. |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function actionSignup() |
91
|
|
|
{ |
92
|
|
|
$model = new SignupForm(); |
93
|
|
|
if ($model->load(Yii::$app->request->post())) { |
94
|
|
|
if ($model->signup()) { |
95
|
|
|
return $this->processGoHome(Module::t('module', 'It remains to activate the account.')); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->render('signup', [ |
100
|
|
|
'model' => $model, |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $token |
106
|
|
|
* @return \yii\web\Response |
107
|
|
|
* @throws BadRequestHttpException |
108
|
|
|
*/ |
109
|
|
|
public function actionEmailConfirm($token = '') |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
$model = new EmailConfirmForm($token); |
113
|
|
|
} catch (InvalidParamException $e) { |
114
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($model->confirmEmail()) { |
118
|
|
|
return $this->processGoHome(Module::t('module', 'Thank you for registering!')); |
119
|
|
|
} |
120
|
|
|
return $this->processGoHome(Module::t('module', 'Error sending message!'), 'error'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Requests password reset. |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function actionRequestPasswordReset() |
129
|
|
|
{ |
130
|
|
|
$model = new PasswordResetRequestForm(); |
131
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
132
|
|
|
if ($model->sendEmail()) { |
133
|
|
|
return $this->processGoHome(Module::t('module', 'Check your email for further instructions.')); |
134
|
|
|
} else { |
135
|
|
|
Yii::$app->session->setFlash('error', Module::t('module', 'Sorry, we are unable to reset password.')); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->render('requestPasswordResetToken', [ |
140
|
|
|
'model' => $model, |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Resets password. |
146
|
|
|
* |
147
|
|
|
* @param string $token |
148
|
|
|
* @return mixed |
149
|
|
|
* @throws BadRequestHttpException |
150
|
|
|
*/ |
151
|
|
|
public function actionResetPassword($token = '') |
152
|
|
|
{ |
153
|
|
|
try { |
154
|
|
|
$model = new ResetPasswordForm($token); |
155
|
|
|
} catch (InvalidParamException $e) { |
156
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
157
|
|
|
} |
158
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { |
159
|
|
|
return $this->processGoHome(Module::t('module', 'Password changed successfully.')); |
160
|
|
|
} |
161
|
|
|
return $this->render('resetPassword', [ |
162
|
|
|
'model' => $model, |
163
|
|
|
]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Finds the User model based on its primary key value. |
168
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
169
|
|
|
* @return User the loaded model |
170
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
protected function findModel() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
if (!Yii::$app->user->isGuest) { |
175
|
|
|
/** @var \modules\users\models\User $identity */ |
176
|
|
|
$identity = Yii::$app->user->identity; |
177
|
|
|
if (($model = User::findOne($identity->id)) !== null) { |
|
|
|
|
178
|
|
|
return $model; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.')); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $message |
186
|
|
|
* @param string $type |
187
|
|
|
* @return \yii\web\Response |
188
|
|
|
*/ |
189
|
|
|
public function processGoHome($message = '', $type = 'success') |
190
|
|
|
{ |
191
|
|
|
if (!empty($message)) { |
192
|
|
|
Yii::$app->session->setFlash($type, $message); |
193
|
|
|
} |
194
|
|
|
return $this->goHome(); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
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.