vendor/symfony/security-http/Authentication/AuthenticationUtils.php line 61

  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\Authentication;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  14. use Symfony\Component\Security\Http\SecurityRequestAttributes;
  15. /**
  16.  * Extracts Security Errors from Request.
  17.  *
  18.  * @author Boris Vujicic <boris.vujicic@gmail.com>
  19.  */
  20. class AuthenticationUtils
  21. {
  22.     private RequestStack $requestStack;
  23.     public function __construct(RequestStack $requestStack)
  24.     {
  25.         $this->requestStack $requestStack;
  26.     }
  27.     public function getLastAuthenticationError(bool $clearSession true): ?AuthenticationException
  28.     {
  29.         $request $this->getRequest();
  30.         $authenticationException null;
  31.         
  32.         
  33.         
  34.         if ($request->attributes->has(SecurityRequestAttributes::AUTHENTICATION_ERROR)) {
  35.             $authenticationException $request->attributes->get(SecurityRequestAttributes::AUTHENTICATION_ERROR);
  36.         } elseif ($request->hasSession() && ($session $request->getSession())->has(SecurityRequestAttributes::AUTHENTICATION_ERROR)) {
  37.             $authenticationException $session->get(SecurityRequestAttributes::AUTHENTICATION_ERROR);
  38.             if ($clearSession) {
  39.                 $session->remove(SecurityRequestAttributes::AUTHENTICATION_ERROR);
  40.             }
  41.         }
  42.         return $authenticationException;
  43.     }
  44.     public function getLastUsername(): string
  45.     {
  46.         $request $this->getRequest();
  47.         if ($request->attributes->has(SecurityRequestAttributes::LAST_USERNAME)) {
  48.             return $request->attributes->get(SecurityRequestAttributes::LAST_USERNAME'');
  49.         }
  50.         return $request->hasSession() ? $request->getSession()->get(SecurityRequestAttributes::LAST_USERNAME'') : '';
  51.     }
  52.     /**
  53.      * @throws \LogicException
  54.      */
  55.     private function getRequest(): Request
  56.     {
  57.         $request $this->requestStack->getCurrentRequest();
  58.      
  59.         if (null === $request) {
  60.             throw new \LogicException('Request should exist so it can be processed for error.');
  61.         }
  62.         return $request;
  63.     }
  64. }