|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSUserBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\UserBundle\Controller; |
| 13 | + |
| 14 | +use FOS\UserBundle\Event\UserEvent; |
| 15 | +use FOS\UserBundle\FOSUserEvents; |
| 16 | +use FOS\UserBundle\Model\User; |
| 17 | +use FOS\UserBundle\Model\UserInterface; |
| 18 | +use FOS\UserBundle\Model\UserManagerInterface; |
| 19 | +use FOS\UserBundle\Services\EmailConfirmation\EmailUpdateConfirmation; |
| 20 | +use FOS\UserBundle\Util\CanonicalFieldsUpdater; |
| 21 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 22 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 23 | +use Symfony\Component\HttpFoundation\Request; |
| 24 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 25 | +use Symfony\Component\Translation\TranslatorInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Controller managing the confirmation of changed user email. |
| 29 | + * |
| 30 | + * @author Dominik Businger <git@azine.me> |
| 31 | + */ |
| 32 | +class ConfirmEmailUpdateController extends AbstractController |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var EventDispatcherInterface |
| 36 | + */ |
| 37 | + private $eventDispatcher; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var UserManagerInterface |
| 41 | + */ |
| 42 | + private $userManager; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var EmailUpdateConfirmation |
| 46 | + */ |
| 47 | + private $emailUpdateConfirmation; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var TranslatorInterface |
| 51 | + */ |
| 52 | + private $translator; |
| 53 | + /** |
| 54 | + * @var CanonicalFieldsUpdater |
| 55 | + */ |
| 56 | + private $canonicalFieldsUpdater; |
| 57 | + |
| 58 | + /** |
| 59 | + * @param EventDispatcherInterface $eventDispatcher |
| 60 | + * @param UserManagerInterface $userManager |
| 61 | + * @param EmailUpdateConfirmation $emailUpdateConfirmation |
| 62 | + * @param TranslatorInterface $translator |
| 63 | + * @param CanonicalFieldsUpdater $canonicalFieldsUpdater |
| 64 | + */ |
| 65 | + public function __construct(EventDispatcherInterface $eventDispatcher, UserManagerInterface $userManager, EmailUpdateConfirmation $emailUpdateConfirmation, TranslatorInterface $translator, CanonicalFieldsUpdater $canonicalFieldsUpdater) |
| 66 | + { |
| 67 | + $this->eventDispatcher = $eventDispatcher; |
| 68 | + $this->userManager = $userManager; |
| 69 | + $this->emailUpdateConfirmation = $emailUpdateConfirmation; |
| 70 | + $this->translator = $translator; |
| 71 | + $this->canonicalFieldsUpdater = $canonicalFieldsUpdater; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Confirm user`s email update. |
| 76 | + * |
| 77 | + * @param Request $request |
| 78 | + * @param string $token |
| 79 | + * |
| 80 | + * @return \Symfony\Component\HttpFoundation\RedirectResponse |
| 81 | + */ |
| 82 | + public function confirmEmailUpdateAction(Request $request, $token) |
| 83 | + { |
| 84 | + /** @var User $user */ |
| 85 | + $user = $this->userManager->findUserByConfirmationToken($token); |
| 86 | + |
| 87 | + // If user was not found throw 404 exception |
| 88 | + if (!$user) { |
| 89 | + throw $this->createNotFoundException($this->translator->trans('email_update.error.message', array(), 'FOSUserBundle')); |
| 90 | + } |
| 91 | + |
| 92 | + // Show invalid token message if the user id found via token does not match the current users id (e.g. anon. or other user) |
| 93 | + if (!($this->getUser() instanceof UserInterface) || ($user->getId() !== $this->getUser()->getId())) { |
| 94 | + throw new AccessDeniedException($this->translator->trans('email_update.error.message', array(), 'FOSUserBundle')); |
| 95 | + } |
| 96 | + |
| 97 | + $this->emailUpdateConfirmation->setUser($user); |
| 98 | + |
| 99 | + $newEmail = $this->emailUpdateConfirmation->fetchEncryptedEmailFromConfirmationLink($request->get('target')); |
| 100 | + |
| 101 | + // Update user email |
| 102 | + if ($newEmail) { |
| 103 | + $user->setConfirmationToken($this->emailUpdateConfirmation->getEmailConfirmedToken()); |
| 104 | + $user->setEmail($newEmail); |
| 105 | + $user->setEmail($this->canonicalFieldsUpdater->canonicalizeEmail($newEmail)); |
| 106 | + } |
| 107 | + |
| 108 | + $this->userManager->updateUser($user); |
| 109 | + |
| 110 | + $event = new UserEvent($user, $request); |
| 111 | + $this->eventDispatcher->dispatch(FOSUserEvents::EMAIL_UPDATE_SUCCESS, $event); |
| 112 | + |
| 113 | + return $this->redirect($this->generateUrl('fos_user_profile_show')); |
| 114 | + } |
| 115 | +} |
0 commit comments