-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathRegistrationController.php
301 lines (264 loc) · 10.2 KB
/
RegistrationController.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?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\SocialNetworkConnectEvent;
use Da\User\Event\UserEvent;
use Da\User\Factory\MailFactory;
use Da\User\Form\RegistrationForm;
use Da\User\Form\ResendForm;
use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Query\UserQuery;
use Da\User\Service\AccountConfirmationService;
use Da\User\Service\ResendConfirmationService;
use Da\User\Service\UserConfirmationService;
use Da\User\Service\UserCreateService;
use Da\User\Service\UserRegisterService;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator;
use Yii;
use yii\base\Module;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class RegistrationController extends Controller
{
use ContainerAwareTrait;
use ModuleAwareTrait;
protected $userQuery;
protected $socialNetworkAccountQuery;
/**
* RegistrationController constructor.
*
* @param string $id
* @param Module $module
* @param UserQuery $userQuery
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
* @param array $config
*/
public function __construct(
$id,
Module $module,
UserQuery $userQuery,
SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config = []
) {
$this->userQuery = $userQuery;
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'actions' => ['register', 'connect'],
'roles' => ['?'],
],
[
'allow' => true,
'actions' => ['confirm', 'resend'],
'roles' => ['?', '@'],
],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actionRegister()
{
if (!$this->module->enableRegistration) {
throw new NotFoundHttpException();
}
/** @var RegistrationForm $form */
$form = $this->make(RegistrationForm::class);
/** @var FormEvent $event */
$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_REGISTER, $event);
/** @var User $user */
// Create a temporay $user so we can get the attributes, then get
// the intersection between the $form fields and the $user fields.
$user = $this->make(User::class, []);
$fields = array_intersect_key($form->attributes, $user->attributes);
// Becomes password_hash
$fields['password'] = $form['password'];
$user = $this->make(User::class, [], $fields);
$user->setScenario('register');
$mailService = MailFactory::makeWelcomeMailerService($user);
if ($this->make(UserRegisterService::class, [$user, $mailService])->run()) {
if ($this->module->enableEmailConfirmation) {
Yii::$app->session->setFlash(
'info',
Yii::t(
'usuario',
'Your account has been created and a message with further instructions has been sent to your email'
)
);
} else {
Yii::$app->session->setFlash('info', Yii::t('usuario', 'Your account has been created'));
}
$this->trigger(FormEvent::EVENT_AFTER_REGISTER, $event);
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Your account has been created'),
'module' => $this->module,
]
);
}
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'User could not be registered.'));
}
return $this->render('register', ['model' => $form, 'module' => $this->module]);
}
/**
* {@inheritdoc}
*/
public function actionConnect($code)
{
if (!$this->module->enableSocialNetworkRegistration) {
throw new NotFoundHttpException();
}
/** @var ?SocialNetworkAccount $account */
$account = $this->socialNetworkAccountQuery->whereCode($code)->one();
if ($account === null || $account->getIsConnected()) {
throw new NotFoundHttpException();
}
/** @var User $user */
$user = $this->make(
User::class,
[],
['scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]
);
$event = $this->make(SocialNetworkConnectEvent::class, [$user, $account]);
$this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($user->load(Yii::$app->request->post()) && $user->validate()) {
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT, $event);
if ($this->module->sendWelcomeMailAfterSocialNetworkRegistration) {
$mailService = MailFactory::makeWelcomeMailerService($user);
} else {
$mailService = null;
}
if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
$account->connect($user);
$this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_CONNECT, $event);
Yii::$app->user->login($user, $this->module->rememberLoginLifespan);
return $this->goBack();
}
}
return $this->render(
'connect',
[
'model' => $user,
'account' => $account,
]
);
}
/**
* {@inheritdoc}
*/
public function actionConfirm($id, $code)
{
/** @var ?User $user */
$user = $this->userQuery->whereId($id)->one();
if ($user === null || $this->module->enableEmailConfirmation === false) {
throw new NotFoundHttpException();
}
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
$userConfirmationService = $this->make(UserConfirmationService::class, [$user]);
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
if ($this->make(AccountConfirmationService::class, [$code, $user, $userConfirmationService])->run()) {
Yii::$app->user->login($user, $this->module->rememberLoginLifespan);
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Thank you, registration is now complete.'));
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
} else {
Yii::$app->session->setFlash(
'danger',
Yii::t('usuario', 'The confirmation link is invalid or expired. Please try requesting a new one.')
);
}
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Account confirmation'),
'module' => $this->module,
]
);
}
/**
* {@inheritdoc}
*/
public function actionResend()
{
if ($this->module->enableEmailConfirmation === false) {
throw new NotFoundHttpException();
}
/** @var ResendForm $form */
$form = $this->make(ResendForm::class);
$event = $this->make(FormEvent::class, [$form]);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
/** @var ?User $user */
$user = $this->userQuery->whereEmail($form->email)->one();
$success = true;
if ($user !== null) {
$this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event);
$mailService = MailFactory::makeConfirmationMailerService($user);
if ($success = $this->make(ResendConfirmationService::class, [$user, $mailService])->run()) {
$this->trigger(FormEvent::EVENT_AFTER_RESEND, $event);
Yii::$app->session->setFlash(
'info',
Yii::t(
'usuario',
'A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'
)
);
}
}
if ($user === null || $success === false) {
Yii::$app->session->setFlash(
'danger',
Yii::t(
'usuario',
'We couldn\'t re-send the mail to confirm your address. Please, verify is the correct email or if it has been confirmed already.'
)
);
}
return $this->render(
'/shared/message',
[
'title' => $success
? Yii::t('usuario', 'A new confirmation link has been sent')
: Yii::t('usuario', 'Unable to send confirmation link'),
'module' => $this->module,
]
);
}
return $this->render(
'resend',
[
'model' => $form,
]
);
}
}