Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

weaverryan
Copy link
Member

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #12360
License MIT
Doc PR not yet

This fixes #12360, and along with already-merged #14733, this would make it possible to make calls back to the AccessDecisionManager inside a voter (e.g. you might check to see if IS_AUTHENTICATED_FULLY from inside your voter).

We originally passed the User instead of the token to be nice, but it's a limitation, and since we never sanitized the User (i.e. a string may be passed to AbstractToken::isGranted()), it's not helpful anyways.

Thanks!

This fixes issues where people need this token.
if ($this->isGranted($attribute, $object, $token->getUser())) {
// grant access as soon as at least one voter returns a positive response
return self::ACCESS_GRANTED;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could actually become an elseif { ... }

@linaori
Copy link
Contributor

linaori commented Sep 23, 2015

I think this is a fair compromise for the feature. The name voteOnAttribute also makes a lot more sense than isGranted, as this would imply that the voter grants access, while it's only one of many possible voters.

* @param string $attribute
* @param object $object
* @param TokenInterface $token
*
Copy link
Member

Choose a reason for hiding this comment

The 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?

@fabpot
Copy link
Member

fabpot commented Sep 23, 2015

👍

@weaverryan
Copy link
Member Author

Done!

Status: Reviewed

@@ -9,8 +9,9 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Tests\Core\Authentication\Voter;
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also realized the test was in the wrong spot - it wasn't moved when the other tests were moved. Fixed that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind, fixing in a proper PR in 2.7

@fabpot
Copy link
Member

fabpot commented Sep 24, 2015

Thank you @weaverryan.

@fabpot fabpot closed this Sep 24, 2015
fabpot added a commit that referenced this pull request Sep 24, 2015
… TokenInterface (weaverryan)

This PR was squashed before being merged into the 2.8 branch (closes #15870).

Discussion
----------

Updating AbstractVoter so that the method receives the TokenInterface

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #12360
| License       | MIT
| Doc PR        | not yet

This fixes #12360, and along with already-merged #14733, this would make it possible to make calls back to the `AccessDecisionManager` inside a voter (e.g. you might check to see if `IS_AUTHENTICATED_FULLY` from inside your voter).

We originally passed the User instead of the token to be nice, but it's a limitation, and since we never sanitized the User (i.e. a string may be passed to `AbstractToken::isGranted()`), it's not helpful anyways.

Thanks!

Commits
-------

948ccec Updating AbstractVoter so that the method receives the TokenInterface
@@ -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';
Copy link
Member

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

@weaverryan weaverryan deleted the abstract-voter branch September 26, 2015 14:59
weaverryan added a commit to weaverryan/symfony that referenced this pull request Sep 26, 2015
weaverryan added a commit to weaverryan/symfony that referenced this pull request Sep 26, 2015
weaverryan added a commit to weaverryan/symfony that referenced this pull request Sep 26, 2015
weaverryan added a commit to weaverryan/symfony that referenced this pull request Sep 26, 2015
weaverryan added a commit to weaverryan/symfony that referenced this pull request Sep 26, 2015
fabpot added a commit that referenced this pull request Sep 27, 2015
This PR was merged into the 2.8 branch.

Discussion
----------

Abstract voter tweaks

| Q             | A
| ------------- | ---
| Bug fix?      | yes (a little)
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Based on suggestions from stof in #15870, this simplifies the BC and deprecation throwing code. This also adds a BadMethodCallException in case the user doesn't override `isGranted` *or* `voteOnAttribute`, because that's just plain wrong (as is calling `isGranted()` on the parent class directly, since that was formerly abstract).

Commits
-------

c03f5c2 Massively simplifying the BC and deprecated-throwing code thanks to suggestions by stof in #15870
fabpot added a commit that referenced this pull request Sep 27, 2015
* 2.8: (28 commits)
  Detect Mintty for color support on Windows
  Detect Mintty for color support on Windows
  [WebProfilerBundle] Fix search button click listener
  [Form][Type Date/Time] added choice_translation_domain option.
  Massively simplifying the BC and deprecated-throwing code thanks to suggestions by stof in #15870
  Making all "debug" messages use the debug router
  Making GuardTokenInterface extend TokenInterface
  Updating behavior to not continue after an authenticator has set the response
  Add a group for tests of the finder against the FTP server
  Fix trigger_error calls
  Fix legacy security tests
  tweaking message related to configuration edge case that we want to be helpful with
  Minor tweaks - lowering the required security-http requirement and nulling out a test field
  Fix license headers
  Fix license headers
  Fix license headers
  Ensure the ClockMock is loaded before using it in the testsuite
  Allow serializer 3.0 in the PropertyInfo component
  Add the replace rules for the security-guard component
  Forbid serializing a Crawler
  ...
@fabpot fabpot mentioned this pull request Nov 16, 2015
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
* 2.8: (28 commits)
  Detect Mintty for color support on Windows
  Detect Mintty for color support on Windows
  [WebProfilerBundle] Fix search button click listener
  [Form][Type Date/Time] added choice_translation_domain option.
  Massively simplifying the BC and deprecated-throwing code thanks to suggestions by stof in symfony#15870
  Making all "debug" messages use the debug router
  Making GuardTokenInterface extend TokenInterface
  Updating behavior to not continue after an authenticator has set the response
  Add a group for tests of the finder against the FTP server
  Fix trigger_error calls
  Fix legacy security tests
  tweaking message related to configuration edge case that we want to be helpful with
  Minor tweaks - lowering the required security-http requirement and nulling out a test field
  Fix license headers
  Fix license headers
  Fix license headers
  Ensure the ClockMock is loaded before using it in the testsuite
  Allow serializer 3.0 in the PropertyInfo component
  Add the replace rules for the security-guard component
  Forbid serializing a Crawler
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants