|
14 | 14 | use FOS\UserBundle\Event\FilterUserResponseEvent; |
15 | 15 | use FOS\UserBundle\Event\FormEvent; |
16 | 16 | use FOS\UserBundle\Event\GetResponseUserEvent; |
| 17 | +use FOS\UserBundle\Event\UserEvent; |
17 | 18 | use FOS\UserBundle\Form\Factory\FactoryInterface; |
18 | 19 | use FOS\UserBundle\FOSUserEvents; |
| 20 | +use FOS\UserBundle\Model\User; |
19 | 21 | use FOS\UserBundle\Model\UserInterface; |
20 | 22 | use FOS\UserBundle\Model\UserManagerInterface; |
| 23 | +use FOS\UserBundle\Services\EmailConfirmation\EmailUpdateConfirmation; |
21 | 24 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
22 | 25 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23 | 26 | use Symfony\Component\HttpFoundation\RedirectResponse; |
24 | 27 | use Symfony\Component\HttpFoundation\Request; |
25 | 28 | use Symfony\Component\HttpFoundation\Response; |
26 | 29 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 30 | +use Symfony\Component\Translation\Translator; |
27 | 31 |
|
28 | 32 | /** |
29 | 33 | * Controller managing the user profile. |
@@ -102,4 +106,54 @@ public function editAction(Request $request) |
102 | 106 | 'form' => $form->createView(), |
103 | 107 | )); |
104 | 108 | } |
| 109 | + |
| 110 | + /** |
| 111 | + * Confirm user`s email update. |
| 112 | + * |
| 113 | + * @param Request $request |
| 114 | + * @param string $token |
| 115 | + * |
| 116 | + * @return \Symfony\Component\HttpFoundation\RedirectResponse |
| 117 | + */ |
| 118 | + public function confirmEmailUpdateAction(Request $request, $token) |
| 119 | + { |
| 120 | + $userManager = $this->container->get('fos_user.user_manager'); |
| 121 | + |
| 122 | + /** @var User $user */ |
| 123 | + $user = $userManager->findUserByConfirmationToken($token); |
| 124 | + |
| 125 | + // If user was not found throw 404 exception |
| 126 | + if (!$user) { |
| 127 | + /** @var Translator $translator */ |
| 128 | + $translator = $this->get('translator'); |
| 129 | + throw $this->createNotFoundException($translator->trans('email_update.error.message', array(), 'FOSUserBundle')); |
| 130 | + } |
| 131 | + |
| 132 | + // Show invalid token message if the user id found via token does not match the current users id (e.g. anon. or other user) |
| 133 | + if (!($this->getUser() instanceof UserInterface) || ($user->getId() !== $this->getUser()->getId())) { |
| 134 | + /** @var Translator $translator */ |
| 135 | + $translator = $this->get('translator'); |
| 136 | + throw new AccessDeniedException($translator->trans('email_update.error.message', array(), 'FOSUserBundle')); |
| 137 | + } |
| 138 | + |
| 139 | + /** @var EmailUpdateConfirmation $emailUpdateConfirmation */ |
| 140 | + $emailUpdateConfirmation = $this->get('fos_user.email_update_confirmation'); |
| 141 | + |
| 142 | + $emailUpdateConfirmation->setUser($user); |
| 143 | + |
| 144 | + $newEmail = $emailUpdateConfirmation->fetchEncryptedEmailFromConfirmationLink($request->get('target')); |
| 145 | + |
| 146 | + // Update user email |
| 147 | + if ($newEmail) { |
| 148 | + $user->setConfirmationToken($emailUpdateConfirmation->getEmailConfirmedToken()); |
| 149 | + $user->setEmail($newEmail); |
| 150 | + } |
| 151 | + |
| 152 | + $userManager->updateUser($user); |
| 153 | + |
| 154 | + $event = new UserEvent($user, $request); |
| 155 | + $this->get('event_dispatcher')->dispatch(FOSUserEvents::EMAIL_UPDATE_SUCCESS, $event); |
| 156 | + |
| 157 | + return $this->redirect($this->generateUrl('fos_user_profile_show')); |
| 158 | + } |
105 | 159 | } |
0 commit comments