1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Security; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
17
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
|
|
|
18
|
|
|
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; |
19
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
22
|
|
|
use Symfony\Component\Security\Core\Role\Role; |
23
|
|
|
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Checks if the logged user has sufficient permissions to access the given resource. |
27
|
|
|
* |
28
|
|
|
* @author Kévin Dunglas <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class ResourceAccessChecker implements ResourceAccessCheckerInterface |
31
|
|
|
{ |
32
|
|
|
use ClassInfoTrait; |
33
|
|
|
|
34
|
|
|
private $expressionLanguage; |
35
|
|
|
private $authenticationTrustResolver; |
36
|
|
|
private $roleHierarchy; |
37
|
|
|
private $tokenStorage; |
38
|
|
|
private $authorizationChecker; |
39
|
|
|
|
40
|
|
|
public function __construct(ExpressionLanguage $expressionLanguage = null, AuthenticationTrustResolverInterface $authenticationTrustResolver = null, RoleHierarchyInterface $roleHierarchy = null, TokenStorageInterface $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null) |
41
|
|
|
{ |
42
|
|
|
$this->expressionLanguage = $expressionLanguage; |
43
|
|
|
$this->authenticationTrustResolver = $authenticationTrustResolver; |
44
|
|
|
$this->roleHierarchy = $roleHierarchy; |
45
|
|
|
$this->tokenStorage = $tokenStorage; |
46
|
|
|
$this->authorizationChecker = $authorizationChecker; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isGranted(string $resourceClass, string $expression, array $extraVariables = []): bool |
50
|
|
|
{ |
51
|
|
|
if (null === $this->tokenStorage || null === $this->authenticationTrustResolver) { |
52
|
|
|
throw new \LogicException('The "symfony/security" library must be installed to use the "access_control" attribute.'); |
53
|
|
|
} |
54
|
|
|
if (null === $token = $this->tokenStorage->getToken()) { |
55
|
|
|
throw new \LogicException('The current token must be set to use the "access_control" attribute (is the URL behind a firewall?).'); |
56
|
|
|
} |
57
|
|
|
if (null === $this->expressionLanguage) { |
58
|
|
|
throw new \LogicException('The "symfony/expression-language" library must be installed to use the "access_control".'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return (bool) $this->expressionLanguage->evaluate($expression, array_merge($extraVariables, $this->getVariables($token))); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @copyright Fabien Potencier <[email protected]> |
66
|
|
|
* |
67
|
|
|
* @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Authorization/Voter/ExpressionVoter.php |
68
|
|
|
*/ |
69
|
|
|
private function getVariables(TokenInterface $token): array |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
'token' => $token, |
73
|
|
|
'user' => $token->getUser(), |
74
|
|
|
'roles' => $this->getEffectiveRoles($token), |
75
|
|
|
'trust_resolver' => $this->authenticationTrustResolver, |
76
|
|
|
// needed for the is_granted expression function |
77
|
|
|
'auth_checker' => $this->authorizationChecker, |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string[] |
83
|
|
|
*/ |
84
|
|
|
private function getEffectiveRoles(TokenInterface $token): array |
85
|
|
|
{ |
86
|
|
|
if (null === $this->roleHierarchy) { |
87
|
|
|
return method_exists($token, 'getRoleNames') ? $token->getRoleNames() : array_map('strval', $token->getRoles()); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) { |
91
|
|
|
return $this->roleHierarchy->getReachableRoleNames($token->getRoleNames()); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return array_map(function (Role $role): string { |
95
|
|
|
return $role->getRole(); |
96
|
|
|
}, $this->roleHierarchy->getReachableRoles($token->getRoles())); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: