summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Grekas <nicolas.grekas@gmail.com>2015-11-24 14:06:05 +0100
committerNicolas Grekas <nicolas.grekas@gmail.com>2015-11-24 14:34:07 +0100
commit59f17117d9a634e6a7a74774f31746093e7751dc (patch)
tree71b337c6b2cfe7e98873c4d8e0712c1e982e0ee8
parent37c3a16866afab076aa9be28696b779f91a48fd3 (diff)
downloadsymfony-security-59f17117d9a634e6a7a74774f31746093e7751dc.zip
symfony-security-59f17117d9a634e6a7a74774f31746093e7751dc.tar.gz
symfony-security-59f17117d9a634e6a7a74774f31746093e7751dc.tar.bz2
[Security] Revert changes made between 2.7 and 2.8-beta
-rw-r--r--Core/Authorization/Voter/AbstractVoter.php89
-rw-r--r--Core/Tests/Authorization/Voter/AbstractVoterTest.php74
-rw-r--r--Core/Tests/Authorization/Voter/Fixtures/MyVoter.php27
3 files changed, 41 insertions, 149 deletions
diff --git a/Core/Authorization/Voter/AbstractVoter.php b/Core/Authorization/Voter/AbstractVoter.php
index 7b04222..5dcf787 100644
--- a/Core/Authorization/Voter/AbstractVoter.php
+++ b/Core/Authorization/Voter/AbstractVoter.php
@@ -11,6 +11,8 @@
namespace Symfony\Component\Security\Core\Authorization\Voter;
+@trigger_error('The '.__NAMESPACE__.'\AbstractVoter class is deprecated since version 2.8, to be removed in 3.0. Upgrade to Symfony\Component\Security\Core\Authorization\Voter\Voter instead.', E_USER_DEPRECATED);
+
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -18,6 +20,8 @@ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
* Abstract Voter implementation that reduces boilerplate code required to create a custom Voter.
*
* @author Roman Marintšenko <inoryy@gmail.com>
+ *
+ * @deprecated since version 2.8, to be removed in 3.0. Upgrade to Symfony\Component\Security\Core\Authorization\Voter\Voter instead.
*/
abstract class AbstractVoter implements VoterInterface
{
@@ -26,8 +30,6 @@ abstract class AbstractVoter implements VoterInterface
*/
public function supportsAttribute($attribute)
{
- @trigger_error('The '.__METHOD__.' is deprecated since version 2.8 and will be removed in version 3.0.', E_USER_DEPRECATED);
-
return in_array($attribute, $this->getSupportedAttributes());
}
@@ -36,8 +38,6 @@ abstract class AbstractVoter implements VoterInterface
*/
public function supportsClass($class)
{
- @trigger_error('The '.__METHOD__.' is deprecated since version 2.8 and will be removed in version 3.0.', E_USER_DEPRECATED);
-
foreach ($this->getSupportedClasses() as $supportedClass) {
if ($supportedClass === $class || is_subclass_of($class, $supportedClass)) {
return true;
@@ -62,7 +62,7 @@ abstract class AbstractVoter implements VoterInterface
*/
public function vote(TokenInterface $token, $object, array $attributes)
{
- if (!$object) {
+ if (!$object || !$this->supportsClass(get_class($object))) {
return self::ACCESS_ABSTAIN;
}
@@ -70,14 +70,14 @@ abstract class AbstractVoter implements VoterInterface
$vote = self::ACCESS_ABSTAIN;
foreach ($attributes as $attribute) {
- if (!$this->supports($attribute, $object)) {
+ if (!$this->supportsAttribute($attribute)) {
continue;
}
// as soon as at least one attribute is supported, default is to deny access
$vote = self::ACCESS_DENIED;
- if ($this->voteOnAttribute($attribute, $object, $token)) {
+ if ($this->isGranted($attribute, $object, $token->getUser())) {
// grant access as soon as at least one voter returns a positive response
return self::ACCESS_GRANTED;
}
@@ -87,61 +87,18 @@ abstract class AbstractVoter implements VoterInterface
}
/**
- * Determines if the attribute and object are supported by this voter.
- *
- * This method will become abstract in 3.0.
- *
- * @param string $attribute An attribute
- * @param string $object The object to secure
- *
- * @return bool True if the attribute and object is supported, false otherwise
- */
- protected function supports($attribute, $object)
- {
- @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);
-
- $classIsSupported = false;
- foreach ($this->getSupportedClasses() as $supportedClass) {
- if ($object instanceof $supportedClass) {
- $classIsSupported = true;
- break;
- }
- }
-
- if (!$classIsSupported) {
- return false;
- }
-
- if (!in_array($attribute, $this->getSupportedAttributes())) {
- return false;
- }
-
- return true;
- }
-
- /**
* Return an array of supported classes. This will be called by supportsClass.
*
* @return array an array of supported classes, i.e. array('Acme\DemoBundle\Model\Product')
- *
- * @deprecated since version 2.8, to be removed in 3.0. Use supports() instead.
*/
- protected function getSupportedClasses()
- {
- @trigger_error('The '.__METHOD__.' is deprecated since version 2.8 and will be removed in version 3.0.', E_USER_DEPRECATED);
- }
+ abstract protected function getSupportedClasses();
/**
* Return an array of supported attributes. This will be called by supportsAttribute.
*
* @return array an array of supported attributes, i.e. array('CREATE', 'READ')
- *
- * @deprecated since version 2.8, to be removed in 3.0. Use supports() instead.
*/
- protected function getSupportedAttributes()
- {
- @trigger_error('The '.__METHOD__.' is deprecated since version 2.8 and will be removed in version 3.0.', E_USER_DEPRECATED);
- }
+ abstract protected function getSupportedAttributes();
/**
* Perform a single access check operation on a given attribute, object and (optionally) user
@@ -154,33 +111,7 @@ abstract class AbstractVoter implements VoterInterface
* @param object $object
* @param UserInterface|string $user
*
- * @deprecated This method will be removed in 3.0 - override voteOnAttribute instead.
- *
- * @return bool
- */
- protected function isGranted($attribute, $object, $user = null)
- {
- // forces isGranted() or voteOnAttribute() to be overridden
- throw new \BadMethodCallException(sprintf('You must override the voteOnAttribute() method in "%s".', get_class($this)));
- }
-
- /**
- * Perform a single access check operation on a given attribute, object and token.
- * It is safe to assume that $attribute and $object's class pass supports method call.
- *
- * This method will become abstract in 3.0.
- *
- * @param string $attribute
- * @param object $object
- * @param TokenInterface $token
- *
* @return bool
*/
- protected function voteOnAttribute($attribute, $object, TokenInterface $token)
- {
- // the user should override this method, and not rely on the deprecated isGranted()
- @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);
-
- return $this->isGranted($attribute, $object, $token->getUser());
- }
+ abstract protected function isGranted($attribute, $object, $user = null);
}
diff --git a/Core/Tests/Authorization/Voter/AbstractVoterTest.php b/Core/Tests/Authorization/Voter/AbstractVoterTest.php
index 5ea7732..b537c1b 100644
--- a/Core/Tests/Authorization/Voter/AbstractVoterTest.php
+++ b/Core/Tests/Authorization/Voter/AbstractVoterTest.php
@@ -11,10 +11,11 @@
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
-use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
-use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
+/**
+ * @group legacy
+ */
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
{
protected $token;
@@ -50,75 +51,8 @@ class AbstractVoterTest extends \PHPUnit_Framework_TestCase
*/
public function testVote(array $attributes, $expectedVote, $object, $message)
{
- $voter = new AbstractVoterTest_Voter();
+ $voter = new Fixtures\MyVoter();
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
}
-
- /**
- * @dataProvider getTests
- * @group legacy
- */
- public function testVoteLegacy(array $attributes, $expectedVote, $object, $message)
- {
- $voter = new AbstractVoterTest_LegacyVoter();
-
- $this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
- }
-
- /**
- * @group legacy
- * @expectedException \BadMethodCallException
- */
- public function testNoOverriddenMethodsThrowsException()
- {
- $voter = new AbstractVoterTest_NothingImplementedVoter();
- $voter->vote($this->token, new \stdClass(), array('EDIT'));
- }
-}
-
-class AbstractVoterTest_Voter extends AbstractVoter
-{
- protected function voteOnAttribute($attribute, $object, TokenInterface $token)
- {
- return 'EDIT' === $attribute;
- }
-
- protected function supports($attribute, $object)
- {
- return $object instanceof \stdClass && in_array($attribute, array('EDIT', 'CREATE'));
- }
-}
-
-class AbstractVoterTest_LegacyVoter extends AbstractVoter
-{
- protected function getSupportedClasses()
- {
- return array('stdClass');
- }
-
- protected function getSupportedAttributes()
- {
- return array('EDIT', 'CREATE');
- }
-
- protected function isGranted($attribute, $object, $user = null)
- {
- return 'EDIT' === $attribute;
- }
-}
-
-class AbstractVoterTest_NothingImplementedVoter extends AbstractVoter
-{
- protected function getSupportedClasses()
- {
- return array('stdClass');
- }
-
- protected function getSupportedAttributes()
- {
- return array('EDIT', 'CREATE');
- }
-
- // this is a bad voter that hasn't overridden isGranted or voteOnAttribute
}
diff --git a/Core/Tests/Authorization/Voter/Fixtures/MyVoter.php b/Core/Tests/Authorization/Voter/Fixtures/MyVoter.php
new file mode 100644
index 0000000..b75f798
--- /dev/null
+++ b/Core/Tests/Authorization/Voter/Fixtures/MyVoter.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Symfony\Component\Security\Core\Tests\Authorization\Voter\Fixtures;
+
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
+
+/**
+ * @group legacy
+ */
+class MyVoter extends AbstractVoter
+{
+ protected function getSupportedClasses()
+ {
+ return array('stdClass');
+ }
+
+ protected function getSupportedAttributes()
+ {
+ return array('EDIT', 'CREATE');
+ }
+
+ protected function isGranted($attribute, $object, $user = null)
+ {
+ return 'EDIT' === $attribute;
+ }
+}