-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Updating AbstractVoter so that the method receives the TokenInterface #15870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,12 @@ public function vote(TokenInterface $token, $object, array $attributes) | |
// abstain vote by default in case none of the attributes are supported | ||
$vote = self::ACCESS_ABSTAIN; | ||
|
||
$reflector = new \ReflectionMethod($this, 'voteOnAttribute'); | ||
$isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter'; | ||
if (!$isNewOverwritten) { | ||
@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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should mention the class of |
||
} | ||
|
||
foreach ($attributes as $attribute) { | ||
if (!$this->supportsAttribute($attribute)) { | ||
continue; | ||
|
@@ -73,9 +79,16 @@ public function vote(TokenInterface $token, $object, array $attributes) | |
// as soon as at least one attribute is supported, default is to deny access | ||
$vote = self::ACCESS_DENIED; | ||
|
||
if ($this->isGranted($attribute, $object, $token->getUser())) { | ||
// grant access as soon as at least one voter returns a positive response | ||
return self::ACCESS_GRANTED; | ||
if ($isNewOverwritten) { | ||
if ($this->voteOnAttribute($attribute, $object, $token)) { | ||
// grant access as soon as at least one voter returns a positive response | ||
return self::ACCESS_GRANTED; | ||
} | ||
} else { | ||
if ($this->isGranted($attribute, $object, $token->getUser())) { | ||
// grant access as soon as at least one voter returns a positive response | ||
return self::ACCESS_GRANTED; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could actually become an |
||
} | ||
} | ||
|
||
|
@@ -107,7 +120,32 @@ abstract protected function getSupportedAttributes(); | |
* @param object $object | ||
* @param UserInterface|string $user | ||
* | ||
* @deprecated This method will be removed in 3.0 - override voteOnAttribute instead. | ||
* | ||
* @return bool | ||
*/ | ||
abstract protected function isGranted($attribute, $object, $user = null); | ||
protected function isGranted($attribute, $object, $user = null) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method should trigger a deprecation warning when being called, in case someone implements |
||
return false; | ||
} | ||
|
||
/** | ||
* Perform a single access check operation on a given attribute, object and (optionally) user | ||
* It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass | ||
* $user can be one of the following: | ||
* a UserInterface object (fully authenticated user) | ||
* a string (anonymously authenticated user). | ||
* | ||
* This method will become abstract in 3.0. | ||
* | ||
* @param string $attribute | ||
* @param object $object | ||
* @param TokenInterface $token | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a note that this method should be abstract in 3.0 so that we do not forget that when cleaning the master branch? |
||
* @return bool | ||
*/ | ||
protected function voteOnAttribute($attribute, $object, TokenInterface $token) | ||
{ | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the default implementation should rather throw a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, this could even be simplified a lot, avoiding the Reflection-based logic:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was sure there was a simpler way, and this looks great! See #15921 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use
__CLASS__
here rather than the class name in a string. It makes it clearer than it is the current class, and is less likely to break if we rename things