1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\user\actions; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\Response; |
7
|
|
|
use yii\widgets\ActiveForm; |
8
|
|
|
use yii2mod\user\traits\EventTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RequestPasswordResetAction |
12
|
|
|
* |
13
|
|
|
* @package yii2mod\user\actions |
14
|
|
|
*/ |
15
|
|
|
class RequestPasswordResetAction extends Action |
16
|
|
|
{ |
17
|
|
|
use EventTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Event is triggered before requesting password reset. |
21
|
|
|
* Triggered with \yii2mod\user\events\FormEvent. |
22
|
|
|
*/ |
23
|
|
|
const EVENT_BEFORE_REQUEST = 'beforeRequest'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Event is triggered after requesting password reset. |
27
|
|
|
* Triggered with \yii2mod\user\events\FormEvent. |
28
|
|
|
*/ |
29
|
|
|
const EVENT_AFTER_REQUEST = 'afterRequest'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string name of the view, which should be rendered |
33
|
|
|
*/ |
34
|
|
|
public $view = '@vendor/yii2mod/yii2-user/views/requestPasswordResetToken'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string password reset request form class |
38
|
|
|
*/ |
39
|
|
|
public $modelClass = 'yii2mod\user\models\PasswordResetRequestForm'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string success message to the user when the mail is sent successfully |
43
|
|
|
*/ |
44
|
|
|
public $successMessage = 'Check your email for further instructions.'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string error message for the user when the email was not sent |
48
|
|
|
*/ |
49
|
|
|
public $errorMessage = 'Sorry, we are unable to reset password for email provided.'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Request password reset for a user. |
53
|
|
|
* |
54
|
|
|
* @return string|\yii\web\Response |
55
|
|
|
*/ |
56
|
|
|
public function run() |
57
|
|
|
{ |
58
|
|
|
$model = Yii::createObject($this->modelClass); |
59
|
|
|
$event = $this->getFormEvent($model); |
60
|
|
|
|
61
|
|
|
$this->trigger(self::EVENT_BEFORE_REQUEST, $event); |
62
|
|
|
|
63
|
|
|
$load = $model->load(Yii::$app->request->post()); |
64
|
|
|
|
65
|
|
View Code Duplication |
if (Yii::$app->request->isAjax) { |
|
|
|
|
66
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
67
|
|
|
|
68
|
|
|
return ActiveForm::validate($model); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
if ($load && $model->validate()) { |
|
|
|
|
72
|
|
|
if ($model->sendEmail()) { |
73
|
|
|
$this->trigger(self::EVENT_AFTER_REQUEST, $event); |
74
|
|
|
Yii::$app->getSession()->setFlash('success', $this->successMessage); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return $this->redirectTo(Yii::$app->getHomeUrl()); |
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
Yii::$app->getSession()->setFlash('error', $this->errorMessage); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->controller->render($this->view, [ |
83
|
|
|
'model' => $model, |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.