|
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
|
|
|
* @inheritdoc |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public function behaviors() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'verbs' => [ |
|
33
|
|
|
'class' => VerbFilter::className(), |
|
34
|
|
|
'actions' => [ |
|
35
|
|
|
'logout' => ['post'], |
|
36
|
|
|
'delete' => ['post'], |
|
37
|
|
|
], |
|
38
|
|
|
], |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return string |
|
44
|
|
|
* @throws NotFoundHttpException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function actionIndex() |
|
47
|
|
|
{ |
|
48
|
|
|
$model = $this->findModel(); |
|
49
|
|
|
return $this->render('index', [ |
|
50
|
|
|
'model' => $model, |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Logs in a user. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string|\yii\web\Response |
|
58
|
|
|
*/ |
|
59
|
|
|
public function actionLogin() |
|
60
|
|
|
{ |
|
61
|
|
|
if (!Yii::$app->user->isGuest) { |
|
62
|
|
|
return $this->processGoHome(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$model = new LoginForm(); |
|
66
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
|
67
|
|
|
return $this->goBack(); |
|
68
|
|
|
} else { |
|
69
|
|
|
return $this->render('login', [ |
|
70
|
|
|
'model' => $model, |
|
71
|
|
|
]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Logs out the current user. |
|
77
|
|
|
* |
|
78
|
|
|
* @return \yii\web\Response |
|
79
|
|
|
*/ |
|
80
|
|
|
public function actionLogout() |
|
81
|
|
|
{ |
|
82
|
|
|
Yii::$app->user->logout(); |
|
83
|
|
|
return $this->processGoHome(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Signs user up. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string|\yii\web\Response |
|
90
|
|
|
*/ |
|
91
|
|
|
public function actionSignup() |
|
92
|
|
|
{ |
|
93
|
|
|
$model = new SignupForm(); |
|
94
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
95
|
|
|
if ($model->signup()) { |
|
96
|
|
|
return $this->processGoHome(Module::t('module', 'It remains to activate the account.')); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
return $this->render('signup', [ |
|
100
|
|
|
'model' => $model, |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param mixed $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 string|\yii\web\Response |
|
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
|
|
|
return $this->render('requestPasswordResetToken', [ |
|
139
|
|
|
'model' => $model, |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Resets password. |
|
145
|
|
|
* |
|
146
|
|
|
* @param mixed $token |
|
147
|
|
|
* @return string|\yii\web\Response |
|
148
|
|
|
* @throws BadRequestHttpException |
|
149
|
|
|
*/ |
|
150
|
|
|
public function actionResetPassword($token) |
|
151
|
|
|
{ |
|
152
|
|
|
try { |
|
153
|
|
|
$model = new ResetPasswordForm($token); |
|
154
|
|
|
} catch (InvalidParamException $e) { |
|
155
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
159
|
|
|
if($this->processResetPassword($model)) { |
|
160
|
|
|
return $this->processGoHome(Module::t('module', 'Password changed successfully.')); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
return $this->render('resetPassword', [ |
|
164
|
|
|
'model' => $model, |
|
165
|
|
|
]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param ResetPasswordForm $model |
|
170
|
|
|
* @return bool|\yii\web\Response |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function processResetPassword($model) |
|
173
|
|
|
{ |
|
174
|
|
|
if ($model->validate() && $model->resetPassword()) { |
|
175
|
|
|
return true; |
|
176
|
|
|
} |
|
177
|
|
|
return false; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Finds the User model based on its primary key value. |
|
182
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
|
183
|
|
|
* @return null|User the loaded model |
|
184
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function findModel() |
|
187
|
|
|
{ |
|
188
|
|
|
if (!Yii::$app->user->isGuest) { |
|
189
|
|
|
/** @var \modules\users\models\User $identity */ |
|
190
|
|
|
$identity = Yii::$app->user->identity; |
|
191
|
|
|
if (($model = User::findOne($identity->id)) !== null) { |
|
192
|
|
|
return $model; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.')); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $message |
|
200
|
|
|
* @param string $type |
|
201
|
|
|
* @return \yii\web\Response |
|
202
|
|
|
*/ |
|
203
|
|
|
public function processGoHome($message = '', $type = 'success') |
|
204
|
|
|
{ |
|
205
|
|
|
if (!empty($message)) { |
|
206
|
|
|
Yii::$app->session->setFlash($type, $message); |
|
207
|
|
|
} |
|
208
|
|
|
return $this->goHome(); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|