src/Controller/Security/SecurityController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  10. use FOS\UserBundle\Controller\SecurityController as BaseSecurityController;
  11. class SecurityController extends BaseSecurityController
  12. {
  13.     private $tokenManager;
  14.     public function __construct(CsrfTokenManagerInterface $tokenManager null)
  15.     {
  16.         $this->tokenManager $tokenManager;
  17.     }
  18.     /**
  19.      * @param Request $request
  20.      *
  21.      * @return Response
  22.      */
  23.     public function loginAction(Request $request)
  24.     {
  25.         /** @var $session Session */
  26.         $session $request->getSession();
  27.         $authErrorKey Security::AUTHENTICATION_ERROR;
  28.         $lastUsernameKey Security::LAST_USERNAME;
  29.         // get the error if any (works with forward and redirect -- see below)
  30.         if ($request->attributes->has($authErrorKey)) {
  31.             $error $request->attributes->get($authErrorKey);
  32.         } elseif (null !== $session && $session->has($authErrorKey)) {
  33.             $error $session->get($authErrorKey);
  34.             $session->remove($authErrorKey);
  35.         } else {
  36.             $error null;
  37.         }
  38.         if (!$error instanceof AuthenticationException) {
  39.             $error null// The value does not come from the security component.
  40.         }
  41.         // last username entered by the user
  42.         $lastUsername = (null === $session) ? '' $session->get($lastUsernameKey);
  43.         $csrfToken $this->tokenManager
  44.             $this->tokenManager->getToken('authenticate')->getValue()
  45.             : null;
  46.         return $this->renderLogin(array(
  47.             'request'   => $request,
  48.             'last_username' => $lastUsername,
  49.             'error' => $error,
  50.             'page' => $this->getDoctrine()->getManager()->getRepository('App\Entity\Page')->findOneBySlug('login'),
  51.             'csrf_token' => $csrfToken
  52.         ));
  53.     }
  54.     public function renderLogin(array $data) {
  55.         $request $data['request'];
  56.         $requestAttributes $request->attributes;
  57.         if ($requestAttributes->get('_route') == 'admin_login') {
  58.             $template sprintf('security/admin_login.html.twig');
  59.         } else {
  60.             $template sprintf('security/signIn.html.twig');
  61.         }
  62.         return $this->render($template$data);
  63.     }
  64. }