Skip to content

Commit a76dd01

Browse files
weaverryanfabpot
authored andcommitted
Updating AbstractVoter so that the method receives the TokenInterface
1 parent 4a33924 commit a76dd01

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

Authorization/Voter/AbstractVoter.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public function vote(TokenInterface $token, $object, array $attributes)
6565
// abstain vote by default in case none of the attributes are supported
6666
$vote = self::ACCESS_ABSTAIN;
6767

68+
$reflector = new \ReflectionMethod($this, 'voteOnAttribute');
69+
$isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter';
70+
if (!$isNewOverwritten) {
71+
@trigger_error(sprintf("The AbstractVoter::isGranted method is deprecated since 2.8 and won't be called anymore in 3.0. Override voteOnAttribute() instead.", $reflector->class), E_USER_DEPRECATED);
72+
}
73+
6874
foreach ($attributes as $attribute) {
6975
if (!$this->supportsAttribute($attribute)) {
7076
continue;
@@ -73,9 +79,16 @@ public function vote(TokenInterface $token, $object, array $attributes)
7379
// as soon as at least one attribute is supported, default is to deny access
7480
$vote = self::ACCESS_DENIED;
7581

76-
if ($this->isGranted($attribute, $object, $token->getUser())) {
77-
// grant access as soon as at least one voter returns a positive response
78-
return self::ACCESS_GRANTED;
82+
if ($isNewOverwritten) {
83+
if ($this->voteOnAttribute($attribute, $object, $token)) {
84+
// grant access as soon as at least one voter returns a positive response
85+
return self::ACCESS_GRANTED;
86+
}
87+
} else {
88+
if ($this->isGranted($attribute, $object, $token->getUser())) {
89+
// grant access as soon as at least one voter returns a positive response
90+
return self::ACCESS_GRANTED;
91+
}
7992
}
8093
}
8194

@@ -107,7 +120,32 @@ abstract protected function getSupportedAttributes();
107120
* @param object $object
108121
* @param UserInterface|string $user
109122
*
123+
* @deprecated This method will be removed in 3.0 - override voteOnAttribute instead.
124+
*
110125
* @return bool
111126
*/
112-
abstract protected function isGranted($attribute, $object, $user = null);
127+
protected function isGranted($attribute, $object, $user = null)
128+
{
129+
return false;
130+
}
131+
132+
/**
133+
* Perform a single access check operation on a given attribute, object and (optionally) user
134+
* It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass
135+
* $user can be one of the following:
136+
* a UserInterface object (fully authenticated user)
137+
* a string (anonymously authenticated user).
138+
*
139+
* This method will become abstract in 3.0.
140+
*
141+
* @param string $attribute
142+
* @param object $object
143+
* @param TokenInterface $token
144+
*
145+
* @return bool
146+
*/
147+
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
148+
{
149+
return false;
150+
}
113151
}

0 commit comments

Comments
 (0)