Issues (11)

Controller/ExportJsSecuredRoutesController.php (1 issue)

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 3
    public function __construct(AccessControl $accessControl, TokenStorageInterface $tokenStorage, Environment $twig, string $cacheDir)
36
    {
37 3
        $this->accessControl = $accessControl;
38 3
        $this->tokenStorage = $tokenStorage;
39 3
        $this->twig = $twig;
40 3
        $this->cacheDir = $cacheDir;
41 3
    }
42
43
    /**
44
     * @return Response
45
     * @throws \Psr\Cache\InvalidArgumentException
46
     */
47 3
    public function exportAction()
48
    {
49 3
        if (null === $this->tokenStorage->getToken()) {
50 1
            throw new LogicException('Unable to retrive the current user. The token storage does not contain security token.');
51
        }
52
53 2
        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 2
        $user = $this->tokenStorage->getToken()->getUser();
58
59 2
        $cacheKey = md5($user->getUsername().json_encode($user->getRoles()));
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Securi...nterface::getUsername() has been deprecated: since Symfony 5.3, use getUserIdentifier() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

59
        $cacheKey = md5(/** @scrutinizer ignore-deprecated */ $user->getUsername().json_encode($user->getRoles()));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60
61 2
        $cache = new FilesystemAdapter('sil_route_security_bundle', 0, $this->cacheDir);
62
63 2
        $securedRoutesWithUserPermission = $cache->get($cacheKey, function (ItemInterface $item) use ($user){
64 2
            $item->expiresAfter(3600);
65
66 2
            $securedRoutesWithUserPermission = [];
67 2
            foreach ($this->accessControl->getAllSecuredRoutes() as $route) {
68 2
                $securedRoutesWithUserPermission[$route] = $this->accessControl->hasUserAccessToRoute($user, $route);
69
            }
70
71 2
            return $securedRoutesWithUserPermission;
72 2
        });
73
74 2
        return new Response($this->twig->render(
75 2
            '@SilRouteSecurity/secured_routes.js.twig',
76 2
            ['securedRoutes' => $securedRoutesWithUserPermission]
77 2
        ), 200, ['Content-Type' => 'application/javascript']);
78
    }
79
}
80