|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Authorization\Voter; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 15 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Abstract Voter implementation that reduces boilerplate code required to create a custom Voter |
| 19 | + * |
| 20 | + * @author Roman Marintšenko <inoryy@gmail.com> |
| 21 | + */ |
| 22 | +abstract class AbstractVoter implements VoterInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
| 27 | + public function supportsAttribute($attribute) |
| 28 | + { |
| 29 | + return in_array($attribute, $this->getSupportedAttributes()); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * {@inheritdoc} |
| 34 | + */ |
| 35 | + public function supportsClass($class) |
| 36 | + { |
| 37 | + foreach ($this->getSupportedClasses() as $supportedClass) { |
| 38 | + if ($supportedClass === $class || is_subclass_of($class, $supportedClass)) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Iteratively check all given attributes by calling isGranted |
| 48 | + * |
| 49 | + * This method terminates as soon as it is able to return ACCESS_GRANTED |
| 50 | + * If at least one attribute is supported, but access not granted, then ACCESS_DENIED is returned |
| 51 | + * Otherwise it will return ACCESS_ABSTAIN |
| 52 | + * |
| 53 | + * @param TokenInterface $token A TokenInterface instance |
| 54 | + * @param object $object The object to secure |
| 55 | + * @param array $attributes An array of attributes associated with the method being invoked |
| 56 | + * |
| 57 | + * @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED |
| 58 | + */ |
| 59 | + public function vote(TokenInterface $token, $object, array $attributes) |
| 60 | + { |
| 61 | + if (!$object || !$this->supportsClass(get_class($object))) { |
| 62 | + return self::ACCESS_ABSTAIN; |
| 63 | + } |
| 64 | + |
| 65 | + // abstain vote by default in case none of the attributes are supported |
| 66 | + $vote = self::ACCESS_ABSTAIN; |
| 67 | + |
| 68 | + foreach ($attributes as $attribute) { |
| 69 | + if (!$this->supportsAttribute($attribute)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // as soon as at least one attribute is supported, default is to deny access |
| 74 | + $vote = self::ACCESS_DENIED; |
| 75 | + |
| 76 | + if ($this->isGranted($attribute, $object, $token->getUser())) { |
| 77 | + // grant access as soon as at least one voter returns a positive response |
| 78 | + return self::ACCESS_GRANTED; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return $vote; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Return an array of supported classes. This will be called by supportsClass |
| 87 | + * |
| 88 | + * @return array an array of supported classes, i.e. ['\Acme\DemoBundle\Model\Product'] |
| 89 | + */ |
| 90 | + abstract protected function getSupportedClasses(); |
| 91 | + |
| 92 | + /** |
| 93 | + * Return an array of supported attributes. This will be called by supportsAttribute |
| 94 | + * |
| 95 | + * @return array an array of supported attributes, i.e. ['CREATE', 'READ'] |
| 96 | + */ |
| 97 | + abstract protected function getSupportedAttributes(); |
| 98 | + |
| 99 | + /** |
| 100 | + * Perform a single access check operation on a given attribute, object and (optionally) user |
| 101 | + * It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass |
| 102 | + * $user can be one of the following: |
| 103 | + * a UserInterface object (fully authenticated user) |
| 104 | + * a string (anonymously authenticated user) |
| 105 | + * |
| 106 | + * @param string $attribute |
| 107 | + * @param object $object |
| 108 | + * @param UserInterface|string $user |
| 109 | + * |
| 110 | + * @return bool |
| 111 | + */ |
| 112 | + abstract protected function isGranted($attribute, $object, $user = null); |
| 113 | +} |
0 commit comments