1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCacheBundle\UserContext; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\UserContext\ContextProvider; |
15
|
|
|
use FOS\HttpCache\UserContext\UserContext; |
16
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
18
|
|
|
use Symfony\Component\Security\Core\Role\RoleInterface; |
19
|
|
|
use Symfony\Component\Security\Core\SecurityContextInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The RoleProvider adds roles to the UserContext for the hash generation. |
23
|
|
|
*/ |
24
|
|
|
class RoleProvider implements ContextProvider |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var SecurityContextInterface|null |
28
|
|
|
*/ |
29
|
|
|
private $context; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create the role provider with a security context. |
33
|
|
|
* |
34
|
|
|
* The security context is optional to not fail on routes that have no |
35
|
|
|
* firewall. It is however not valid to call updateUserContext when not in |
36
|
|
|
* a firewall context. |
37
|
|
|
* |
38
|
|
|
* @param SecurityContextInterface|TokenStorageInterface $context |
|
|
|
|
39
|
|
|
*/ |
40
|
18 |
|
public function __construct($context = null) |
41
|
|
|
{ |
42
|
18 |
|
if ($context |
43
|
18 |
|
&& !$context instanceof SecurityContextInterface |
|
|
|
|
44
|
18 |
|
&& !$context instanceof TokenStorageInterface |
45
|
|
|
) { |
46
|
|
|
throw new \InvalidArgumentException( |
47
|
|
|
'Context must implement either TokenStorageInterface or SecurityContextInterface' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
18 |
|
$this->context = $context; |
|
|
|
|
52
|
18 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
* |
57
|
|
|
* @throws InvalidConfigurationException when called without a security context being set |
58
|
|
|
*/ |
59
|
3 |
|
public function updateUserContext(UserContext $context) |
60
|
|
|
{ |
61
|
3 |
|
if (null === $this->context) { |
62
|
1 |
|
throw new InvalidConfigurationException('The context hash URL must be under a firewall.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
if (null === $token = $this->context->getToken()) { |
66
|
1 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$roles = array_map(function (RoleInterface $role) { |
70
|
1 |
|
return $role->getRole(); |
71
|
1 |
|
}, $token->getRoles()); |
72
|
|
|
|
73
|
|
|
// Order is not important for roles and should not change hash. |
74
|
1 |
|
sort($roles); |
75
|
|
|
|
76
|
1 |
|
$context->addParameter('roles', $roles); |
77
|
1 |
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.