vendor/ezsystems/ezplatform-page-builder/src/lib/Security/EditorialMode/TokenAuthorizedRouter.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace EzSystems\EzPlatformPageBuilder\Security\EditorialMode;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Cmf\Component\Routing\ChainRouter as BaseChainRouter;
  10. use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. /**
  14.  * Router responsible for decorate routes with editorial mode token.
  15.  */
  16. class TokenAuthorizedRouter extends BaseChainRouter implements VersatileGeneratorInterface
  17. {
  18.     /** @var \EzSystems\EzPlatformPageBuilder\Security\EditorialMode\TokenManager */
  19.     private $jwtTokenManager;
  20.     /** @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface */
  21.     private $tokenStorage;
  22.     /** @var \Symfony\Component\Routing\RouterInterface */
  23.     private $router;
  24.     /** @var \EzSystems\EzPlatformPageBuilder\Security\EditorialMode\TokenAuthenticator|null */
  25.     private $authenticator;
  26.     /** @var array */
  27.     private $routesMap;
  28.     /**
  29.      * @param \Symfony\Component\Routing\RouterInterface $router
  30.      * @param \EzSystems\EzPlatformPageBuilder\Security\EditorialMode\TokenManager $jwtTokenManager
  31.      * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokenStorage
  32.      * @param \EzSystems\EzPlatformPageBuilder\Security\EditorialMode\TokenAuthenticator|null $authenticator
  33.      * @param array $routesMap
  34.      * @param \Psr\Log\LoggerInterface|null $logger
  35.      */
  36.     public function __construct(
  37.         RouterInterface $router,
  38.         ?TokenManager $jwtTokenManager,
  39.         TokenStorageInterface $tokenStorage,
  40.         ?TokenAuthenticator $authenticator,
  41.         array $routesMap,
  42.         LoggerInterface $logger null
  43.     ) {
  44.         parent::__construct($logger);
  45.         $this->router $router;
  46.         $this->jwtTokenManager $jwtTokenManager;
  47.         $this->tokenStorage $tokenStorage;
  48.         $this->authenticator $authenticator;
  49.         $this->routesMap $routesMap;
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH): string
  55.     {
  56.         if ($this->authenticator !== null && $this->authenticator->isEnabled()) {
  57.             $token $this->tokenStorage->getToken();
  58.             if ($token !== null) {
  59.                 $parameters[$this->authenticator->getTokenQueryParamName()] = $this->jwtTokenManager->create(
  60.                     $token->getUser()
  61.                 );
  62.             }
  63.         }
  64.         return $this->router->generate($this->routesMap[$name], $parameters$referenceType);
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function supports($name): bool
  70.     {
  71.         if (is_string($name)) {
  72.             return isset($this->routesMap[$name]);
  73.         }
  74.         return false;
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function getRouteDebugMessage($name, array $parameters = []): string
  80.     {
  81.         return "Route '$name' not found";
  82.     }
  83. }