|
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\SocialNetworkConnectEvent; |
|
16
|
|
|
use Da\User\Event\UserEvent; |
|
17
|
|
|
use Da\User\Factory\MailFactory; |
|
18
|
|
|
use Da\User\Form\RegistrationForm; |
|
19
|
|
|
use Da\User\Form\ResendForm; |
|
20
|
|
|
use Da\User\Model\SocialNetworkAccount; |
|
21
|
|
|
use Da\User\Model\User; |
|
22
|
|
|
use Da\User\Query\SocialNetworkAccountQuery; |
|
23
|
|
|
use Da\User\Query\UserQuery; |
|
24
|
|
|
use Da\User\Service\AccountConfirmationService; |
|
25
|
|
|
use Da\User\Service\ResendConfirmationService; |
|
26
|
|
|
use Da\User\Service\UserConfirmationService; |
|
27
|
|
|
use Da\User\Service\UserCreateService; |
|
28
|
|
|
use Da\User\Service\UserRegisterService; |
|
29
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
|
30
|
|
|
use Da\User\Validator\AjaxRequestModelValidator; |
|
31
|
|
|
use Yii; |
|
32
|
|
|
use yii\base\Module; |
|
33
|
|
|
use yii\filters\AccessControl; |
|
34
|
|
|
use yii\web\Controller; |
|
35
|
|
|
use yii\web\NotFoundHttpException; |
|
36
|
|
|
|
|
37
|
|
|
class RegistrationController extends Controller |
|
38
|
|
|
{ |
|
39
|
|
|
use ContainerAwareTrait; |
|
40
|
|
|
|
|
41
|
|
|
protected $userQuery; |
|
42
|
|
|
protected $socialNetworkAccountQuery; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* RegistrationController constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $id |
|
48
|
|
|
* @param Module $module |
|
49
|
|
|
* @param UserQuery $userQuery |
|
50
|
|
|
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery |
|
51
|
|
|
* @param array $config |
|
52
|
|
|
*/ |
|
53
|
5 |
|
public function __construct( |
|
54
|
|
|
$id, |
|
55
|
|
|
Module $module, |
|
56
|
|
|
UserQuery $userQuery, |
|
57
|
|
|
SocialNetworkAccountQuery $socialNetworkAccountQuery, |
|
58
|
|
|
array $config = [] |
|
59
|
|
|
) { |
|
60
|
5 |
|
$this->userQuery = $userQuery; |
|
61
|
5 |
|
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery; |
|
62
|
5 |
|
parent::__construct($id, $module, $config); |
|
63
|
5 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
5 |
|
public function behaviors() |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
71
|
5 |
|
'access' => [ |
|
72
|
5 |
|
'class' => AccessControl::className(), |
|
73
|
|
|
'rules' => [ |
|
74
|
|
|
[ |
|
75
|
|
|
'allow' => true, |
|
76
|
|
|
'actions' => ['register', 'connect'], |
|
77
|
|
|
'roles' => ['?'], |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
|
|
'allow' => true, |
|
81
|
|
|
'actions' => ['confirm', 'resend'], |
|
82
|
|
|
'roles' => ['?', '@'], |
|
83
|
|
|
], |
|
84
|
|
|
], |
|
85
|
|
|
], |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
public function actionRegister() |
|
90
|
|
|
{ |
|
91
|
3 |
|
if (!$this->module->enableRegistration) { |
|
92
|
|
|
throw new NotFoundHttpException(); |
|
93
|
|
|
} |
|
94
|
|
|
/** @var RegistrationForm $form */ |
|
95
|
3 |
|
$form = $this->make(RegistrationForm::class); |
|
96
|
|
|
/** @var FormEvent $event */ |
|
97
|
3 |
|
$event = $this->make(FormEvent::class, [$form]); |
|
98
|
|
|
|
|
99
|
3 |
|
$this->make(AjaxRequestModelValidator::class, [$form])->validate(); |
|
100
|
|
|
|
|
101
|
3 |
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
|
102
|
3 |
|
$this->trigger(UserEvent::EVENT_BEFORE_REGISTER, $event); |
|
103
|
|
|
/** @var User $user */ |
|
104
|
3 |
|
$user = $this->make(User::class, [], $form->attributes); |
|
105
|
3 |
|
$user->setScenario('register'); |
|
106
|
3 |
|
$mailService = MailFactory::makeWelcomeMailerService($user); |
|
107
|
|
|
|
|
108
|
3 |
|
if ($this->make(UserRegisterService::class, [$user, $mailService])->run()) { |
|
109
|
3 |
|
Yii::$app->session->setFlash( |
|
110
|
3 |
|
'info', |
|
111
|
3 |
|
Yii::t( |
|
112
|
3 |
|
'usuario', |
|
113
|
3 |
|
'Your account has been created and a message with further instructions has been sent to your email' |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
3 |
|
$this->trigger(UserEvent::EVENT_AFTER_REGISTER, $event); |
|
117
|
|
|
|
|
118
|
3 |
|
return $this->render( |
|
119
|
3 |
|
'/shared/message', |
|
120
|
|
|
[ |
|
121
|
3 |
|
'title' => Yii::t('usuario', 'Your account has been created'), |
|
122
|
3 |
|
'module' => $this->module, |
|
123
|
|
|
] |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
3 |
|
return $this->render( |
|
129
|
3 |
|
'register', |
|
130
|
|
|
[ |
|
131
|
3 |
|
'model' => $form, |
|
132
|
3 |
|
'module' => $this->module, |
|
133
|
|
|
] |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function actionConnect($code) |
|
138
|
|
|
{ |
|
139
|
|
|
/** @var SocialNetworkAccount $account */ |
|
140
|
|
|
$account = $this->socialNetworkAccountQuery->whereCode($code)->one(); |
|
141
|
|
|
if ($account === null || $account->getIsConnected()) { |
|
142
|
|
|
throw new NotFoundHttpException(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** @var User $user */ |
|
146
|
|
|
$user = $this->make( |
|
147
|
|
|
User::class, |
|
148
|
|
|
[], |
|
149
|
|
|
['scenario' => 'connect', 'username' => $account->username, 'email' => $account->email] |
|
150
|
|
|
); |
|
151
|
|
|
$event = $this->make(SocialNetworkConnectEvent::class, [$user, $account]); |
|
152
|
|
|
|
|
153
|
|
|
$this->make(AjaxRequestModelValidator::class, [$user])->validate(); |
|
154
|
|
|
|
|
155
|
|
|
if ($user->load(Yii::$app->request->post())) { |
|
156
|
|
|
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT, $event); |
|
157
|
|
|
|
|
158
|
|
|
$mailService = MailFactory::makeWelcomeMailerService($user); |
|
159
|
|
|
if ($this->make(UserCreateService::class, [$user, $mailService])->run()) { |
|
160
|
|
|
$account->connect($user); |
|
161
|
|
|
$this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_CONNECT, $event); |
|
162
|
|
|
|
|
163
|
|
|
Yii::$app->user->login($user, $this->module->rememberLoginLifespan); |
|
164
|
|
|
|
|
165
|
|
|
return $this->goBack(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->render( |
|
170
|
|
|
'connect', |
|
171
|
|
|
[ |
|
172
|
|
|
'model' => $user, |
|
173
|
|
|
'account' => $account, |
|
174
|
|
|
] |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
public function actionConfirm($id, $code) |
|
179
|
|
|
{ |
|
180
|
|
|
/** @var User $user */ |
|
181
|
1 |
|
$user = $this->userQuery->whereId($id)->one(); |
|
182
|
|
|
|
|
183
|
1 |
|
if ($user === null || $this->module->enableEmailConfirmation === false) { |
|
184
|
|
|
throw new NotFoundHttpException(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** @var UserEvent $event */ |
|
188
|
1 |
|
$event = $this->make(UserEvent::class, [$user]); |
|
189
|
1 |
|
$userConfirmationService = $this->make(UserConfirmationService::class, [$user]); |
|
190
|
|
|
|
|
191
|
1 |
|
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event); |
|
192
|
|
|
|
|
193
|
1 |
|
if ($this->make(AccountConfirmationService::class, [$code, $user, $userConfirmationService])->run()) { |
|
194
|
1 |
|
Yii::$app->user->login($user, $this->module->rememberLoginLifespan); |
|
195
|
1 |
|
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Thank you, registration is now complete.')); |
|
196
|
|
|
|
|
197
|
1 |
|
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event); |
|
198
|
|
|
} else { |
|
199
|
1 |
|
Yii::$app->session->setFlash( |
|
200
|
1 |
|
'danger', |
|
201
|
1 |
|
Yii::t('usuario', 'The confirmation link is invalid or expired. Please try requesting a new one.') |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
1 |
|
return $this->render( |
|
206
|
1 |
|
'/shared/message', |
|
207
|
|
|
[ |
|
208
|
1 |
|
'title' => Yii::t('usuario', 'Account confirmation'), |
|
209
|
1 |
|
'module' => $this->module, |
|
210
|
|
|
] |
|
211
|
|
|
); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
1 |
|
public function actionResend() |
|
215
|
|
|
{ |
|
216
|
1 |
|
if ($this->module->enableEmailConfirmation === false) { |
|
217
|
|
|
throw new NotFoundHttpException(); |
|
218
|
|
|
} |
|
219
|
|
|
/** @var ResendForm $form */ |
|
220
|
1 |
|
$form = $this->make(ResendForm::class); |
|
221
|
1 |
|
$event = $this->make(FormEvent::class, [$form]); |
|
222
|
|
|
|
|
223
|
1 |
|
$this->make(AjaxRequestModelValidator::class, [$form])->validate(); |
|
224
|
|
|
|
|
225
|
1 |
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
|
226
|
|
|
/** @var User $user */ |
|
227
|
1 |
|
$user = $this->userQuery->whereEmail($form->email)->one(); |
|
228
|
1 |
|
$success = true; |
|
229
|
1 |
|
if ($user !== null) { |
|
230
|
1 |
|
$this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event); |
|
231
|
1 |
|
$mailService = MailFactory::makeConfirmationMailerService($user); |
|
232
|
1 |
|
if ($success = $this->make(ResendConfirmationService::class, [$user, $mailService])->run()) { |
|
233
|
1 |
|
$this->trigger(FormEvent::EVENT_AFTER_RESEND, $event); |
|
234
|
1 |
|
Yii::$app->session->setFlash( |
|
235
|
1 |
|
'info', |
|
236
|
1 |
|
Yii::t( |
|
237
|
1 |
|
'usuario', |
|
238
|
|
|
'A message has been sent to your email address. ' . |
|
239
|
1 |
|
'It contains a confirmation link that you must click to complete registration.' |
|
240
|
|
|
) |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
1 |
|
if ($user === null || $success === false) { |
|
245
|
1 |
|
Yii::$app->session->setFlash( |
|
246
|
1 |
|
'danger', |
|
247
|
1 |
|
Yii::t( |
|
248
|
1 |
|
'usuario', |
|
249
|
|
|
'We couldn\'t re-send the mail to confirm your address. ' . |
|
250
|
1 |
|
'Please, verify is the correct email or if it has been confirmed already.' |
|
251
|
|
|
) |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
1 |
|
return $this->render( |
|
256
|
1 |
|
'/shared/message', |
|
257
|
|
|
[ |
|
258
|
1 |
|
'title' => $success |
|
259
|
1 |
|
? Yii::t('usuario', 'A new confirmation link has been sent') |
|
260
|
1 |
|
: Yii::t('usuario', 'Unable to send confirmation link'), |
|
261
|
1 |
|
'module' => $this->module, |
|
262
|
|
|
] |
|
263
|
|
|
); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
1 |
|
return $this->render( |
|
267
|
1 |
|
'resend', |
|
268
|
|
|
[ |
|
269
|
1 |
|
'model' => $form, |
|
270
|
|
|
] |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|