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