Skip to content

Commit 8c8cfa1

Browse files
committed
Simplify AbstractVoter
1 parent 1805649 commit 8c8cfa1

File tree

2 files changed

+8
-27
lines changed

2 files changed

+8
-27
lines changed

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

+6-24
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

+2-3
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)