-
Notifications
You must be signed in to change notification settings - Fork 138
/
RecoveryController.php
183 lines (157 loc) · 5.53 KB
/
RecoveryController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Da\User\Controller;
use Da\User\Event\FormEvent;
use Da\User\Event\ResetPasswordEvent;
use Da\User\Factory\MailFactory;
use Da\User\Form\RecoveryForm;
use Da\User\Model\Token;
use Da\User\Module;
use Da\User\Query\TokenQuery;
use Da\User\Query\UserQuery;
use Da\User\Service\PasswordRecoveryService;
use Da\User\Service\ResetPasswordService;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class RecoveryController extends Controller
{
use ContainerAwareTrait;
use ModuleAwareTrait;
protected $userQuery;
protected $tokenQuery;
/**
* RecoveryController constructor.
*
* @param string $id
* @param Module $module
* @param UserQuery $userQuery
* @param TokenQuery $tokenQuery
* @param array $config
*/
public function __construct($id, Module $module, UserQuery $userQuery, TokenQuery $tokenQuery, array $config = [])
{
$this->userQuery = $userQuery;
$this->tokenQuery = $tokenQuery;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'actions' => ['request', 'reset'],
'roles' => ['?'],
],
],
],
];
}
/**
* Displays / handles user password recovery request.
*
* @throws NotFoundHttpException
* @throws InvalidConfigException
* @throws InvalidParamException
* @return string
*
*/
public function actionRequest()
{
if (!$this->module->allowPasswordRecovery) {
throw new NotFoundHttpException();
}
/** @var RecoveryForm $form */
$form = $this->make(RecoveryForm::class, [], ['scenario' => RecoveryForm::SCENARIO_REQUEST]);
$event = $this->make(FormEvent::class, [$form]);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
$this->trigger(FormEvent::EVENT_BEFORE_REQUEST, $event);
$mailService = MailFactory::makeRecoveryMailerService($form->email);
if ($this->make(PasswordRecoveryService::class, [$form->email, $mailService])->run()) {
$this->trigger(FormEvent::EVENT_AFTER_REQUEST, $event);
}
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Recovery message sent'),
'module' => $this->module,
]
);
}
return $this->render('request', ['model' => $form]);
}
/**
* Displays / handles user password reset.
*
* @param $id
* @param $code
*
* @throws NotFoundHttpException
* @throws InvalidConfigException
* @throws InvalidParamException
* @return string
*
*/
public function actionReset($id, $code)
{
if (!$this->module->allowPasswordRecovery && !$this->module->allowAdminPasswordRecovery) {
throw new NotFoundHttpException();
}
/** @var Token $token */
$token = $this->tokenQuery->whereUserId($id)->whereCode($code)->whereIsRecoveryType()->one();
/** @var ResetPasswordEvent $event */
$event = $this->make(ResetPasswordEvent::class, [$token]);
$this->trigger(ResetPasswordEvent::EVENT_BEFORE_TOKEN_VALIDATE, $event);
if ($token === null || $token->getIsExpired() || $token->user === null) {
Yii::$app->session->setFlash(
'danger',
Yii::t('usuario', 'Recovery link is invalid or expired. Please try requesting a new one.')
);
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Invalid or expired link'),
'module' => $this->module,
]
);
}
/** @var RecoveryForm $form */
$form = $this->make(RecoveryForm::class, [], ['scenario' => RecoveryForm::SCENARIO_RESET]);
$event = $event->updateForm($form);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->getRequest()->post())) {
if ($this->make(ResetPasswordService::class, [$form->password, $token->user])->run()) {
$this->trigger(ResetPasswordEvent::EVENT_AFTER_RESET, $event);
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Password has been changed'));
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Password has been changed'),
'module' => $this->module,
]
);
}
}
return $this->render('reset', ['model' => $form]);
}
}