Skip to content

Commit 1e65f71

Browse files
committed
bug #15921 Abstract voter tweaks (weaverryan)
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
2 parents 709cc13 + c03f5c2 commit 1e65f71

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

src/Symfony/Component/Security/Core/Authorization/Voter/AbstractVoter.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ public function vote(TokenInterface $token, $object, array $attributes)
7070
$vote = self::ACCESS_ABSTAIN;
7171
$class = get_class($object);
7272

73-
$reflector = new \ReflectionMethod($this, 'voteOnAttribute');
74-
$isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter';
75-
if (!$isNewOverwritten) {
76-
@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);
77-
}
78-
7973
foreach ($attributes as $attribute) {
8074
if (!$this->supports($attribute, $class)) {
8175
continue;
@@ -84,16 +78,9 @@ public function vote(TokenInterface $token, $object, array $attributes)
8478
// as soon as at least one attribute is supported, default is to deny access
8579
$vote = self::ACCESS_DENIED;
8680

87-
if ($isNewOverwritten) {
88-
if ($this->voteOnAttribute($attribute, $object, $token)) {
89-
// grant access as soon as at least one voter returns a positive response
90-
return self::ACCESS_GRANTED;
91-
}
92-
} else {
93-
if ($this->isGranted($attribute, $object, $token->getUser())) {
94-
// grant access as soon as at least one voter returns a positive response
95-
return self::ACCESS_GRANTED;
96-
}
81+
if ($this->voteOnAttribute($attribute, $object, $token)) {
82+
// grant access as soon as at least one voter returns a positive response
83+
return self::ACCESS_GRANTED;
9784
}
9885
}
9986

@@ -191,7 +178,8 @@ protected function getSupportedAttributes()
191178
*/
192179
protected function isGranted($attribute, $object, $user = null)
193180
{
194-
return false;
181+
// forces isGranted() or voteOnAttribute() to be overridden
182+
throw new \BadMethodCallException(sprintf('You must override the voteOnAttribute() method in "%s".', get_class($this)));
195183
}
196184

197185
/**
@@ -211,6 +199,9 @@ protected function isGranted($attribute, $object, $user = null)
211199
*/
212200
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
213201
{
214-
return false;
202+
// the user should override this method, and not rely on the deprecated isGranted()
203+
@trigger_error(sprintf("The AbstractVoter::isGranted() method is deprecated since 2.8 and won't be called anymore in 3.0. Override voteOnAttribute() in %s instead.", get_class($this)), E_USER_DEPRECATED);
204+
205+
return $this->isGranted($attribute, $object, $token->getUser());
215206
}
216207
}

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@
1919
*/
2020
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
2121
{
22-
/**
23-
* @var AbstractVoter
24-
*/
25-
private $voter;
26-
2722
private $token;
2823

2924
protected function setUp()
3025
{
31-
$this->voter = new VoterFixture();
32-
3326
$tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
3427
$tokenMock
3528
->expects($this->any())
@@ -44,7 +37,9 @@ protected function setUp()
4437
*/
4538
public function testVote($expectedVote, $object, $attributes, $message)
4639
{
47-
$this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message);
40+
$voter = new VoterFixture();
41+
42+
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
4843
}
4944

5045
/**
@@ -58,6 +53,16 @@ public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attrib
5853
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
5954
}
6055

56+
/**
57+
* @group legacy
58+
* @expectedException \BadMethodCallException
59+
*/
60+
public function testNoOverriddenMethodsThrowsException()
61+
{
62+
$voter = new DeprecatedVoterNothingImplementedFixture();
63+
$voter->vote($this->token, new ObjectFixture(), array('foo'));
64+
}
65+
6166
public function getData()
6267
{
6368
return array(
@@ -113,6 +118,23 @@ protected function isGranted($attribute, $object, $user = null)
113118
}
114119
}
115120

121+
class DeprecatedVoterNothingImplementedFixture extends AbstractVoter
122+
{
123+
protected function getSupportedClasses()
124+
{
125+
return array(
126+
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
127+
);
128+
}
129+
130+
protected function getSupportedAttributes()
131+
{
132+
return array('foo', 'bar', 'baz');
133+
}
134+
135+
// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
136+
}
137+
116138
class ObjectFixture
117139
{
118140
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy