Skip to content

Commit 71d2c38

Browse files
committed
[Security] optimized ExpressionVoter
1 parent 539d58d commit 71d2c38

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

Authorization/Voter/ExpressionVoter.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ public function supportsClass($class)
6161
* {@inheritdoc}
6262
*/
6363
public function vote(TokenInterface $token, $object, array $attributes)
64+
{
65+
$result = VoterInterface::ACCESS_ABSTAIN;
66+
$variables = null;
67+
foreach ($attributes as $attribute) {
68+
if (!$this->supportsAttribute($attribute)) {
69+
continue;
70+
}
71+
72+
if (null === $variables) {
73+
$variables = $this->getVariables($token, $object);
74+
}
75+
76+
$result = VoterInterface::ACCESS_DENIED;
77+
if ($this->expressionLanguage->evaluate($attribute, $variables)) {
78+
return VoterInterface::ACCESS_GRANTED;
79+
}
80+
}
81+
82+
return $result;
83+
}
84+
85+
private function getVariables(TokenInterface $token, $object)
6486
{
6587
if (null !== $this->roleHierarchy) {
6688
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
@@ -83,18 +105,6 @@ public function vote(TokenInterface $token, $object, array $attributes)
83105
$variables['request'] = $object;
84106
}
85107

86-
$result = VoterInterface::ACCESS_ABSTAIN;
87-
foreach ($attributes as $attribute) {
88-
if (!$this->supportsAttribute($attribute)) {
89-
continue;
90-
}
91-
92-
$result = VoterInterface::ACCESS_DENIED;
93-
if ($this->expressionLanguage->evaluate($attribute, $variables)) {
94-
return VoterInterface::ACCESS_GRANTED;
95-
}
96-
}
97-
98-
return $result;
108+
return $variables;
99109
}
100110
}

0 commit comments

Comments
 (0)