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