* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace FOS\UserBundle\Controller; use FOS\UserBundle\Event\FilterUserResponseEvent; use FOS\UserBundle\Event\FormEvent; use FOS\UserBundle\Event\GetResponseUserEvent; use FOS\UserBundle\Form\Factory\FactoryInterface; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Model\UserInterface; use FOS\UserBundle\Model\UserManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use App\Entity\Users; /** * Controller managing the registration. * * @author Thibault Duplessis * @author Christophe Coevoet */ class RegistrationController extends AbstractController { private $eventDispatcher; private $formFactory; private $userManager; private $tokenStorage; public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenStorageInterface $tokenStorage) { $this->eventDispatcher = $eventDispatcher; $this->formFactory = $formFactory; $this->userManager = $userManager; $this->tokenStorage = $tokenStorage; } /** * @param Request $request * * @return Response */ public function registerAction(Request $request) { $user = $this->userManager->createUser(); $event = new GetResponseUserEvent($user, $request); $this->eventDispatcher->dispatch($event, FOSUserEvents::REGISTRATION_INITIALIZE); if (null !== $event->getResponse()) { return $event->getResponse(); } $form = $this->formFactory->createForm(); $form->setData($user); $form->handleRequest($request); if ($form->isSubmitted()) { if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $userCheck = $em->getRepository(Users::class)->findOneBy(['email'=>$user->getEmail()]); if ($userCheck){ $this->addFlash('danger','Email is already registered!'); return new RedirectResponse($this->generateUrl('fos_user_registration_register')); } $event = new FormEvent($form, $request); $this->eventDispatcher->dispatch($event, FOSUserEvents::REGISTRATION_SUCCESS); $this->userManager->updateUser($user); if (null === $response = $event->getResponse()) { $url = $this->generateUrl('fos_user_registration_confirmed'); $response = new RedirectResponse($url); } $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user, $request, $response), FOSUserEvents::REGISTRATION_COMPLETED); return $response; } $event = new FormEvent($form, $request); $this->eventDispatcher->dispatch($event, FOSUserEvents::REGISTRATION_FAILURE); if (null !== $response = $event->getResponse()) { return $response; } } return $this->render('@FOSUser/Registration/register.html.twig', array( 'form' => $form->createView(), )); } /** * Tell the user to check their email provider. */ public function checkEmailAction(Request $request) { $email = $request->getSession()->get('fos_user_send_confirmation_email/email'); if (empty($email)) { return new RedirectResponse($this->generateUrl('fos_user_registration_register')); } $request->getSession()->remove('fos_user_send_confirmation_email/email'); $user = $this->userManager->findUserByEmail($email); if (null === $user) { return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login')); } return $this->render('@FOSUser/Registration/check_email.html.twig', array( 'user' => $user, )); } /** * Receive the confirmation token from user email provider, login the user. * * @param Request $request * @param string $token * * @return Response */ public function confirmAction(Request $request, $token) { $userManager = $this->userManager; $user = $userManager->findUserByConfirmationToken($token); if (null === $user) { return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login')); } $user->setConfirmationToken(null); $user->setEnabled(true); $event = new GetResponseUserEvent($user, $request); $this->eventDispatcher->dispatch($event, FOSUserEvents::REGISTRATION_CONFIRM); $userManager->updateUser($user); if (null === $response = $event->getResponse()) { $url = $this->generateUrl('fos_user_registration_confirmed'); $response = new RedirectResponse($url); } $this->eventDispatcher->dispatch(new FilterUserResponseEvent($user, $request, $response), FOSUserEvents::REGISTRATION_CONFIRMED); return $response; } /** * Tell the user his account is now confirmed. */ public function confirmedAction(Request $request) { $user = $this->getUser(); if (!is_object($user) || !$user instanceof UserInterface) { throw new AccessDeniedException('This user does not have access to this section.'); } return $this->render('@FOSUser/Registration/confirmed.html.twig', array( 'user' => $user, 'targetUrl' => $this->getTargetUrlFromSession($request->getSession()), )); } /** * @return string|null */ private function getTargetUrlFromSession(SessionInterface $session) { $key = sprintf('_security.%s.target_path', $this->tokenStorage->getToken()->getProviderKey()); if ($session->has($key)) { return $session->get($key); } return null; } }