src/mywebsolutions/user-bundle/Controller/SecurityController.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * Class SecurityController
  4.  * @package MWS\UserBundle\Controller
  5.  * @author Martin Walther <martin@myweb.solutions>
  6.  *
  7.  * (c) MyWebSolutions
  8.  */
  9. namespace MWS\UserBundle\Controller;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Security;
  16. class SecurityController extends AbstractController
  17. {
  18.     /**
  19.      * @param Request $request
  20.      *
  21.      * @return Response
  22.      */
  23.     public function loginAction(Request $request): Response
  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->has('security.csrf.token_manager')
  44.             ? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue()
  45.             : null;
  46.         return $this->renderLogin(array(
  47.             'last_username' => $lastUsername,
  48.             'error' => $error,
  49.             'csrf_token' => $csrfToken,
  50.         ));
  51.     }
  52.     /**
  53.      * Renders the login template with the given parameters. Overwrite this function in
  54.      * an extended controller to provide additional data for the login template.
  55.      *
  56.      * @param array $data
  57.      *
  58.      * @return Response
  59.      */
  60.     protected function renderLogin(array $data): Response
  61.     {
  62.         return $this->render('@MWSUser/Security/login.html.twig'$data);
  63.     }
  64.     public function checkAction()
  65.     {
  66.         throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  67.     }
  68.     public function logoutAction()
  69.     {
  70.         throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  71.     }
  72. }