summaryrefslogtreecommitdiffstats
path: root/Core
diff options
context:
space:
mode:
authorRoman Marintšenko <inoryy@gmail.com>2014-06-20 14:24:18 +0300
committerFabien Potencier <fabien.potencier@gmail.com>2014-09-23 11:51:18 +0200
commit18d45ff47273f1bb9a11b917f653621cf6cdad03 (patch)
treede6a954cd3a955f7154bf0ebe32d117c98c31213 /Core
parent729bef85463490cebd41160e3a111954f63a1638 (diff)
downloadsymfony-security-18d45ff47273f1bb9a11b917f653621cf6cdad03.zip
symfony-security-18d45ff47273f1bb9a11b917f653621cf6cdad03.tar.gz
symfony-security-18d45ff47273f1bb9a11b917f653621cf6cdad03.tar.bz2
[Security] add an AbstractVoter implementation
Diffstat (limited to 'Core')
-rw-r--r--Core/Authorization/Voter/AbstractVoter.php113
1 files changed, 113 insertions, 0 deletions
diff --git a/Core/Authorization/Voter/AbstractVoter.php b/Core/Authorization/Voter/AbstractVoter.php
new file mode 100644
index 0000000..61c928e
--- /dev/null
+++ b/Core/Authorization/Voter/AbstractVoter.php
@@ -0,0 +1,113 @@
+<?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\User\UserInterface;
+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
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function supportsAttribute($attribute)
+ {
+ return in_array($attribute, $this->getSupportedAttributes());
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function supportsClass($class)
+ {
+ foreach ($this->getSupportedClasses() as $supportedClass) {
+ if ($supportedClass === $class || is_subclass_of($class, $supportedClass)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * 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 || !$this->supportsClass(get_class($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->supportsAttribute($attribute)) {
+ continue;
+ }
+
+ // as soon as at least one attribute is supported, default is to deny access
+ $vote = self::ACCESS_DENIED;
+
+ if ($this->isGranted($attribute, $object, $token->getUser())) {
+ // grant access as soon as at least one voter returns a positive response
+ return self::ACCESS_GRANTED;
+ }
+ }
+
+ return $vote;
+ }
+
+ /**
+ * Return an array of supported classes. This will be called by supportsClass
+ *
+ * @return array an array of supported classes, i.e. ['\Acme\DemoBundle\Model\Product']
+ */
+ 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. ['CREATE', 'READ']
+ */
+ abstract protected function getSupportedAttributes();
+
+ /**
+ * Perform a single access check operation on a given attribute, object and (optionally) user
+ * It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass
+ * $user can be one of the following:
+ * a UserInterface object (fully authenticated user)
+ * a string (anonymously authenticated user)
+ *
+ * @param string $attribute
+ * @param object $object
+ * @param UserInterface|string $user
+ *
+ * @return bool
+ */
+ abstract protected function isGranted($attribute, $object, $user = null);
+}