vendor/sonata-project/user-bundle/src/Action/LoginAction.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\UserBundle\Action;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
  14. use Sonata\UserBundle\Model\UserInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\Session;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  22. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  23. use Symfony\Component\Security\Core\Security;
  24. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  25. use Twig\Environment;
  26. final class LoginAction
  27. {
  28.     /**
  29.      * @var Environment
  30.      */
  31.     private $twig;
  32.     /**
  33.      * @var UrlGeneratorInterface
  34.      */
  35.     private $urlGenerator;
  36.     /**
  37.      * @var AuthorizationCheckerInterface
  38.      */
  39.     private $authorizationChecker;
  40.     /**
  41.      * @var Pool
  42.      */
  43.     private $adminPool;
  44.     /**
  45.      * @var TemplateRegistryInterface
  46.      */
  47.     private $templateRegistry;
  48.     /**
  49.      * @var TokenStorageInterface
  50.      */
  51.     private $tokenStorage;
  52.     /**
  53.      * @var Session
  54.      */
  55.     private $session;
  56.     /**
  57.      * @var CsrfTokenManagerInterface
  58.      */
  59.     private $csrfTokenManager;
  60.     public function __construct(
  61.         Environment $twig,
  62.         UrlGeneratorInterface $urlGenerator,
  63.         AuthorizationCheckerInterface $authorizationChecker,
  64.         Pool $adminPool,
  65.         TemplateRegistryInterface $templateRegistry,
  66.         TokenStorageInterface $tokenStorage,
  67.         Session $session
  68.     ) {
  69.         $this->twig $twig;
  70.         $this->urlGenerator $urlGenerator;
  71.         $this->authorizationChecker $authorizationChecker;
  72.         $this->adminPool $adminPool;
  73.         $this->templateRegistry $templateRegistry;
  74.         $this->tokenStorage $tokenStorage;
  75.         $this->session $session;
  76.     }
  77.     public function __invoke(Request $request): Response
  78.     {
  79.         if ($this->isAuthenticated()) {
  80.             $this->session->getFlashBag()->add('sonata_user_error''sonata_user_already_authenticated');
  81.             return new RedirectResponse($this->urlGenerator->generate('sonata_admin_dashboard'));
  82.         }
  83.         $session $request->getSession();
  84.         $authErrorKey Security::AUTHENTICATION_ERROR;
  85.         // get the error if any (works with forward and redirect -- see below)
  86.         if ($request->attributes->has($authErrorKey)) {
  87.             $error $request->attributes->get($authErrorKey);
  88.         } elseif (null !== $session && $session->has($authErrorKey)) {
  89.             $error $session->get($authErrorKey);
  90.             $session->remove($authErrorKey);
  91.         } else {
  92.             $error null;
  93.         }
  94.         if (!$error instanceof AuthenticationException) {
  95.             $error null// The value does not come from the security component.
  96.         }
  97.         if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  98.             $refererUri $request->server->get('HTTP_REFERER');
  99.             $url $refererUri && $refererUri !== $request->getUri() ? $refererUri $this->urlGenerator->generate('sonata_admin_dashboard');
  100.             return new RedirectResponse($url);
  101.         }
  102.         $csrfToken null;
  103.         if ($this->csrfTokenManager) {
  104.             $csrfToken $this->csrfTokenManager->getToken('authenticate')->getValue();
  105.         }
  106.         return new Response($this->twig->render('@SonataUser/Admin/Security/login.html.twig', [
  107.             'admin_pool' => $this->adminPool,
  108.             'base_template' => $this->templateRegistry->getTemplate('layout'),
  109.             'csrf_token' => $csrfToken,
  110.             'error' => $error,
  111.             'last_username' => (null === $session) ? '' $session->get(Security::LAST_USERNAME),
  112.             'reset_route' => $this->urlGenerator->generate('sonata_user_admin_resetting_request'),
  113.         ]));
  114.     }
  115.     public function setCsrfTokenManager(CsrfTokenManagerInterface $csrfTokenManager): void
  116.     {
  117.         $this->csrfTokenManager $csrfTokenManager;
  118.     }
  119.     private function isAuthenticated(): bool
  120.     {
  121.         $token $this->tokenStorage->getToken();
  122.         if (!$token) {
  123.             return false;
  124.         }
  125.         $user $token->getUser();
  126.         return $user instanceof UserInterface;
  127.     }
  128. }