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

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.             try {
  29.                 if (!$this->supports($attribute$subject)) {
  30.                     continue;
  31.                 }
  32.             } catch (\TypeError $e) {
  33.                 if (\PHP_VERSION_ID 80000) {
  34.                     if (=== strpos($e->getMessage(), 'Argument 1 passed to')
  35.                         && false !== strpos($e->getMessage(), '::supports() must be of the type string')) {
  36.                         continue;
  37.                     }
  38.                 } elseif (false !== strpos($e->getMessage(), 'supports(): Argument #1')) {
  39.                     continue;
  40.                 }
  41.                 throw $e;
  42.             }
  43.             // as soon as at least one attribute is supported, default is to deny access
  44.             $vote self::ACCESS_DENIED;
  45.             if ($this->voteOnAttribute($attribute$subject$token)) {
  46.                 // grant access as soon as at least one attribute returns a positive response
  47.                 return self::ACCESS_GRANTED;
  48.             }
  49.         }
  50.         return $vote;
  51.     }
  52.     /**
  53.      * Determines if the attribute and subject are supported by this voter.
  54.      *
  55.      * @param string $attribute An attribute
  56.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  57.      *
  58.      * @return bool True if the attribute and subject are supported, false otherwise
  59.      */
  60.     abstract protected function supports(string $attribute$subject);
  61.     /**
  62.      * Perform a single access check operation on a given attribute, subject and token.
  63.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  64.      *
  65.      * @param mixed $subject
  66.      *
  67.      * @return bool
  68.      */
  69.     abstract protected function voteOnAttribute(string $attribute$subjectTokenInterface $token);
  70. }