summaryrefslogtreecommitdiffstats
path: root/Core
diff options
context:
space:
mode:
authorNicolas Grekas <nicolas.grekas@gmail.com>2015-11-26 15:51:35 +0100
committerNicolas Grekas <nicolas.grekas@gmail.com>2015-11-26 15:51:35 +0100
commit7fb58694400f63e4f01e0bb9750c04d071524619 (patch)
treea8b134b661a0cf7dbef69d7f0d25058837718f68 /Core
parentf6d11e7f3c9834df9dcb66407798bbf427132381 (diff)
parent3bc433a9e00e6f9d12e144fd47403357bdd2fdae (diff)
downloadsymfony-security-7fb58694400f63e4f01e0bb9750c04d071524619.zip
symfony-security-7fb58694400f63e4f01e0bb9750c04d071524619.tar.gz
symfony-security-7fb58694400f63e4f01e0bb9750c04d071524619.tar.bz2
Merge branch '2.8'
* 2.8: [Security] Deprecate "AbstractVoter" in favor of "Voter" [Security] Revert changes made between 2.7 and 2.8-beta Conflicts: UPGRADE-2.8.md src/Symfony/Component/Security/Core/Authorization/Voter/AbstractVoter.php src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AbstractVoterTest.php
Diffstat (limited to 'Core')
-rw-r--r--Core/Authorization/Voter/AbstractVoter.php83
-rw-r--r--Core/Authorization/Voter/Voter.php69
-rw-r--r--Core/Tests/Authorization/Voter/VoterTest.php (renamed from Core/Tests/Authorization/Voter/AbstractVoterTest.php)8
3 files changed, 73 insertions, 87 deletions
diff --git a/Core/Authorization/Voter/AbstractVoter.php b/Core/Authorization/Voter/AbstractVoter.php
deleted file mode 100644
index 665d5f1..0000000
--- a/Core/Authorization/Voter/AbstractVoter.php
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Security\Core\Authorization\Voter;
-
-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>
- */
-abstract class AbstractVoter implements VoterInterface
-{
- /**
- * Iteratively check all given attributes by calling isGranted.
- *
- * This method terminates as soon as it is able to return ACCESS_GRANTED
- * If at least one attribute is supported, but access not granted, then ACCESS_DENIED is returned
- * Otherwise it will return ACCESS_ABSTAIN
- *
- * @param TokenInterface $token A TokenInterface instance
- * @param object $object The object to secure
- * @param array $attributes An array of attributes associated with the method being invoked
- *
- * @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
- */
- public function vote(TokenInterface $token, $object, array $attributes)
- {
- if (!$object) {
- return self::ACCESS_ABSTAIN;
- }
-
- // abstain vote by default in case none of the attributes are supported
- $vote = self::ACCESS_ABSTAIN;
-
- foreach ($attributes as $attribute) {
- if (!$this->supports($attribute, $object)) {
- 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)) {
- // grant access as soon as at least one voter returns a positive response
- return self::ACCESS_GRANTED;
- }
- }
-
- return $vote;
- }
-
- /**
- * Determines if the attribute and object are supported by this voter.
- *
- * @param string $attribute An attribute
- * @param string $object The object to secure
- *
- * @return bool True if the attribute and object is supported, false otherwise
- */
- abstract protected function supports($attribute, $class);
-
- /**
- * 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.
- *
- * @param string $attribute
- * @param object $object
- * @param TokenInterface $token
- *
- * @return bool
- */
- abstract protected function voteOnAttribute($attribute, $object, TokenInterface $token);
-}
diff --git a/Core/Authorization/Voter/Voter.php b/Core/Authorization/Voter/Voter.php
new file mode 100644
index 0000000..2674034
--- /dev/null
+++ b/Core/Authorization/Voter/Voter.php
@@ -0,0 +1,69 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Core\Authorization\Voter;
+
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+
+/**
+ * Voter is an abstract default implementation of a voter.
+ *
+ * @author Roman Marintšenko <inoryy@gmail.com>
+ * @author Grégoire Pineau <lyrixx@lyrixx.info>
+ */
+abstract class Voter implements VoterInterface
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function vote(TokenInterface $token, $object, array $attributes)
+ {
+ // abstain vote by default in case none of the attributes are supported
+ $vote = self::ACCESS_ABSTAIN;
+
+ foreach ($attributes as $attribute) {
+ if (!$this->supports($attribute, $object)) {
+ 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)) {
+ // grant access as soon as at least one attribute returns a positive response
+ return self::ACCESS_GRANTED;
+ }
+ }
+
+ return $vote;
+ }
+
+ /**
+ * Determines if the attribute and subject are supported by this voter.
+ *
+ * @param string $attribute An attribute
+ * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
+ *
+ * @return bool True if the attribute and subject are supported, false otherwise
+ */
+ abstract protected function supports($attribute, $subject);
+
+ /**
+ * Perform a single access check operation on a given attribute, subject and token.
+ *
+ * @param string $attribute
+ * @param mixed $subject
+ * @param TokenInterface $token
+ *
+ * @return bool
+ */
+ abstract protected function voteOnAttribute($attribute, $subject, TokenInterface $token);
+}
diff --git a/Core/Tests/Authorization/Voter/AbstractVoterTest.php b/Core/Tests/Authorization/Voter/VoterTest.php
index 537dc4c..4bac44d 100644
--- a/Core/Tests/Authorization/Voter/AbstractVoterTest.php
+++ b/Core/Tests/Authorization/Voter/VoterTest.php
@@ -12,10 +12,10 @@
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\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
-class AbstractVoterTest extends \PHPUnit_Framework_TestCase
+class VoterTest extends \PHPUnit_Framework_TestCase
{
protected $token;
@@ -50,13 +50,13 @@ class AbstractVoterTest extends \PHPUnit_Framework_TestCase
*/
public function testVote(array $attributes, $expectedVote, $object, $message)
{
- $voter = new AbstractVoterTest_Voter();
+ $voter = new VoterTest_Voter();
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
}
}
-class AbstractVoterTest_Voter extends AbstractVoter
+class VoterTest_Voter extends Voter
{
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
{