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

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. /**
  24. * @var bool
  25. */
  26. private $allowIfAllAbstainDecisions;
  27. public function __construct(bool $allowIfAllAbstainDecisions = false)
  28. {
  29. $this->allowIfAllAbstainDecisions = $allowIfAllAbstainDecisions;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function decide(\Traversable $results): bool
  35. {
  36. $deny = 0;
  37. foreach ($results as $result) {
  38. if (VoterInterface::ACCESS_GRANTED === $result) {
  39. return true;
  40. }
  41. if (VoterInterface::ACCESS_DENIED === $result) {
  42. ++$deny;
  43. }
  44. }
  45. if ($deny > 0) {
  46. return false;
  47. }
  48. return $this->allowIfAllAbstainDecisions;
  49. }
  50. public function __toString(): string
  51. {
  52. return 'affirmative';
  53. }
  54. }