1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\EmailUpdateConfirmationBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Azine\EmailUpdateConfirmationBundle\AzineEmailUpdateConfirmationEvents; |
6
|
|
|
use Azine\EmailUpdateConfirmationBundle\Services\EmailUpdateConfirmation; |
7
|
|
|
use FOS\UserBundle\Event\UserEvent; |
8
|
|
|
use FOS\UserBundle\Model\User; |
9
|
|
|
use FOS\UserBundle\Model\UserInterface; |
10
|
|
|
use FOS\UserBundle\Model\UserManagerInterface; |
11
|
|
|
use FOS\UserBundle\Util\CanonicalFieldsUpdater; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Controller managing the confirmation of changed user email. |
20
|
|
|
* |
21
|
|
|
* @author Dominik Businger <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ConfirmEmailUpdateController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EventDispatcherInterface |
27
|
|
|
*/ |
28
|
|
|
private $eventDispatcher; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var UserManagerInterface |
32
|
|
|
*/ |
33
|
|
|
private $userManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EmailUpdateConfirmation |
37
|
|
|
*/ |
38
|
|
|
private $emailUpdateConfirmation; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TranslatorInterface |
42
|
|
|
*/ |
43
|
|
|
private $translator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var CanonicalFieldsUpdater |
47
|
|
|
*/ |
48
|
|
|
private $canonicalFieldsUpdater; |
49
|
|
|
|
50
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, UserManagerInterface $userManager, EmailUpdateConfirmation $emailUpdateConfirmation, TranslatorInterface $translator, CanonicalFieldsUpdater $canonicalFieldsUpdater) |
51
|
|
|
{ |
52
|
|
|
$this->eventDispatcher = $eventDispatcher; |
53
|
|
|
$this->userManager = $userManager; |
54
|
|
|
$this->emailUpdateConfirmation = $emailUpdateConfirmation; |
55
|
|
|
$this->translator = $translator; |
56
|
|
|
$this->canonicalFieldsUpdater = $canonicalFieldsUpdater; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Confirm user`s email update. |
61
|
|
|
* |
62
|
|
|
* @param Request $request |
63
|
|
|
* @param string $token |
64
|
|
|
* @param string $redirectRoute |
65
|
|
|
* |
66
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
67
|
|
|
*/ |
68
|
|
|
public function confirmEmailUpdateAction(Request $request, $token, $redirectRoute) |
69
|
|
|
{ |
70
|
|
|
/** @var User $user */ |
71
|
|
|
$user = $this->userManager->findUserByConfirmationToken($token); |
72
|
|
|
|
73
|
|
|
// If user was not found throw 404 exception |
74
|
|
|
if (!$user) { |
|
|
|
|
75
|
|
|
throw $this->createNotFoundException($this->translator->trans('email_update.error.message')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Show invalid token message if the user id found via token does not match the current users id (e.g. anon. or other user) |
79
|
|
|
if (!($this->getUser() instanceof UserInterface) || ($user->getId() !== $this->getUser()->getId())) { |
80
|
|
|
throw new AccessDeniedException($this->translator->trans('email_update.error.message')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$newEmail = $this->emailUpdateConfirmation->fetchEncryptedEmailFromConfirmationLink($user, $request->get('target')); |
84
|
|
|
|
85
|
|
|
// Update user email |
86
|
|
|
if ($newEmail) { |
87
|
|
|
$user->setConfirmationToken($this->emailUpdateConfirmation->getEmailConfirmedToken()); |
88
|
|
|
$user->setEmail($newEmail); |
89
|
|
|
$user->setEmail($this->canonicalFieldsUpdater->canonicalizeEmail($newEmail)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->userManager->updateUser($user); |
93
|
|
|
|
94
|
|
|
$event = new UserEvent($user, $request); |
95
|
|
|
$this->eventDispatcher->dispatch(AzineEmailUpdateConfirmationEvents::EMAIL_UPDATE_SUCCESS, $event); |
96
|
|
|
|
97
|
|
|
return $this->redirect($this->generateUrl($redirectRoute)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|