src/ProPneu/Service/UserBundle/Controller/SecurityController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\ProPneu\Service\UserBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  7. use FOS\UserBundle\Controller\SecurityController as BaseSecurityController;
  8. /**
  9.  * Class SecurityController
  10.  * @package App\ProPneu\Service\UserBundle\Controller
  11.  */
  12. class SecurityController extends BaseSecurityController
  13. {
  14.     /**
  15.      * Afficher la page d'authentification
  16.      * @param Request $request
  17.      * @return Response
  18.      */
  19.     public function loginAction(Request $request)
  20.     {
  21.         // Rédiriger vers la page spécifique si l'utilisateur est connecté
  22.         $securityContext $this->get('security.authorization_checker');
  23.         if ($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  24.             $authChecker $this->container->get('security.authorization_checker');
  25.             $router      $this->container->get('router');
  26.             if ($authChecker->isGranted('ROLE_ADMIN') || $authChecker->isGranted('ROLE_SUPERADMIN') || $authChecker->isGranted('ROLE_CHEF_CENTRE')) {
  27.                 return new RedirectResponse($router->generate('dashboard_index'), 307);
  28.             }
  29.         }
  30.         /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
  31.         $session $request->getSession();
  32.         if (class_exists('\Symfony\Component\Security\Core\Security')) {
  33.             $authErrorKey    Security::AUTHENTICATION_ERROR;
  34.             $lastUsernameKey Security::LAST_USERNAME;
  35.         } else {
  36.             // BC for SF < 2.6
  37.             $authErrorKey    Security::AUTHENTICATION_ERROR;
  38.             $lastUsernameKey Security::LAST_USERNAME;
  39.         }
  40.         // get the error if any (works with forward and redirect -- see below)
  41.         if ($request->attributes->has($authErrorKey)) {
  42.             $error $request->attributes->get($authErrorKey);
  43.         } elseif (null !== $session && $session->has($authErrorKey)) {
  44.             $error $session->get($authErrorKey);
  45.             $session->remove($authErrorKey);
  46.         } else {
  47.             $error null;
  48.         }
  49.         if (!$error instanceof AuthenticationException) {
  50.             $error null// The value does not come from the security component.
  51.         }
  52.         // last username entered by the user
  53.         $lastUsername = (null === $session) ? '' $session->get($lastUsernameKey);
  54.         if ($this->has('security.csrf.token_manager')) {
  55.             $csrfToken $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
  56.         } else {
  57.             // BC for SF < 2.4
  58.             $csrfToken $this->has('form.csrf_provider')
  59.                 ? $this->get('form.csrf_provider')->generateCsrfToken('authenticate')
  60.                 : null;
  61.         }
  62.         $_branche_name $request->attributes->get('_branche');
  63.         $data = [
  64.             'last_username' => $lastUsername,
  65.             'csrf_token'    => $csrfToken,
  66.             'branche_name'  => $_branche_name
  67.         ];
  68.         if ($error) {
  69.             $data['error'] = $error;
  70.         }
  71.         return $this->render('UserBundle:Security:login.html.twig'$data);
  72.     }
  73.     public function logoutAction()
  74.     {
  75.         parent::logoutAction(); // TODO: Change the autogenerated stub
  76.     }
  77. }