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

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\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. use Twig\Environment;
  24. final class LoginAction
  25. {
  26.     private Environment $twig;
  27.     private UrlGeneratorInterface $urlGenerator;
  28.     private AuthenticationUtils $authenticationUtils;
  29.     private Pool $adminPool;
  30.     private TemplateRegistryInterface $templateRegistry;
  31.     private TokenStorageInterface $tokenStorage;
  32.     private TranslatorInterface $translator;
  33.     private ?CsrfTokenManagerInterface $csrfTokenManager;
  34.     public function __construct(
  35.         Environment $twig,
  36.         UrlGeneratorInterface $urlGenerator,
  37.         AuthenticationUtils $authenticationUtils,
  38.         Pool $adminPool,
  39.         TemplateRegistryInterface $templateRegistry,
  40.         TokenStorageInterface $tokenStorage,
  41.         TranslatorInterface $translator,
  42.         ?CsrfTokenManagerInterface $csrfTokenManager null
  43.     ) {
  44.         $this->twig $twig;
  45.         $this->urlGenerator $urlGenerator;
  46.         $this->authenticationUtils $authenticationUtils;
  47.         $this->adminPool $adminPool;
  48.         $this->templateRegistry $templateRegistry;
  49.         $this->tokenStorage $tokenStorage;
  50.         $this->translator $translator;
  51.         $this->csrfTokenManager $csrfTokenManager;
  52.     }
  53.     public function __invoke(Request $request): Response
  54.     {
  55.         if ($this->isAuthenticated()) {
  56.             $request->getSession()->getFlashBag()->add(
  57.                 'sonata_user_error',
  58.                 $this->translator->trans('sonata_user_already_authenticated', [], 'SonataUserBundle')
  59.             );
  60.             return new RedirectResponse($this->urlGenerator->generate('sonata_admin_dashboard'));
  61.         }
  62.         $csrfToken null;
  63.         if (null !== $this->csrfTokenManager) {
  64.             $csrfToken $this->csrfTokenManager->getToken('authenticate')->getValue();
  65.         }
  66.         return new Response($this->twig->render('@SonataUser/Admin/Security/login.html.twig', [
  67.             'admin_pool' => $this->adminPool,
  68.             'base_template' => $this->templateRegistry->getTemplate('layout'),
  69.             'csrf_token' => $csrfToken,
  70.             'error' => $this->authenticationUtils->getLastAuthenticationError(),
  71.             'last_username' => $this->authenticationUtils->getLastUsername(),
  72.             'reset_route' => $this->urlGenerator->generate('sonata_user_admin_resetting_request'),
  73.         ]));
  74.     }
  75.     private function isAuthenticated(): bool
  76.     {
  77.         $token $this->tokenStorage->getToken();
  78.         if (null === $token) {
  79.             return false;
  80.         }
  81.         $user $token->getUser();
  82.         return $user instanceof UserInterface;
  83.     }
  84. }