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