Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b8b429

Browse files
committed
feature #15870 Updating AbstractVoter so that the method receives the 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
2 parents d1ae400 + 948ccec commit 5b8b429

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed

UPGRADE-2.8.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,39 @@ FrameworkBundle
406406
session:
407407
cookie_httponly: false
408408
```
409+
410+
Security
411+
--------
412+
413+
* The AbstractToken::isGranted() method was deprecated. Instead,
414+
override the voteOnAttribute() method. This method has one small
415+
difference: it's passed the TokenInterface instead of the user:
416+
417+
Before:
418+
419+
```php
420+
class MyCustomVoter extends AbstractVoter
421+
{
422+
// ...
423+
424+
protected function isGranted($attribute, $object, $user = null)
425+
{
426+
// ...
427+
}
428+
}
429+
```
430+
431+
After:
432+
433+
```php
434+
class MyCustomVoter extends AbstractVoter
435+
{
436+
// ...
437+
438+
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
439+
{
440+
$user = $token->getUser();
441+
// ...
442+
}
443+
}
444+
```

src/Symfony/Component/Security/Core/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
}

src/Symfony/Component/Security/Tests/Core/Authentication/Voter/AbstractVoterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Tests\Core\Authentication\Voter;
1313

14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1415
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
1516

1617
/**
@@ -46,6 +47,17 @@ public function testVote($expectedVote, $object, $attributes, $message)
4647
$this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message);
4748
}
4849

50+
/**
51+
* @dataProvider getData
52+
* @group legacy
53+
*/
54+
public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attributes, $message)
55+
{
56+
$voter = new DeprecatedVoterFixture();
57+
58+
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
59+
}
60+
4961
public function getData()
5062
{
5163
return array(
@@ -75,6 +87,26 @@ protected function getSupportedAttributes()
7587
return array('foo', 'bar', 'baz');
7688
}
7789

90+
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
91+
{
92+
return $attribute === 'foo';
93+
}
94+
}
95+
96+
class DeprecatedVoterFixture extends AbstractVoter
97+
{
98+
protected function getSupportedClasses()
99+
{
100+
return array(
101+
'Symfony\Component\Security\Tests\Core\Authentication\Voter\ObjectFixture',
102+
);
103+
}
104+
105+
protected function getSupportedAttributes()
106+
{
107+
return array('foo', 'bar', 'baz');
108+
}
109+
78110
protected function isGranted($attribute, $object, $user = null)
79111
{
80112
return $attribute === 'foo';

0 commit comments

Comments
 (0)