1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\basic\controllers; |
4
|
|
|
|
5
|
|
|
use app\basic\forms\ContactForm; |
6
|
|
|
use app\basic\forms\LoginForm; |
7
|
|
|
use app\basic\forms\PasswordResetRequestForm; |
8
|
|
|
use app\basic\forms\ResetPasswordForm; |
9
|
|
|
use app\basic\forms\SignupForm; |
10
|
|
|
use app\basic\models\UserModels; |
11
|
|
|
use yii\captcha\CaptchaAction; |
12
|
|
|
use yii\exceptions\InvalidArgumentException; |
13
|
|
|
use yii\base\Model; |
14
|
|
|
use yii\mail\MailerInterface; |
15
|
|
|
use yii\web\BadRequestHttpException; |
16
|
|
|
use yii\web\Controller; |
17
|
|
|
use yii\web\ErrorAction; |
18
|
|
|
use yii\web\Response; |
19
|
|
|
use yii\web\filters\AccessControl; |
20
|
|
|
use yii\web\filters\VerbFilter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* SiteController is the controller Web Application Basic. |
24
|
|
|
**/ |
25
|
|
|
class SiteController extends Controller |
26
|
|
|
{ |
27
|
|
|
private $_User; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* behaviors |
31
|
|
|
* |
32
|
|
|
* @return array behaviors config. |
33
|
|
|
**/ |
34
|
|
|
public function behaviors() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
'access' => [ |
38
|
|
|
'__class' => AccessControl::class, |
39
|
|
|
'only' => ['logout'], |
40
|
|
|
'rules' => [ |
41
|
|
|
[ |
42
|
|
|
'actions' => ['logout'], |
43
|
|
|
'allow' => true, |
44
|
|
|
'roles' => ['@'], |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
'verbs' => [ |
49
|
|
|
'__class' => VerbFilter::class, |
50
|
|
|
'actions' => [ |
51
|
|
|
'logout' => ['POST'], |
52
|
|
|
], |
53
|
|
|
], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* actions |
59
|
|
|
* |
60
|
|
|
* @return array actions config. |
61
|
|
|
**/ |
62
|
|
|
public function actions() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
'error' => [ |
66
|
|
|
'__class' => ErrorAction::class, |
67
|
|
|
], |
68
|
|
|
'captcha' => [ |
69
|
|
|
'__class' => CaptchaAction::class, |
70
|
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* actionIndex |
77
|
|
|
* Displays homepage. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
**/ |
81
|
|
|
public function actionIndex() |
82
|
|
|
{ |
83
|
|
|
return $this->render('index'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* actionLogin |
88
|
|
|
* Login action. |
89
|
|
|
* |
90
|
|
|
* @return Response|string |
91
|
|
|
**/ |
92
|
|
|
public function actionLogin() |
93
|
|
|
{ |
94
|
|
|
if (!$this->app->user->isGuest) { |
95
|
|
|
return $this->goHome(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$model = new LoginForm($this->app); |
|
|
|
|
99
|
|
|
|
100
|
|
|
if ($model->load($this->app->request->post()) && $model->validate()) { |
101
|
|
|
$this->login($model); |
102
|
|
|
return $this->goBack(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$model->password = ''; |
106
|
|
|
|
107
|
|
|
return $this->render('login', [ |
108
|
|
|
'model' => $model, |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* actionLogout |
114
|
|
|
* Logout action. |
115
|
|
|
* |
116
|
|
|
* @return Response |
117
|
|
|
**/ |
118
|
|
|
public function actionLogout() |
119
|
|
|
{ |
120
|
|
|
$this->app->user->logout(); |
121
|
|
|
|
122
|
|
|
return $this->goHome(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* actionContact |
127
|
|
|
* Displays contact page. |
128
|
|
|
* |
129
|
|
|
* @return Response|string |
130
|
|
|
**/ |
131
|
|
|
public function actionContact() |
132
|
|
|
{ |
133
|
|
|
$model = new ContactForm(); |
134
|
|
|
|
135
|
|
|
if ($model->load($this->app->request->post()) && $model->validate()) { |
136
|
|
|
$this->sendContact($this->app->params['adminEmail'], $this->app->mailer, $model); |
137
|
|
|
$this->app->session->setFlash('contactFormSubmitted'); |
138
|
|
|
return $this->refresh(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->render('contact', [ |
142
|
|
|
'model' => $model, |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* actionAbout |
148
|
|
|
* Displays about page. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
**/ |
152
|
|
|
public function actionAbout() |
153
|
|
|
{ |
154
|
|
|
return $this->render('about'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* actionSignup |
159
|
|
|
* Signs user up. |
160
|
|
|
* |
161
|
|
|
* @return mixed |
162
|
|
|
**/ |
163
|
|
|
public function actionSignup() |
164
|
|
|
{ |
165
|
|
|
$model = new SignupForm(); |
166
|
|
|
|
167
|
|
|
if ($model->load($this->app->request->post())) { |
168
|
|
|
if ($this->_User = $model->signup()) { |
169
|
|
|
if ($this->app->getUser()->login($this->_User)) { |
170
|
|
|
return $this->goHome(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this->render('signup', [ |
176
|
|
|
'model' => $model, |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* actionRequestPasswordReset |
182
|
|
|
* Requests password reset. |
183
|
|
|
* |
184
|
|
|
* @return mixed |
185
|
|
|
**/ |
186
|
|
|
public function actionRequestPasswordReset() |
187
|
|
|
{ |
188
|
|
|
$model = new PasswordResetRequestForm(); |
189
|
|
|
|
190
|
|
|
if ($model->load($this->app->request->post()) && $model->validate()) { |
191
|
|
|
if ($this->sendResetPassword($this->app->mailer, $model)) { |
192
|
|
|
$this->app->session->setFlash( |
193
|
|
|
'success', |
194
|
|
|
$this->app->t('basic', 'Check your email for further instructions.') |
195
|
|
|
); |
196
|
|
|
return $this->goHome(); |
197
|
|
|
} |
198
|
|
|
$this->app->session->setFlash( |
199
|
|
|
'error', |
200
|
|
|
$this->app->t('basic', 'Sorry, we are unable to reset password for the provided email address.') |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->render('requestPasswordResetToken', [ |
205
|
|
|
'model' => $model, |
206
|
|
|
]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* actionResetPassword |
211
|
|
|
* Resets password. |
212
|
|
|
* |
213
|
|
|
* @param string $token |
214
|
|
|
* @return mixed |
215
|
|
|
* @throws BadRequestHttpException |
216
|
|
|
**/ |
217
|
|
|
public function actionResetPassword($token) |
218
|
|
|
{ |
219
|
|
|
try { |
220
|
|
|
$model = new ResetPasswordForm(); |
221
|
|
|
} catch (InvalidArgumentException $e) { |
222
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$user = new UserModels(); |
226
|
|
|
$tokenExpire = $this->app->params['user.passwordResetTokenExpire']; |
227
|
|
|
|
228
|
|
|
if (empty($token) || !is_string($token)) { |
229
|
|
|
$this->app->session->setFlash('danger', $this->app->t('basic', 'Password reset token cannot be blank.')); |
230
|
|
|
return $this->goHome(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if (!$user->findByPasswordResetToken($token, $tokenExpire)) { |
234
|
|
|
$this->app->session->setFlash('danger', $this->app->t('basic', 'Wrong password reset token.')); |
235
|
|
|
return $this->goHome(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ($model->load($this->app->request->post()) && $model->validate() && $model->resetPassword($token, $tokenExpire)) { |
239
|
|
|
$this->app->session->setFlash('success', $this->app->t('basic', 'New password saved.')); |
240
|
|
|
return $this->goHome(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this->render('resetPassword', [ |
244
|
|
|
'model' => $model, |
245
|
|
|
]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* login |
250
|
|
|
* Logs in a user using the provided username and password. |
251
|
|
|
* |
252
|
|
|
* @param Model $model. |
253
|
|
|
* @return bool whether the user is logged in successfully. |
254
|
|
|
**/ |
255
|
|
|
public function login(Model $model) |
256
|
|
|
{ |
257
|
|
|
return $this->app->user->login($model->getUser(), $model->rememberMe ? 3600 * 24 * 30 : 0); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* sendContactForm |
262
|
|
|
* Sends an email to the specified email address using the information collected by this model. |
263
|
|
|
* |
264
|
|
|
* @param string $email the target email address. |
265
|
|
|
* @param MailerInterface $mailer. |
266
|
|
|
* @param Model $model. |
267
|
|
|
* @return bool whether the model passes validation. |
268
|
|
|
**/ |
269
|
|
|
public function sendContact(string $email, MailerInterface $mailer, Model $model) |
270
|
|
|
{ |
271
|
|
|
$mailer->compose() |
272
|
|
|
->setTo($email) |
273
|
|
|
->setFrom([$model->email => $model->name]) |
274
|
|
|
->setSubject($model->subject) |
275
|
|
|
->setTextBody($model->body) |
276
|
|
|
->send(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* sendResetPassword |
281
|
|
|
* Sends an email with a link, for resetting the password. |
282
|
|
|
* |
283
|
|
|
* @param MailerInterface $mailer. |
284
|
|
|
* @param Model $model. |
285
|
|
|
* @return bool whether the email was send. |
286
|
|
|
**/ |
287
|
|
|
public function sendResetPassword(MailerInterface $mailer, Model $model) |
288
|
|
|
{ |
289
|
|
|
$this->_User = new UserModels(); |
290
|
|
|
|
291
|
|
|
$this->_User = $this->_User->findOne([ |
292
|
|
|
'status' => $this->_User::STATUS_ACTIVE, |
293
|
|
|
'email' => $model->email, |
294
|
|
|
]); |
295
|
|
|
|
296
|
|
|
if (!$this->_User) { |
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if (!$this->_User->isPasswordResetTokenValid( |
301
|
|
|
$this->_User->password_reset_token, |
302
|
|
|
$this->app->params['user.passwordResetTokenExpire'] |
303
|
|
|
)) { |
304
|
|
|
$this->_User->generatePasswordResetToken(); |
305
|
|
|
if (!$this->_User->save()) { |
306
|
|
|
return false; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $mailer->compose( |
311
|
|
|
['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], |
312
|
|
|
['user' => $this->_User] |
313
|
|
|
) |
314
|
|
|
->setFrom([$this->app->params['adminEmail'] => $this->app->name . ' robot']) |
315
|
|
|
->setTo($model->email) |
316
|
|
|
->setSubject('Password reset for ' . $this->app->name) |
317
|
|
|
->send(); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.