-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensioSecurityExpressionVoter.php
More file actions
147 lines (129 loc) · 4.81 KB
/
Copy pathSensioSecurityExpressionVoter.php
File metadata and controls
147 lines (129 loc) · 4.81 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
*
* (c) Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Yarhon\RouteGuardBundle\Security\Authorization;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Yarhon\RouteGuardBundle\ExpressionLanguage\ExpressionDecorator;
/**
* Sensio FrameworkExtraBundle executes security expressions in place, bypassing the traditional flow to
* \Symfony\Component\Security\Core\Authorization\AuthorizationChecker and further to the Voter(s).
* SensioSecurityExpressionVoter is intended to execute those security expressions using traditional flow.
*
* @see \Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener
*
* @author Yaroslav Honcharuk <yaroslav.xs@gmail.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
class SensioSecurityExpressionVoter extends Voter
{
protected static $variableNames = [
'token',
'user',
'object',
'subject',
'roles',
'trust_resolver',
'auth_checker',
'request',
];
/**
* @var ExpressionLanguage
*/
protected $expressionLanguage;
/**
* @var AuthenticationTrustResolverInterface
*/
protected $trustResolver;
/**
* @var AuthorizationCheckerInterface
*/
protected $authChecker;
/**
* @var RoleHierarchyInterface|null
*/
protected $roleHierarchy;
/**
* @param ExpressionLanguage $expressionLanguage
* @param AuthenticationTrustResolverInterface $trustResolver
* @param AuthorizationCheckerInterface $authChecker
* @param RoleHierarchyInterface|null $roleHierarchy
*/
public function __construct(ExpressionLanguage $expressionLanguage, AuthenticationTrustResolverInterface $trustResolver, AuthorizationCheckerInterface $authChecker, RoleHierarchyInterface $roleHierarchy = null)
{
$this->expressionLanguage = $expressionLanguage;
$this->trustResolver = $trustResolver;
$this->authChecker = $authChecker;
$this->roleHierarchy = $roleHierarchy;
}
/**
* @return array
*/
public static function getVariableNames()
{
return self::$variableNames;
}
/**
* {@inheritdoc}
*/
protected function supports($attribute, $subject)
{
return $attribute instanceof ExpressionDecorator;
}
/**
* {@inheritdoc}
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var ExpressionDecorator $attribute */
$expressionVariables = $attribute->getVariables();
$variables = $this->getVariables($token, $subject);
// In case of overlap, built-in variables win.
$variables = array_merge($expressionVariables, $variables);
return $this->expressionLanguage->evaluate($attribute->getExpression(), $variables);
}
/**
* @codeCoverageIgnore
*
* @param TokenInterface $token
* @param mixed $subject
*
* @return array
*/
protected function getVariables(TokenInterface $token, $subject)
{
if (null !== $this->roleHierarchy) {
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
} else {
$roles = $token->getRoles();
}
$variables = [
'token' => $token,
'user' => $token->getUser(),
'object' => $subject,
'subject' => $subject,
'roles' => array_map(function ($role) { return $role->getRole(); }, $roles),
'trust_resolver' => $this->trustResolver,
// "auth_checker" variable is used by Sensio security expressions (see \Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::getVariables),
// and would be available by default since Symfony 4.2 (see \Symfony\Component\Security\Core\Authorization\Voter\SymfonySecurityExpressionVoter::getVariables).
'auth_checker' => $this->authChecker,
];
// this is mainly to propose a better experience when the expression is used
// in an access control rule, as the developer does not know that it's going
// to be handled by this voter
if ($subject instanceof Request) {
$variables['request'] = $subject;
}
return $variables;
}
}