8000 feature #16102 Simplify AbstractVoter (Koc) · symfony/symfony@3567548 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3567548

Browse files
committed
feature #16102 Simplify AbstractVoter (Koc)
This PR was merged into the 2.8 branch. Discussion ---------- Simplify AbstractVoter | Q | A | ------------ 8000 - | --- | Bug fix? | no | New feature? | no, just simplification | BC breaks? | no, because 2.8 is not yet released | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 93de659 Simplify AbstractVoter
2 parents 9bbab98 + 93de659 commit 3567548

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

UPGRADE-3.0.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,9 @@ UPGRADE FROM 2.x to 3.0
707707
```php
708708
class MyVoter extends AbstractVoter
709709
{
710-
protected function supports($attribute, $class)
710+
protected function supports($attribute, $object)
711711
{
712-
return $this->isClassInstanceOf($class, 'AppBundle\Entity\Post')
713-
&& in_array($attribute, array('CREATE', 'EDIT'));
712+
return $object instanceof Post && in_array($attribute, array('CREATE', 'EDIT'));
714713
}
715714
716715
// ...

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ public function vote(TokenInterface $token, $object, array $attributes)
6868

6969
// abstain vote by default in case none of the attributes are supported
7070
$vote = self::ACCESS_ABSTAIN;
71-
$class = get_class($object);
7271

7372
foreach ($attributes as $attribute) {
74-
if (!$this->supports($attribute, $class)) {
73+
if (!$this->supports($attribute, $object)) {
7574
continue;
7675
}
7776

@@ -88,25 +87,22 @@ public function vote(TokenInterface $token, $object, array $attributes)
8887
}
8988

9089
/**
91-
* Determines if the attribute and class are supported by this voter.
92-
*
93-
* To determine if the passed class is instance of the supported class, the
94-
* isClassInstanceOf() method can be used.
90+
* Determines if the attribute and object are supported by this voter.
9591
*
9692
* This method will become abstract in 3.0.
9793
*
9894
* @param string $attribute An attribute
99-
* @param string $class The fully qualified class name of the passed object
95+
* @param string $object The object to secure
10096
*
101-
* @return bool True if the attribute and class is supported, false otherwise
97+
* @return bool True if the attribute and object is supported, false otherwise
10298
*/
103-
protected function supports($attribute, $class)
99+
protected function supports($attribute, $object)
104100
{
105101
@trigger_error('The getSupportedClasses and getSupportedAttributes methods are deprecated since version 2.8 and will be removed in version 3.0. Overwrite supports instead.', E_USER_DEPRECATED);
106102

107103
$classIsSupported = false;
108104
foreach ($this->getSupportedClasses() as $supportedClass) {
109-
if ($this->isClassInstanceOf($class, $supportedClass)) {
105+
if ($object instanceof $supportedClass) {
110106
$classIsSupported = true;
111107
break;
112108
}
@@ -123,20 +119,6 @@ protected function supports($attribute, $class)
123119
return true;
124120
}
125121

126-
/**
127-
* A helper method to test if the actual class is instanceof or equal
128-
* to the expected class.
129-
*
130-
* @param string $actualClass The actual class name
131-
* @param string $expectedClass The expected class name
132-
*
133-
* @return bool
134-
*/
135-
protected function isClassInstanceOf($actualClass, $expectedClass)
136-
{
137-
return $expectedClass === $actualClass || is_subclass_of($actualClass, $expectedClass);
138-
}
139-
140122
/**
141123
* Return an array of supported classes. This will be called by supportsClass.
142124
*

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ protected function voteOnAttribute($attribute, $object, TokenInterface $token)
8484
return 'EDIT' === $attribute;
8585
}
8686

87-
protected function supports($attribute, $class)
87+
protected function supports($attribute, $object)
8888
{
89-
return $this->isClassInstanceOf($class, 'stdClass')
90-
&& in_array($attribute, array('EDIT', 'CREATE'));
89+
return $object instanceof \stdClass && in_array($attribute, array('EDIT', 'CREATE'));
9190
}
9291
}
9392

0 commit comments

Comments
 (0)
0