src/Security/AuthenticationUtilsExtend.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use LogicException;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. class AuthenticationUtilsExtend extends AuthenticationUtils
  9. {
  10.     public function __construct(private RequestStack $requestStack)
  11.     {
  12.         parent::__construct($this->requestStack);
  13.     }
  14.     public function getLastAuthenticationError(bool $clearSession true)
  15.     {
  16.         $request $this->getRequest();
  17.         $authenticationException null;
  18.         $session $request->getSession();
  19.         if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
  20.             $authenticationException $request->attributes->get(Security::AUTHENTICATION_ERROR);
  21.             $session->set(Security::AUTHENTICATION_ERROR$authenticationException);
  22.         } elseif ($request->hasSession() && $session->has(Security::AUTHENTICATION_ERROR)) {
  23.             $authenticationException $session->get(Security::AUTHENTICATION_ERROR);
  24.             if ($clearSession) {
  25.                 $session->remove(Security::AUTHENTICATION_ERROR);
  26.             }
  27.         }
  28.         return $authenticationException;
  29.     }
  30.     public function getLastUsername()
  31.     {
  32.         $request $this->getRequest();
  33.         if ($request->attributes->has(Security::LAST_USERNAME)) {
  34.             return $request->attributes->get(Security::LAST_USERNAME'');
  35.         }
  36.         return $request->hasSession() ? $request->getSession()->get(Security::LAST_USERNAME'') : '';
  37.     }
  38.     private function getRequest(): Request
  39.     {
  40.         if ($this->requestStack->getParentRequest()?->attributes?->has(Security::AUTHENTICATION_ERROR)) {
  41.             $request $this->requestStack->getParentRequest();
  42.         } else {
  43.             $request $this->requestStack->getCurrentRequest();
  44.         }
  45.         if (null === $request) {
  46.             throw new LogicException('Request should exist so it can be processed for error.');
  47.         }
  48.         return $request;
  49.     }
  50. }