vendor/symfony/security-core/Authorization/Strategy/AffirmativeStrategy.php line 34

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 any voter returns an affirmative response.
  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 AffirmativeStrategy implements AccessDecisionStrategyInterface\Stringable
  22. {
  23.     private bool $allowIfAllAbstainDecisions;
  24.     public function __construct(bool $allowIfAllAbstainDecisions false)
  25.     {
  26.         $this->allowIfAllAbstainDecisions $allowIfAllAbstainDecisions;
  27.     }
  28.     public function decide(\Traversable $results): bool
  29.     {
  30.         $deny 0;
  31.         foreach ($results as $result) {
  32.             if (VoterInterface::ACCESS_GRANTED === $result) {
  33.                 return true;
  34.             }
  35.             if (VoterInterface::ACCESS_DENIED === $result) {
  36.                 ++$deny;
  37.             }
  38.         }
  39.         if ($deny 0) {
  40.             return false;
  41.         }
  42.         return $this->allowIfAllAbstainDecisions;
  43.     }
  44.     public function __toString(): string
  45.     {
  46.         return 'affirmative';
  47.     }
  48. }