vendor/symfony/security-core/Authorization/Voter/Voter.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core\Authorization\Voter;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. /**
  13.  * Voter is an abstract default implementation of a voter.
  14.  *
  15.  * @author Roman Marintšenko <inoryy@gmail.com>
  16.  * @author Grégoire Pineau <lyrixx@lyrixx.info>
  17.  */
  18. abstract class Voter implements VoterInterface
  19. {
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public function vote(TokenInterface $token$subject, array $attributes)
  24.     {
  25.         // abstain vote by default in case none of the attributes are supported
  26.         $vote self::ACCESS_ABSTAIN;
  27.         foreach ($attributes as $attribute) {
  28.             if (!$this->supports($attribute$subject)) {
  29.                 continue;
  30.             }
  31.             // as soon as at least one attribute is supported, default is to deny access
  32.             $vote self::ACCESS_DENIED;
  33.             if ($this->voteOnAttribute($attribute$subject$token)) {
  34.                 // grant access as soon as at least one attribute returns a positive response
  35.                 return self::ACCESS_GRANTED;
  36.             }
  37.         }
  38.         return $vote;
  39.     }
  40.     /**
  41.      * Determines if the attribute and subject are supported by this voter.
  42.      *
  43.      * @param string $attribute An attribute
  44.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  45.      *
  46.      * @return bool True if the attribute and subject are supported, false otherwise
  47.      */
  48.     abstract protected function supports($attribute$subject);
  49.     /**
  50.      * Perform a single access check operation on a given attribute, subject and token.
  51.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  52.      *
  53.      * @param string $attribute
  54.      * @param mixed  $subject
  55.      *
  56.      * @return bool
  57.      */
  58.     abstract protected function voteOnAttribute($attribute$subjectTokenInterface $token);
  59. }