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

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 Symfony\Component\Translation\TranslatorInterface;
  26. use Twig\Environment;
  27. final class LoginAction
  28. {
  29.     /**
  30.      * @var Environment
  31.      */
  32.     private $twig;
  33.     /**
  34.      * @var UrlGeneratorInterface
  35.      */
  36.     private $urlGenerator;
  37.     /**
  38.      * @var AuthorizationCheckerInterface
  39.      */
  40.     private $authorizationChecker;
  41.     /**
  42.      * @var Pool
  43.      */
  44.     private $adminPool;
  45.     /**
  46.      * @var TemplateRegistryInterface
  47.      */
  48.     private $templateRegistry;
  49.     /**
  50.      * @var TokenStorageInterface
  51.      */
  52.     private $tokenStorage;
  53.     /**
  54.      * @var Session
  55.      */
  56.     private $session;
  57.     /**
  58.      * @var TranslatorInterface
  59.      */
  60.     private $translator;
  61.     /**
  62.      * @var CsrfTokenManagerInterface
  63.      */
  64.     private $csrfTokenManager;
  65.     // NEXT_MAJOR: Make $translator argument mandatory.
  66.     public function __construct(
  67.         Environment $twig,
  68.         UrlGeneratorInterface $urlGenerator,
  69.         AuthorizationCheckerInterface $authorizationChecker,
  70.         Pool $adminPool,
  71.         TemplateRegistryInterface $templateRegistry,
  72.         TokenStorageInterface $tokenStorage,
  73.         Session $session,
  74.         ?TranslatorInterface $translator null
  75.     ) {
  76.         $this->twig $twig;
  77.         $this->urlGenerator $urlGenerator;
  78.         $this->authorizationChecker $authorizationChecker;
  79.         $this->adminPool $adminPool;
  80.         $this->templateRegistry $templateRegistry;
  81.         $this->tokenStorage $tokenStorage;
  82.         $this->session $session;
  83.         // NEXT_MAJOR: Remove this block.
  84.         if (null === $translator) {
  85.             @trigger_error(sprintf(
  86.                 'Not passing an instance of "%s" as argument 6 to "%s()" is deprecated since'
  87.                 .' sonata-project/user-bundle 4.10 and will be not possible in version 5.0.',
  88.                 TranslatorInterface::class,
  89.                 __METHOD__
  90.             ), \E_USER_DEPRECATED);
  91.             $translator = new IdentityTranslator();
  92.         }
  93.         $this->translator $translator;
  94.     }
  95.     public function __invoke(Request $request): Response
  96.     {
  97.         if ($this->isAuthenticated()) {
  98.             $this->session->getFlashBag()->add(
  99.                 'sonata_user_error',
  100.                 $this->translator->trans('sonata_user_already_authenticated', [], 'SonataUserBundle')
  101.             );
  102.             return new RedirectResponse($this->urlGenerator->generate('sonata_admin_dashboard'));
  103.         }
  104.         $session $request->getSession();
  105.         $authErrorKey Security::AUTHENTICATION_ERROR;
  106.         // get the error if any (works with forward and redirect -- see below)
  107.         if ($request->attributes->has($authErrorKey)) {
  108.             $error $request->attributes->get($authErrorKey);
  109.         } elseif (null !== $session && $session->has($authErrorKey)) {
  110.             $error $session->get($authErrorKey);
  111.             $session->remove($authErrorKey);
  112.         } else {
  113.             $error null;
  114.         }
  115.         if (!$error instanceof AuthenticationException) {
  116.             $error null// The value does not come from the security component.
  117.         }
  118.         if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  119.             $refererUri $request->server->get('HTTP_REFERER');
  120.             $url $refererUri && $refererUri !== $request->getUri() ? $refererUri $this->urlGenerator->generate('sonata_admin_dashboard');
  121.             return new RedirectResponse($url);
  122.         }
  123.         $csrfToken null;
  124.         if ($this->csrfTokenManager) {
  125.             $csrfToken $this->csrfTokenManager->getToken('authenticate')->getValue();
  126.         }
  127.         return new Response($this->twig->render('@SonataUser/Admin/Security/login.html.twig', [
  128.             'admin_pool' => $this->adminPool,
  129.             'base_template' => $this->templateRegistry->getTemplate('layout'),
  130.             'csrf_token' => $csrfToken,
  131.             'error' => $error,
  132.             'last_username' => (null === $session) ? '' $session->get(Security::LAST_USERNAME),
  133.             'reset_route' => $this->urlGenerator->generate('sonata_user_admin_resetting_request'),
  134.         ]));
  135.     }
  136.     public function setCsrfTokenManager(CsrfTokenManagerInterface $csrfTokenManager): void
  137.     {
  138.         $this->csrfTokenManager $csrfTokenManager;
  139.     }
  140.     private function isAuthenticated(): bool
  141.     {
  142.         $token $this->tokenStorage->getToken();
  143.         if (!$token) {
  144.             return false;
  145.         }
  146.         $user $token->getUser();
  147.         return $user instanceof UserInterface;
  148.     }
  149. }