|
| 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\Authentication\Token\TokenInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Voter is an abstract default implementation of a voter. |
| 18 | + * |
| 19 | + * @author Roman Marintšenko <inoryy@gmail.com> |
| 20 | + * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 21 | + */ |
| 22 | +abstract class Voter implements VoterInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
| 27 | + public function vote(TokenInterface $token, $object, array $attributes) |
| 28 | + { |
| 29 | + // abstain vote by default in case none of the attributes are supported |
| 30 | + $vote = self::ACCESS_ABSTAIN; |
| 31 | + |
| 32 | + foreach ($attributes as $attribute) { |
| 33 | + if (!$this->supports($attribute, $object)) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + // as soon as at least one attribute is supported, default is to deny access |
| 38 | + $vote = self::ACCESS_DENIED; |
| 39 | + |
| 40 | + if ($this->voteOnAttribute($attribute, $object, $token)) { |
| 41 | + // grant access as soon as at least one attribute returns a positive response |
| 42 | + return self::ACCESS_GRANTED; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return $vote; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Determines if the attribute and subject are supported by this voter. |
| 51 | + * |
| 52 | + * @param string $attribute An attribute |
| 53 | + * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type |
| 54 | + * |
| 55 | + * @return bool True if the attribute and subject are supported, false otherwise |
| 56 | + */ |
| 57 | + abstract protected function supports($attribute, $subject); |
| 58 | + |
| 59 | + /** |
| 60 | + * Perform a single access check operation on a given attribute, subject and token. |
| 61 | + * |
| 62 | + * @param string $attribute |
| 63 | + * @param mixed $subject |
| 64 | + * @param TokenInterface $token |
| 65 | + * |
| 66 | + * @return bool |
| 67 | + */ |
| 68 | + abstract protected function voteOnAttribute($attribute, $subject, TokenInterface $token); |
| 69 | +} |
0 commit comments