1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Form\User\RegistrationForm; |
6
|
|
|
use App\Form\User\ResetPasswordForm; |
7
|
|
|
use Bone\Mvc\Registry; |
8
|
|
|
use Bone\Service\MailService; |
9
|
|
|
use Del\Common\ContainerService; |
10
|
|
|
use Del\Exception\EmailLinkException; |
11
|
|
|
use Del\Exception\UserException; |
12
|
|
|
use Del\Service\UserService; |
13
|
|
|
use Del\Value\User\State; |
14
|
|
|
use Exception; |
15
|
|
|
use Zend\Validator\EmailAddress; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class UserController |
19
|
|
|
* @package App\Controller |
20
|
|
|
* @todo get swagger updated with oauth 2 stuff to get header in |
21
|
|
|
*/ |
22
|
|
|
class UserController extends BaseController |
23
|
|
|
{ |
24
|
|
|
/** @var UserService */ |
25
|
|
|
private $userService; |
26
|
|
|
|
27
|
12 |
|
public function init() |
28
|
|
|
{ |
29
|
12 |
|
parent::init(); |
30
|
12 |
|
$c = ContainerService::getInstance()->getContainer(); |
31
|
12 |
|
$this->userService = $c['service.user']; |
32
|
12 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Fetch user details by ID. |
36
|
|
|
* |
37
|
|
|
* @SWG\Get( |
38
|
|
|
* path="/user/{id}", |
39
|
|
|
* tags={"user"}, |
40
|
|
|
* @SWG\Parameter( |
41
|
|
|
* name="id", |
42
|
|
|
* in="path", |
43
|
|
|
* type="integer", |
44
|
|
|
* description="the type of response", |
45
|
|
|
* required=false, |
46
|
|
|
* default=1 |
47
|
|
|
* ), |
48
|
|
|
* @SWG\Response(response="200", description="Sends user details") |
49
|
|
|
* ) |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
1 |
|
public function indexAction() |
53
|
|
|
{ |
54
|
1 |
|
if (!$this->httpMethodCheck('GET')) { return; } |
55
|
|
|
|
56
|
1 |
|
$id = $this->getParam('id'); |
57
|
|
|
|
58
|
|
|
/** @var UserService $userSvc */ |
59
|
1 |
|
$userSvc = ContainerService::getInstance()->getContainer()['service.user']; |
60
|
|
|
|
61
|
1 |
|
$user = $userSvc->findUserById($id); |
|
|
|
|
62
|
1 |
|
if (!$user) { |
63
|
|
|
$this->sendJsonResponse(['error' => 'User not found'], 404); |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$this->sendJsonObjectResponse($user); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Activate from the email link token. |
74
|
|
|
* |
75
|
|
|
* @SWG\Get( |
76
|
|
|
* path="/{locale}/user/activate/{email}/{token}", |
77
|
|
|
* tags={"user_registration"}, |
78
|
|
|
* @SWG\Response(response="200", description="Registers a new unactivated user"), |
79
|
|
|
* @SWG\Parameter( |
80
|
|
|
* name="locale", |
81
|
|
|
* in="path", |
82
|
|
|
* type="string", |
83
|
|
|
* description="the locale to use", |
84
|
|
|
* required=true, |
85
|
|
|
* default="en_GB" |
86
|
|
|
* ), |
87
|
|
|
* @SWG\Parameter( |
88
|
|
|
* name="email", |
89
|
|
|
* in="path", |
90
|
|
|
* type="string", |
91
|
|
|
* description="the users email", |
92
|
|
|
* required=true, |
93
|
|
|
* default="[email protected]" |
94
|
|
|
* ), |
95
|
|
|
* @SWG\Parameter( |
96
|
|
|
* name="token", |
97
|
|
|
* in="path", |
98
|
|
|
* type="string", |
99
|
|
|
* description="the email link token", |
100
|
|
|
* required=true, |
101
|
|
|
* default="r4nd0mT0k3n" |
102
|
|
|
* ) |
103
|
|
|
* ) |
104
|
|
|
* @throws Exception |
105
|
|
|
*/ |
106
|
6 |
|
public function activateAction() |
107
|
|
|
{ |
108
|
6 |
|
if (!$this->httpMethodCheck('GET')) { return; } |
109
|
|
|
|
110
|
6 |
|
$email = $this->getParam('email'); |
111
|
6 |
|
$token = $this->getParam('token'); |
112
|
|
|
|
113
|
6 |
|
$userService = $this->userService; |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
|
117
|
6 |
|
$link = $userService->findEmailLink($email, $token); |
118
|
|
|
|
119
|
2 |
|
$user = $link->getUser(); |
120
|
2 |
|
$user->setState(new State(State::STATE_ACTIVATED)); |
121
|
2 |
|
$userService->saveUser($user); |
122
|
2 |
|
$userService->deleteEmailLink($link); |
123
|
2 |
|
$data = ['success' => true]; |
124
|
2 |
|
$code = 200; |
125
|
|
|
|
126
|
4 |
|
} catch (EmailLinkException $e) { |
127
|
4 |
|
switch ($e->getMessage()) { |
128
|
4 |
|
case EmailLinkException::LINK_EXPIRED: |
129
|
3 |
|
case EmailLinkException::LINK_NO_MATCH: |
130
|
|
|
$data = [ |
131
|
1 |
|
'success' => false, |
132
|
1 |
|
'error' => $e->getMessage(), |
133
|
|
|
]; |
134
|
1 |
|
$code = 403; |
135
|
1 |
|
break; |
136
|
3 |
|
case EmailLinkException::LINK_NOT_FOUND: |
137
|
|
|
$data = [ |
138
|
3 |
|
'success' => false, |
139
|
3 |
|
'error' => $e->getMessage(), |
140
|
|
|
]; |
141
|
3 |
|
$code = 404; |
142
|
3 |
|
break; |
143
|
|
|
default: |
144
|
|
|
$data = [ |
145
|
|
|
'success' => false, |
146
|
|
|
'error' => $e->getMessage(), |
147
|
|
|
]; |
148
|
|
|
$code = 500; |
149
|
|
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
6 |
|
$this->sendJsonResponse($data, $code); |
154
|
6 |
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Refresh the activation email link token. |
159
|
|
|
* |
160
|
|
|
* @SWG\Get( |
161
|
|
|
* path="/{locale}/user/activate/resend/{email}", |
162
|
|
|
* tags={"user_registration"}, |
163
|
|
|
* @SWG\Parameter( |
164
|
|
|
* name="locale", |
165
|
|
|
* in="path", |
166
|
|
|
* type="string", |
167
|
|
|
* description="the locale to use", |
168
|
|
|
* required=true, |
169
|
|
|
* default="en_GB" |
170
|
|
|
* ), |
171
|
|
|
* @SWG\Parameter( |
172
|
|
|
* name="email", |
173
|
|
|
* in="path", |
174
|
|
|
* type="string", |
175
|
|
|
* description="the email of the user registering", |
176
|
|
|
* required=true, |
177
|
|
|
* default="[email protected]" |
178
|
|
|
* ), |
179
|
|
|
* @SWG\Response(response="200", description="Sends email link details") |
180
|
|
|
* ) |
181
|
|
|
* @throws Exception |
182
|
|
|
*/ |
183
|
2 |
|
public function resendActivationAction() |
184
|
|
|
{ |
185
|
2 |
|
if (!$this->httpMethodCheck('GET')) { return; } |
186
|
|
|
|
187
|
2 |
|
$email = $this->getParam('email'); |
188
|
|
|
|
189
|
2 |
|
$user = $this->userService->findUserByEmail($email); |
190
|
2 |
|
if (!$user) { |
191
|
|
|
$this->sendJsonResponse(['error' => UserException::USER_NOT_FOUND], 404); |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
if ($user->getState()->getValue() == State::STATE_ACTIVATED) { |
196
|
1 |
|
$this->sendJsonResponse(['error' => UserException::USER_ACTIVATED], 400); |
197
|
1 |
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
$link = $this->userService->generateEmailLink($user); |
201
|
|
|
|
202
|
1 |
|
$mail = $this->getMailService(); |
203
|
1 |
|
$env = $this->getServerEnvironment(); |
204
|
1 |
|
$email = $user->getEmail(); |
205
|
1 |
|
$token = $link->getToken(); |
206
|
|
|
|
207
|
1 |
|
$message = $this->getViewEngine()->render('emails/user_registration/user_registration', [ |
208
|
1 |
|
'siteUrl' => $env->getSiteURL(), |
209
|
1 |
|
'activationLink' => '/' . $this->getParam('locale') . '/user/activate/' . $email . '/' . $token, |
210
|
|
|
]); |
211
|
|
|
|
212
|
1 |
|
$mail->setFrom('noreply@' . $env->getServerName()) |
213
|
1 |
|
->setTo($user->getEmail()) |
214
|
1 |
|
->setSubject($this->getTranslator() |
215
|
1 |
|
->translate('email.user.register.thankswith') . ' ' . Registry::ahoy()->get('site')['name']) |
216
|
1 |
|
->setMessage($message) |
217
|
1 |
|
->send(); |
218
|
|
|
|
219
|
|
|
|
220
|
1 |
|
$this->sendJsonObjectResponse($link); |
221
|
1 |
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get a lost password email link token. |
225
|
|
|
* |
226
|
|
|
* @SWG\Get( |
227
|
|
|
* path="/{locale}/user/lost-password/{email}", |
228
|
|
|
* tags={"user_registration"}, |
229
|
|
|
* @SWG\Parameter( |
230
|
|
|
* name="locale", |
231
|
|
|
* in="path", |
232
|
|
|
* type="string", |
233
|
|
|
* description="the locale to use", |
234
|
|
|
* required=true, |
235
|
|
|
* default="en_GB" |
236
|
|
|
* ), |
237
|
|
|
* @SWG\Parameter( |
238
|
|
|
* name="email", |
239
|
|
|
* in="path", |
240
|
|
|
* type="string", |
241
|
|
|
* description="the email of the user", |
242
|
|
|
* required=true, |
243
|
|
|
* default="[email protected]" |
244
|
|
|
* ), |
245
|
|
|
* @SWG\Response(response="200", description="Sends email link details") |
246
|
|
|
* ) |
247
|
|
|
* @throws Exception |
248
|
|
|
*/ |
249
|
3 |
|
public function lostPasswordAction() |
250
|
|
|
{ |
251
|
3 |
|
if (!$this->httpMethodCheck('GET')) { return; } |
252
|
|
|
|
253
|
3 |
|
$email = $this->getParam('email'); |
254
|
|
|
|
255
|
3 |
|
$user = $this->userService->findUserByEmail($email); |
256
|
3 |
|
if (!$user) { |
257
|
1 |
|
$this->sendJsonResponse(['error' => UserException::USER_NOT_FOUND], 404); |
258
|
1 |
|
return; |
259
|
|
|
} |
260
|
|
|
|
261
|
2 |
|
if ($user->getState()->getValue() == State::STATE_UNACTIVATED) { |
262
|
1 |
|
$this->sendJsonResponse(['error' => UserException::USER_UNACTIVATED], 400); |
263
|
1 |
|
return; |
264
|
|
|
} |
265
|
|
|
|
266
|
1 |
|
$link = $this->userService->generateEmailLink($user); |
267
|
1 |
|
$this->sendJsonObjectResponse($link); |
268
|
1 |
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Register as a new user. Returns an email link token. |
272
|
|
|
* |
273
|
|
|
* @SWG\Post( |
274
|
|
|
* path="/{locale}/user/register", |
275
|
|
|
* tags={"user_registration"}, |
276
|
|
|
* @SWG\Response(response="200", description="Registers a new unactivated user"), |
277
|
|
|
* @SWG\Parameter( |
278
|
|
|
* name="locale", |
279
|
|
|
* in="path", |
280
|
|
|
* type="string", |
281
|
|
|
* description="the locale to use", |
282
|
|
|
* required=true, |
283
|
|
|
* default="en_GB" |
284
|
|
|
* ), |
285
|
|
|
* @SWG\Parameter( |
286
|
|
|
* name="email", |
287
|
|
|
* in="formData", |
288
|
|
|
* type="string", |
289
|
|
|
* description="the users email", |
290
|
|
|
* required=true, |
291
|
|
|
* default="[email protected]" |
292
|
|
|
* ), |
293
|
|
|
* @SWG\Parameter( |
294
|
|
|
* name="password", |
295
|
|
|
* in="formData", |
296
|
|
|
* type="string", |
297
|
|
|
* description="a password for the user", |
298
|
|
|
* required=true, |
299
|
|
|
* default="password" |
300
|
|
|
* ), |
301
|
|
|
* @SWG\Parameter( |
302
|
|
|
* name="confirm", |
303
|
|
|
* in="formData", |
304
|
|
|
* type="string", |
305
|
|
|
* description="password confirmation", |
306
|
|
|
* required=true, |
307
|
|
|
* default="password" |
308
|
|
|
* ) |
309
|
|
|
* ) |
310
|
|
|
* @throws Exception |
311
|
|
|
*/ |
312
|
7 |
|
public function registerAction() |
313
|
|
|
{ |
314
|
7 |
|
if (!$this->httpMethodCheck('POST')) { return; } |
315
|
|
|
|
316
|
7 |
|
$form = new RegistrationForm('register'); |
317
|
|
|
|
318
|
7 |
|
if ($this->getRequest()->getMethod() == 'POST') { |
319
|
|
|
|
320
|
7 |
|
$formData = $this->getRequest()->getParsedBody(); |
321
|
7 |
|
$form->populate($formData); |
|
|
|
|
322
|
|
|
|
323
|
7 |
|
if ($form->isValid()) { |
324
|
|
|
try { |
325
|
7 |
|
$data = $form->getValues(); |
326
|
7 |
|
$user = $this->userService->registerUser($data); |
327
|
7 |
|
$link = $this->userService->generateEmailLink($user); |
328
|
7 |
|
$mail = $this->getMailService(); |
329
|
7 |
|
$env = $this->getServerEnvironment(); |
330
|
7 |
|
$email = $user->getEmail(); |
331
|
7 |
|
$token = $link->getToken(); |
332
|
|
|
|
333
|
7 |
|
$message = $this->getViewEngine()->render('emails/user_registration/user_registration', [ |
334
|
7 |
|
'siteUrl' => $env->getSiteURL(), |
335
|
7 |
|
'activationLink' => '/' . $this->getParam('locale') . '/user/activate/' . $email . '/' . $token, |
336
|
|
|
]); |
337
|
|
|
|
338
|
7 |
|
$mail->setFrom('noreply@' . $env->getServerName()) |
339
|
7 |
|
->setTo($user->getEmail()) |
340
|
7 |
|
->setSubject($this->getTranslator() |
341
|
7 |
|
->translate('email.user.register.thankswith') . ' ' . Registry::ahoy()->get('site')['name']) |
342
|
7 |
|
->setMessage($message) |
343
|
7 |
|
->send(); |
344
|
7 |
|
$this->sendJsonObjectResponse($link); |
345
|
|
|
|
346
|
|
|
} catch (UserException $e) { |
347
|
|
|
|
348
|
|
|
switch ($e->getMessage()) { |
349
|
|
|
case UserException::USER_EXISTS: |
350
|
|
|
case UserException::WRONG_PASSWORD: |
351
|
|
|
throw new Exception($e->getMessage(), 400); |
352
|
|
|
break; |
353
|
|
|
} |
354
|
|
|
throw $e; |
355
|
|
|
} |
356
|
|
|
} else { |
357
|
|
|
throw new Exception('Invalid request data;', 400); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
} |
361
|
7 |
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Resets the users password. Requires an email link token. |
365
|
|
|
* |
366
|
|
|
* @SWG\Post( |
367
|
|
|
* path="/{locale}/user/reset-password/{email}/{token}", |
368
|
|
|
* tags={"user_registration"}, |
369
|
|
|
* @SWG\Response(response="200", description="Resets a users email"), |
370
|
|
|
* @SWG\Parameter( |
371
|
|
|
* name="locale", |
372
|
|
|
* in="path", |
373
|
|
|
* type="string", |
374
|
|
|
* description="the locale to use", |
375
|
|
|
* required=true, |
376
|
|
|
* default="en_GB" |
377
|
|
|
* ), |
378
|
|
|
* @SWG\Parameter( |
379
|
|
|
* name="email", |
380
|
|
|
* in="path", |
381
|
|
|
* type="string", |
382
|
|
|
* description="the email of the user", |
383
|
|
|
* required=true, |
384
|
|
|
* default="[email protected]" |
385
|
|
|
* ), |
386
|
|
|
* @SWG\Parameter( |
387
|
|
|
* name="token", |
388
|
|
|
* in="path", |
389
|
|
|
* type="string", |
390
|
|
|
* description="the email link token", |
391
|
|
|
* required=true, |
392
|
|
|
* default="r4nd0mT0k3n" |
393
|
|
|
* ), |
394
|
|
|
* @SWG\Parameter( |
395
|
|
|
* name="password", |
396
|
|
|
* in="formData", |
397
|
|
|
* type="string", |
398
|
|
|
* description="a password for the user", |
399
|
|
|
* required=true, |
400
|
|
|
* default="password" |
401
|
|
|
* ), |
402
|
|
|
* @SWG\Parameter( |
403
|
|
|
* name="confirm", |
404
|
|
|
* in="formData", |
405
|
|
|
* type="string", |
406
|
|
|
* description="password confirmation", |
407
|
|
|
* required=true, |
408
|
|
|
* default="password" |
409
|
|
|
* ) |
410
|
|
|
* ) |
411
|
|
|
* @throws Exception |
412
|
|
|
*/ |
413
|
|
|
public function resetPassAction() |
414
|
|
|
{ |
415
|
|
|
if (!$this->httpMethodCheck('POST')) { return; } |
416
|
|
|
|
417
|
|
|
$email = $this->getParam('email'); |
418
|
|
|
$token = $this->getParam('token'); |
419
|
|
|
|
420
|
|
|
$user = $this->userService->findUserByEmail($email); |
421
|
|
|
if (!$user) { |
422
|
|
|
$this->sendJsonResponse(['error' => UserException::USER_NOT_FOUND], 404); |
423
|
|
|
return; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
try { |
427
|
|
|
$link = $this->userService->findEmailLink($email, $token); |
428
|
|
|
} catch (EmailLinkException $e) { |
429
|
|
|
$code = $e->getMessage() == EmailLinkException::LINK_EXPIRED ? 400 : 404; |
430
|
|
|
$this->sendJsonResponse(['error' => $e->getMessage(), $code]); |
431
|
|
|
return; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
$form = new ResetPasswordForm('reset-pass'); |
435
|
|
|
|
436
|
|
|
$data = $this->getRequest()->getParsedBody(); |
437
|
|
|
|
438
|
|
|
$form->populate($data); |
|
|
|
|
439
|
|
|
|
440
|
|
|
if ($form->isValid()) { |
441
|
|
|
|
442
|
|
|
if ($data['password'] == $data['confirm']) { |
443
|
|
|
$this->userService->changePassword($user, $data['password']); |
444
|
|
|
$this->userService->deleteEmailLink($link); |
445
|
|
|
$this->sendJsonResponse(['success' => 'Password successfully changed']); |
446
|
|
|
return; |
447
|
|
|
} else { |
448
|
|
|
$this->sendJsonResponse(['error' => 'Passwords did not match, please try again.'], 400); |
449
|
|
|
} |
450
|
|
|
} else { |
451
|
|
|
$errors = []; |
452
|
|
|
$fields = $form->getFields(); |
453
|
|
|
foreach ($fields as $field) { |
454
|
|
|
$validators = $field->getValidators(); |
455
|
|
|
foreach ($validators as $validator) { |
456
|
|
|
$errors[$field->getName()] = $validator->getMessages(); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
$this->sendJsonResponse(['error' => $errors], 400); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @SWG\Post( |
466
|
|
|
* path="/{locale}/me/change-password/", |
467
|
|
|
* tags={"user_profile"}, |
468
|
|
|
* @SWG\Response(response="200", description="Changes a users password."), |
469
|
|
|
* @SWG\Parameter( |
470
|
|
|
* name="locale", |
471
|
|
|
* in="path", |
472
|
|
|
* type="string", |
473
|
|
|
* description="the locale to use", |
474
|
|
|
* required=true, |
475
|
|
|
* default="en_GB" |
476
|
|
|
* ), |
477
|
|
|
* @SWG\Parameter( |
478
|
|
|
* name="email", |
479
|
|
|
* in="path", |
480
|
|
|
* type="string", |
481
|
|
|
* description="the email of the user", |
482
|
|
|
* required=true, |
483
|
|
|
* default="[email protected]" |
484
|
|
|
* ), |
485
|
|
|
* @SWG\Parameter( |
486
|
|
|
* name="token", |
487
|
|
|
* in="path", |
488
|
|
|
* type="formData", |
489
|
|
|
* description="the email link token", |
490
|
|
|
* required=true, |
491
|
|
|
* default="r4nd0mT0k3n" |
492
|
|
|
* ), |
493
|
|
|
* @SWG\Parameter( |
494
|
|
|
* name="password", |
495
|
|
|
* in="formData", |
496
|
|
|
* type="string", |
497
|
|
|
* description="a password for the user", |
498
|
|
|
* required=true, |
499
|
|
|
* default="password" |
500
|
|
|
* ), |
501
|
|
|
* @SWG\Parameter( |
502
|
|
|
* name="confirm", |
503
|
|
|
* in="formData", |
504
|
|
|
* type="string", |
505
|
|
|
* description="password confirmation", |
506
|
|
|
* required=true, |
507
|
|
|
* default="password" |
508
|
|
|
* ) |
509
|
|
|
* ) |
510
|
|
|
* @throws Exception |
511
|
|
|
*/ |
512
|
|
|
public function changePassAction() |
513
|
|
|
{ |
514
|
|
|
// $request = $this->getRequest(); |
515
|
|
|
// |
516
|
|
|
// $form = new Application_Form_ChangePass(); |
517
|
|
|
// |
518
|
|
|
// if ($request->isPost()) { |
519
|
|
|
// |
520
|
|
|
// $data = $request->getPost(); |
521
|
|
|
// |
522
|
|
|
// if ($form->isValid($data)) { |
523
|
|
|
// |
524
|
|
|
// $oldPassword = $form->getValue('oldPassword'); |
525
|
|
|
// $password = $form->getValue('password'); |
526
|
|
|
// $confirm = $form->getValue('confirm'); |
527
|
|
|
// |
528
|
|
|
// if ($password != $confirm) { |
529
|
|
|
// |
530
|
|
|
// $this->view->message = ['Your new passwords didn\'t match!','danger']; |
531
|
|
|
// |
532
|
|
|
// } else { |
533
|
|
|
// |
534
|
|
|
// $user = $this->getUserService()->findUserById(Zend_Auth::getInstance()->getIdentity()); |
535
|
|
|
// |
536
|
|
|
// if($this->getUserService()->checkPassword($user, $oldPassword)) { |
537
|
|
|
// |
538
|
|
|
// $this->getUserService()->changePassword($user, $password); |
539
|
|
|
// $this->view->message = ['Password successfully updated. Click here to <a href ="/">return</a>.','success']; |
540
|
|
|
// return; |
541
|
|
|
// |
542
|
|
|
// } else { |
543
|
|
|
// |
544
|
|
|
// $this->view->message = ['Your password was wrong!','danger']; |
545
|
|
|
// |
546
|
|
|
// } |
547
|
|
|
// } |
548
|
|
|
// } |
549
|
|
|
// } |
550
|
|
|
// $this->view->form = $form; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @SWG\Post( |
555
|
|
|
* path="/{locale}/me/change-email/", |
556
|
|
|
* tags={"user_profile"}, |
557
|
|
|
* @SWG\Response(response="200", description="Changes a users email."), |
558
|
|
|
* @SWG\Parameter( |
559
|
|
|
* name="locale", |
560
|
|
|
* in="path", |
561
|
|
|
* type="string", |
562
|
|
|
* description="the locale to use", |
563
|
|
|
* required=true, |
564
|
|
|
* default="en_GB" |
565
|
|
|
* ), |
566
|
|
|
* @SWG\Parameter( |
567
|
|
|
* name="email", |
568
|
|
|
* in="path", |
569
|
|
|
* type="string", |
570
|
|
|
* description="the email of the user", |
571
|
|
|
* required=true, |
572
|
|
|
* default="[email protected]" |
573
|
|
|
* ), |
574
|
|
|
* @SWG\Parameter( |
575
|
|
|
* name="token", |
576
|
|
|
* in="path", |
577
|
|
|
* type="formData", |
578
|
|
|
* description="the email link token", |
579
|
|
|
* required=true, |
580
|
|
|
* default="r4nd0mT0k3n" |
581
|
|
|
* ), |
582
|
|
|
* @SWG\Parameter( |
583
|
|
|
* name="password", |
584
|
|
|
* in="formData", |
585
|
|
|
* type="string", |
586
|
|
|
* description="a password for the user", |
587
|
|
|
* required=true, |
588
|
|
|
* default="password" |
589
|
|
|
* ), |
590
|
|
|
* @SWG\Parameter( |
591
|
|
|
* name="confirm", |
592
|
|
|
* in="formData", |
593
|
|
|
* type="string", |
594
|
|
|
* description="password confirmation", |
595
|
|
|
* required=true, |
596
|
|
|
* default="password" |
597
|
|
|
* ) |
598
|
|
|
* ) |
599
|
|
|
* @throws Exception |
600
|
|
|
*/ |
601
|
|
|
public function changeEmailAction() |
602
|
|
|
{ |
603
|
|
|
// /* @var $request Zend_Controller_Request_Http */ |
604
|
|
|
// $request = $this->getRequest(); |
605
|
|
|
// |
606
|
|
|
// $user = $this->getUserService()->findUserById(Zend_Auth::getInstance()->getIdentity()); |
607
|
|
|
// |
608
|
|
|
// $form = new Application_Form_ChangeEmail(); |
609
|
|
|
// |
610
|
|
|
// if ($request->isPost()) { |
611
|
|
|
// |
612
|
|
|
// $data = $request->getPost(); |
613
|
|
|
// |
614
|
|
|
// if ($form->isValid($data)) { |
615
|
|
|
// |
616
|
|
|
// $newEmail = $form->getValue('email'); |
617
|
|
|
// $password = $form->getValue('password'); |
618
|
|
|
// |
619
|
|
|
// $existing = $this->getUserService()->findUserByEmail($newEmail); |
620
|
|
|
// if($existing) { |
621
|
|
|
// |
622
|
|
|
// $this->view->message = ['This email is already registered with a Cloud Tax Return account.','danger']; |
623
|
|
|
// return; |
624
|
|
|
// } |
625
|
|
|
// |
626
|
|
|
// if ($this->getUserService()->checkPassword($user, $password)) { |
627
|
|
|
// |
628
|
|
|
// $link = $this->getUserService()->generateEmailLink($user); |
629
|
|
|
// |
630
|
|
|
// try { |
631
|
|
|
// |
632
|
|
|
// $siteURL = SITE_URL; |
633
|
|
|
// $currentEmail = $user->getEmail(); |
634
|
|
|
// $token = $link->getToken(); |
635
|
|
|
// |
636
|
|
|
// $message = |
637
|
|
|
// <<<END |
638
|
|
|
// You have requested to change the login email for your Cloud Tax Return account to $newEmail. Click on the link below to confirm this change. |
639
|
|
|
// You can ignore this email if you do not wish to change your address.<br / <br /> |
640
|
|
|
//<a href="$siteURL/reset-email/$currentEmail/$newEmail/$token">Switch to my new email address.</a>. |
641
|
|
|
//END; |
642
|
|
|
// |
643
|
|
|
// /** @var Mail $emailService */ |
644
|
|
|
// $mail = new Mail(); |
645
|
|
|
// $mail->setFrom('[email protected]') |
646
|
|
|
// ->setTo($currentEmail) |
647
|
|
|
// ->setSubject('Change your email address on Cloud Tax Return.') |
648
|
|
|
// ->setHeader(Template::getHeader()) |
649
|
|
|
// ->setFooter(Template::getFooter()) |
650
|
|
|
// ->setMessage($message) |
651
|
|
|
// ->send(); |
652
|
|
|
// |
653
|
|
|
// $this->view->message = ['Please check your email for a link to activate your new address.','info']; |
654
|
|
|
// $this->view->form = null; |
655
|
|
|
// |
656
|
|
|
// } catch (Exception $e) { |
657
|
|
|
// $this->view->message = ['We were unable to send your e-mail confirmation. Please contact '.$this->config->email->support.'.','danger']; |
658
|
|
|
// } |
659
|
|
|
// |
660
|
|
|
// } else { |
661
|
|
|
// $this->view->message = ['Your password was wrong','danger']; |
662
|
|
|
// } |
663
|
|
|
// |
664
|
|
|
// } |
665
|
|
|
// } |
666
|
|
|
// $this->view->form = $form; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* @SWG\Post( |
672
|
|
|
* path="/{locale}/me/reset-email/", |
673
|
|
|
* tags={"user_profile"}, |
674
|
|
|
* @SWG\Response(response="200", description="Resets the users email."), |
675
|
|
|
* @SWG\Parameter( |
676
|
|
|
* name="locale", |
677
|
|
|
* in="path", |
678
|
|
|
* type="string", |
679
|
|
|
* description="the locale to use", |
680
|
|
|
* required=true, |
681
|
|
|
* default="en_GB" |
682
|
|
|
* ), |
683
|
|
|
* @SWG\Parameter( |
684
|
|
|
* name="email", |
685
|
|
|
* in="formData", |
686
|
|
|
* type="string", |
687
|
|
|
* description="the email of the user", |
688
|
|
|
* required=true, |
689
|
|
|
* default="[email protected]" |
690
|
|
|
* ), |
691
|
|
|
* @SWG\Parameter( |
692
|
|
|
* name="newemail", |
693
|
|
|
* in="formData", |
694
|
|
|
* type="string", |
695
|
|
|
* description="password confirmation", |
696
|
|
|
* required=true, |
697
|
|
|
* default="password" |
698
|
|
|
* ), |
699
|
|
|
* @SWG\Parameter( |
700
|
|
|
* name="token", |
701
|
|
|
* in="path", |
702
|
|
|
* type="string", |
703
|
|
|
* description="the email link token", |
704
|
|
|
* required=true, |
705
|
|
|
* default="r4nd0mT0k3n" |
706
|
|
|
* ), |
707
|
|
|
* @SWG\Parameter( |
708
|
|
|
* name="password", |
709
|
|
|
* in="formData", |
710
|
|
|
* type="string", |
711
|
|
|
* description="a password for the user", |
712
|
|
|
* required=true, |
713
|
|
|
* default="password" |
714
|
|
|
* ), |
715
|
|
|
* ) |
716
|
|
|
* @throws Exception |
717
|
|
|
*/ |
718
|
|
|
public function resetEmailAction() |
719
|
|
|
{ |
720
|
|
|
// $email = $this->_request->getParam('email'); |
721
|
|
|
// $newEmail = $this->_request->getParam('newemail'); |
722
|
|
|
// $token = $this->_request->getParam('token'); |
723
|
|
|
// |
724
|
|
|
// try { |
725
|
|
|
// |
726
|
|
|
// $link = $this->getUserService()->findEmailLink($email, $token); |
727
|
|
|
// $user = $link->getUser(); |
728
|
|
|
// $user->setEmail($newEmail); |
729
|
|
|
// $this->getUserService()->saveUser($user); |
730
|
|
|
// $this->getUserService()->deleteEmailLink($link); |
731
|
|
|
// $this->view->message = ['You have switched your email address. Please log in with '.$newEmail.' from now on.', 'success']; |
732
|
|
|
// |
733
|
|
|
// } catch (EmailLinkException $e) { |
734
|
|
|
// $this->view->message = [$e->getMessage(), 'danger']; |
735
|
|
|
// return; |
736
|
|
|
// } catch (Exception $e) { |
737
|
|
|
// throw $e; |
738
|
|
|
// } |
739
|
|
|
} |
740
|
|
|
} |
741
|
|
|
|