Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit528e2c6

Browse files
committed
Massively simplifying the BC and deprecated-throwing code thanks to suggestions by stof in#15870
1 parenta7612b1 commit528e2c6

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

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

Lines changed: 9 additions & 21 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() !==__CLASS__;
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() in %s instead.",$reflector->class,get_class($this)),E_USER_DEPRECATED);
77-
}
78-
7973
foreach ($attributesas$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-
returnself::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-
returnself::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+
returnself::ACCESS_GRANTED;
9784
}
9885
}
9986

@@ -191,10 +178,8 @@ protected function getSupportedAttributes()
191178
*/
192179
protectedfunctionisGranted($attribute,$object,$user =null)
193180
{
194-
// in case someone calls this directly
195-
@trigger_error('The'.__METHOD__.' is deprecated since version 2.8 and will be removed in version 3.0.');
196-
197-
returnfalse;
181+
// forces isGranted() or voteOnAttribute() to be overridden
182+
thrownew \BadMethodCallException(sprintf('You must override either the voteOnAttribute() or isGranted() (deprecated) method in "%s"',get_class($this)));
198183
}
199184

200185
/**
@@ -214,6 +199,9 @@ protected function isGranted($attribute, $object, $user = null)
214199
*/
215200
protectedfunctionvoteOnAttribute($attribute,$object,TokenInterface$token)
216201
{
217-
returnfalse;
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());
218206
}
219207
}

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

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

2924
protectedfunctionsetUp()
3025
{
31-
$this->voter =newVoterFixture();
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
publicfunctiontestVote($expectedVote,$object,$attributes,$message)
4639
{
47-
$this->assertEquals($expectedVote,$this->voter->vote($this->token,$object,$attributes),$message);
40+
$voter =newVoterFixture();
41+
42+
$this->assertEquals($expectedVote,$voter->vote($this->token,$object,$attributes),$message);
4843
}
4944

5045
/**
@@ -58,6 +53,17 @@ 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+
publicfunctiontestNoOverriddenMethodsThrowsException()
61+
{
62+
$voter =newDeprecatedVoterNothingImplementedFixture();
63+
$voter->vote($this->token,newObjectFixture(),array('foo'));
64+
}
65+
66+
6167
publicfunctiongetData()
6268
{
6369
returnarray(
@@ -113,6 +119,23 @@ protected function isGranted($attribute, $object, $user = null)
113119
}
114120
}
115121

122+
class DeprecatedVoterNothingImplementedFixtureextends AbstractVoter
123+
{
124+
protectedfunctiongetSupportedClasses()
125+
{
126+
returnarray(
127+
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
128+
);
129+
}
130+
131+
protectedfunctiongetSupportedAttributes()
132+
{
133+
returnarray('foo','bar','baz');
134+
}
135+
136+
// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
137+
}
138+
116139
class ObjectFixture
117140
{
118141
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp