|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Sil\RouteSecurityBundle\Controller; |
| 5 | + |
| 6 | +use Sil\RouteSecurityBundle\Exception\LogicException; |
| 7 | +use Sil\RouteSecurityBundle\Security\AccessControl; |
| 8 | +use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
| 10 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 11 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 12 | +use Symfony\Contracts\Cache\ItemInterface; |
| 13 | +use Twig\Environment; |
| 14 | + |
| 15 | +class ExportJsSecuredRoutesController |
| 16 | +{ |
| 17 | + /** @var AccessControl */ |
| 18 | + private $accessControl; |
| 19 | + |
| 20 | + /** @var TokenStorageInterface */ |
| 21 | + private $tokenStorage; |
| 22 | + |
| 23 | + /** @var Environment */ |
| 24 | + private $twig; |
| 25 | + |
| 26 | + /** @var string */ |
| 27 | + private $cacheDir; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param AccessControl $accessControl |
| 31 | + * @param TokenStorageInterface $tokenStorage |
| 32 | + * @param Environment $twig |
| 33 | + * @param string $cacheDir |
| 34 | + */ |
| 35 | + public function __construct(AccessControl $accessControl, TokenStorageInterface $tokenStorage, Environment $twig, string $cacheDir) |
| 36 | + { |
| 37 | + $this->accessControl = $accessControl; |
| 38 | + $this->tokenStorage = $tokenStorage; |
| 39 | + $this->twig = $twig; |
| 40 | + $this->cacheDir = $cacheDir; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @return Response |
| 45 | + * @throws \Psr\Cache\InvalidArgumentException |
| 46 | + */ |
| 47 | + public function exportAction() |
| 48 | + { |
| 49 | + if (null === $this->tokenStorage->getToken()) { |
| 50 | + throw new LogicException('Unable to retrive the current user. The token storage does not contain security token.'); |
| 51 | + } |
| 52 | + |
| 53 | + if (false === $this->tokenStorage->getToken()->getUser() instanceof UserInterface) { |
| 54 | + throw new LogicException(sprintf('The security token must containt an User object that implements %s', UserInterface::class)); |
| 55 | + } |
| 56 | + |
| 57 | + $user = $this->tokenStorage->getToken()->getUser(); |
| 58 | + |
| 59 | + $cacheKey = md5($user->getUsername().json_encode($user->getRoles())); |
| 60 | + |
| 61 | + $cache = new FilesystemAdapter('sil_route_security_bundle', 0, $this->cacheDir); |
| 62 | + |
| 63 | + $securedRoutesWithUserPermission = $cache->get($cacheKey, function (ItemInterface $item) use ($user){ |
| 64 | + $item->expiresAfter(3600); |
| 65 | + |
| 66 | + $securedRoutesWithUserPermission = []; |
| 67 | + foreach ($this->accessControl->getAllSecuredRoutes() as $route) { |
| 68 | + $securedRoutesWithUserPermission[$route] = $this->accessControl->hasUserAccessToRoute($user, $route); |
| 69 | + } |
| 70 | + |
| 71 | + return $securedRoutesWithUserPermission; |
| 72 | + }); |
| 73 | + |
| 74 | + return new Response($this->twig->render( |
| 75 | + '@SilRouteSecurity/secured_routes.js.twig', |
| 76 | + ['securedRoutes' => $securedRoutesWithUserPermission] |
| 77 | + ), 200, ['Content-Type' => 'application/javascript']); |
| 78 | + } |
| 79 | +} |
0 commit comments