/
RoleProvider.php
60 lines (51 loc) · 1.78 KB
/
RoleProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCacheBundle\UserContext;
use FOS\HttpCache\UserContext\ContextProvider;
use FOS\HttpCache\UserContext\UserContext;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* The RoleProvider adds roles to the UserContext for the hash generation.
*/
class RoleProvider implements ContextProvider
{
/**
* Create the role provider with a security context.
*
* The token storage is optional to not fail on routes that have no
* firewall. It is however not valid to call updateUserContext when not in
* a firewall context.
*/
public function __construct(
private ?TokenStorageInterface $tokenStorage = null
) {
}
/**
* {@inheritdoc}
*
* @throws InvalidConfigurationException when called without a security context being set
*/
public function updateUserContext(UserContext $context): void
{
if (null === $this->tokenStorage) {
throw new InvalidConfigurationException('The context hash URL must be under a firewall.');
}
$token = $this->tokenStorage->getToken();
if (null === $token) {
$token = new NullToken();
}
$roles = $token->getRoleNames();
// Order is not important for roles and should not change hash.
sort($roles);
$context->addParameter('roles', $roles);
}
}