vendor/symfony/security-core/Authorization/Strategy/UnanimousStrategy.php line 37

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\Strategy;
  11. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  12. /**
  13.  * Grants access if only grant (or abstain) votes were received.
  14.  *
  15.  * If all voters abstained from voting, the decision will be based on the
  16.  * allowIfAllAbstainDecisions property value (defaults to false).
  17.  *
  18.  * @author Fabien Potencier <fabien@symfony.com>
  19.  * @author Alexander M. Turek <me@derrabus.de>
  20.  */
  21. final class UnanimousStrategy implements AccessDecisionStrategyInterface, \Stringable
  22. {
  23.     private $allowIfAllAbstainDecisions;
  24.     public function __construct(bool $allowIfAllAbstainDecisions false)
  25.     {
  26.         $this->allowIfAllAbstainDecisions $allowIfAllAbstainDecisions;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function decide(\Traversable $results): bool
  32.     {
  33.         $grant 0;
  34.         foreach ($results as $result) {
  35.             if (VoterInterface::ACCESS_DENIED === $result) {
  36.                 return false;
  37.             }
  38.             if (VoterInterface::ACCESS_GRANTED === $result) {
  39.                 ++$grant;
  40.             }
  41.         }
  42.         // no deny votes
  43.         if ($grant 0) {
  44.             return true;
  45.         }
  46.         return $this->allowIfAllAbstainDecisions;
  47.     }
  48.     public function __toString(): string
  49.     {
  50.         return 'unanimous';
  51.     }
  52. }