vendor/contao/core-bundle/src/Security/Authentication/Token/TokenChecker.php line 139

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Security\Authentication\Token;
  11. use Contao\BackendUser;
  12. use Contao\FrontendUser;
  13. use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
  14. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  21. use Symfony\Component\Security\Http\FirewallMapInterface;
  22. class TokenChecker
  23. {
  24.     private const FRONTEND_FIREWALL 'contao_frontend';
  25.     private const BACKEND_FIREWALL 'contao_backend';
  26.     /**
  27.      * @var RequestStack
  28.      */
  29.     private $requestStack;
  30.     /**
  31.      * @var FirewallMapInterface
  32.      */
  33.     private $firewallMap;
  34.     /**
  35.      * @var TokenStorageInterface
  36.      */
  37.     private $tokenStorage;
  38.     /**
  39.      * @var SessionInterface
  40.      */
  41.     private $session;
  42.     /**
  43.      * @var AuthenticationTrustResolverInterface
  44.      */
  45.     private $trustResolver;
  46.     /**
  47.      * @var VoterInterface
  48.      */
  49.     private $roleVoter;
  50.     /**
  51.      * @internal Do not inherit from this class; decorate the "contao.security.token_checker" service instead
  52.      */
  53.     public function __construct(RequestStack $requestStackFirewallMapInterface $firewallMapTokenStorageInterface $tokenStorageSessionInterface $sessionAuthenticationTrustResolverInterface $trustResolverVoterInterface $roleVoter)
  54.     {
  55.         $this->requestStack $requestStack;
  56.         $this->firewallMap $firewallMap;
  57.         $this->tokenStorage $tokenStorage;
  58.         $this->session $session;
  59.         $this->trustResolver $trustResolver;
  60.         $this->roleVoter $roleVoter;
  61.     }
  62.     /**
  63.      * Checks if a front end user is authenticated.
  64.      */
  65.     public function hasFrontendUser(): bool
  66.     {
  67.         $token $this->getToken(self::FRONTEND_FIREWALL);
  68.         return null !== $token && VoterInterface::ACCESS_GRANTED === $this->roleVoter->vote($tokennull, ['ROLE_MEMBER']);
  69.     }
  70.     /**
  71.      * Checks if a back end user is authenticated.
  72.      */
  73.     public function hasBackendUser(): bool
  74.     {
  75.         $token $this->getToken(self::BACKEND_FIREWALL);
  76.         return null !== $token && VoterInterface::ACCESS_GRANTED === $this->roleVoter->vote($tokennull, ['ROLE_USER']);
  77.     }
  78.     /**
  79.      * Gets the front end username from the session.
  80.      */
  81.     public function getFrontendUsername(): ?string
  82.     {
  83.         $token $this->getToken(self::FRONTEND_FIREWALL);
  84.         if (null === $token || !$token->getUser() instanceof FrontendUser) {
  85.             return null;
  86.         }
  87.         return $token->getUser()->getUsername();
  88.     }
  89.     /**
  90.      * Gets the back end username from the session.
  91.      */
  92.     public function getBackendUsername(): ?string
  93.     {
  94.         $token $this->getToken(self::BACKEND_FIREWALL);
  95.         if (null === $token || !$token->getUser() instanceof BackendUser) {
  96.             return null;
  97.         }
  98.         return $token->getUser()->getUsername();
  99.     }
  100.     /**
  101.      * Tells whether the front end preview can show unpublished fragments.
  102.      */
  103.     public function isPreviewMode(): bool
  104.     {
  105.         $request $this->requestStack->getMasterRequest();
  106.         if (null === $request || !$request->attributes->get('_preview'false)) {
  107.             return false;
  108.         }
  109.         $token $this->getToken(self::FRONTEND_FIREWALL);
  110.         return $token instanceof FrontendPreviewToken && $token->showUnpublished();
  111.     }
  112.     private function getToken(string $context): ?TokenInterface
  113.     {
  114.         $token $this->getTokenFromStorage($context);
  115.         if (null === $token) {
  116.             $token $this->getTokenFromSession('_security_'.$context);
  117.         }
  118.         if (!$token instanceof TokenInterface || !$token->isAuthenticated()) {
  119.             return null;
  120.         }
  121.         if ($this->trustResolver->isAnonymous($token)) {
  122.             return null;
  123.         }
  124.         return $token;
  125.     }
  126.     private function getTokenFromStorage(string $context): ?TokenInterface
  127.     {
  128.         $request $this->requestStack->getMasterRequest();
  129.         if (!$this->firewallMap instanceof FirewallMap || null === $request) {
  130.             return null;
  131.         }
  132.         $config $this->firewallMap->getFirewallConfig($request);
  133.         if (!$config instanceof FirewallConfig || $config->getContext() !== $context) {
  134.             return null;
  135.         }
  136.         return $this->tokenStorage->getToken();
  137.     }
  138.     private function getTokenFromSession(string $sessionKey): ?TokenInterface
  139.     {
  140.         if (!$this->session->isStarted()) {
  141.             $request $this->requestStack->getMasterRequest();
  142.             if (!$request || !$request->hasPreviousSession()) {
  143.                 return null;
  144.             }
  145.         }
  146.         // This will start the session if Request::hasPreviousSession() was true
  147.         if (!$this->session->has($sessionKey)) {
  148.             return null;
  149.         }
  150.         $token unserialize($this->session->get($sessionKey), ['allowed_classes' => true]);
  151.         if (!$token instanceof TokenInterface) {
  152.             return null;
  153.         }
  154.         return $token;
  155.     }
  156. }