vendor/symfony/security-http/Authenticator/AbstractLoginFormAuthenticator.php line 63

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Authenticator;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  16. use Symfony\Component\Security\Http\SecurityRequestAttributes;
  17. /**
  18.  * A base class to make form login authentication easier!
  19.  *
  20.  * @author Ryan Weaver <ryan@symfonycasts.com>
  21.  */
  22. abstract class AbstractLoginFormAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterfaceInteractiveAuthenticatorInterface
  23. {
  24.     /**
  25.      * Return the URL to the login page.
  26.      */
  27.     abstract protected function getLoginUrl(Request $request): string;
  28.     /**
  29.      * Override to change the request conditions that have to be
  30.      * matched in order to handle the login form submit.
  31.      *
  32.      * This default implementation handles all POST requests to the
  33.      * login path (@see getLoginUrl()).
  34.      */
  35.     public function supports(Request $request): bool
  36.     {
  37.         return $request->isMethod('POST') && $this->getLoginUrl($request) === $request->getBaseUrl().$request->getPathInfo();
  38.     }
  39.     /**
  40.      * Override to change what happens after a bad username/password is submitted.
  41.      */
  42.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  43.     {
  44.         if ($request->hasSession()) {
  45.             $request->getSession()->set(SecurityRequestAttributes::AUTHENTICATION_ERROR$exception);
  46.         }
  47.         $url $this->getLoginUrl($request);
  48.         return new RedirectResponse($url);
  49.     }
  50.     /**
  51.      * Override to control what happens when the user hits a secure page
  52.      * but isn't logged in yet.
  53.      */
  54.     public function start(Request $requestAuthenticationException $authException null): Response
  55.     {
  56.         $url $this->getLoginUrl($request);
  57.         return new RedirectResponse($url);
  58.     }
  59.     public function isInteractive(): bool
  60.     {
  61.         return true;
  62.     }
  63. }