Created
May 1, 2017 12:09
-
-
Save teeyo/147f2d5d21d1beadce133a51b02d9570 to your computer and use it in GitHub Desktop.
Redirecting authenticated users when they try to access registration page. [FOSUserBundle - Symfony 3]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the FOSUserBundle package. | |
* | |
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace TeeyoUserBundle\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\Controller; | |
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | |
use FOS\UserBundle\Controller\RegistrationController as BaseController; | |
/** | |
* Controller managing the registration. | |
* | |
* @author Thibault Duplessis <thibault.duplessis@gmail.com> | |
* @author Christophe Coevoet <stof@notk.org> | |
*/ | |
class RegistrationController extends BaseController | |
{ | |
/** | |
* @param Request $request | |
* | |
* @return Response | |
*/ | |
public function registerAction(Request $request) | |
{ | |
/** | |
* If the user has already logged in (marked as is authenticated fully by symfony's security) | |
* then redirect this user back (in my case, to the dashboard, which is the main entry for | |
* my logged in users) | |
*/ | |
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { | |
return $this->redirectToRoute('teeyo_quiz_homepage'); | |
} | |
/** @var $formFactory FactoryInterface */ | |
$formFactory = $this->get('fos_user.registration.form.factory'); | |
/** @var $userManager UserManagerInterface */ | |
$userManager = $this->get('fos_user.user_manager'); | |
/** @var $dispatcher EventDispatcherInterface */ | |
$dispatcher = $this->get('event_dispatcher'); | |
$user = $userManager->createUser(); | |
$user->setEnabled(true); | |
$event = new GetResponseUserEvent($user, $request); | |
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event); | |
if (null !== $event->getResponse()) { | |
return $event->getResponse(); | |
} | |
$form = $formFactory->createForm(); | |
$form->setData($user); | |
$form->handleRequest($request); | |
if ($form->isSubmitted()) { | |
if ($form->isValid()) { | |
$event = new FormEvent($form, $request); | |
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event); | |
$userManager->updateUser($user); | |
if (null === $response = $event->getResponse()) { | |
$url = $this->generateUrl('fos_user_registration_confirmed'); | |
$response = new RedirectResponse($url); | |
} | |
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); | |
return $response; | |
} | |
$event = new FormEvent($form, $request); | |
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event); | |
if (null !== $response = $event->getResponse()) { | |
return $response; | |
} | |
} | |
return $this->render('@FOSUser/Registration/register.html.twig', array( | |
'form' => $form->createView(), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment