vendor/symfony/security-bundle/DependencyInjection/Compiler/AddSecurityVotersPass.php line 51

  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\Bundle\SecurityBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Exception\LogicException;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\Security\Core\Authorization\Voter\TraceableVoter;
  18. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  19. /**
  20.  * Adds all configured security voters to the access decision manager.
  21.  *
  22.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23.  */
  24. class AddSecurityVotersPass implements CompilerPassInterface
  25. {
  26.     use PriorityTaggedServiceTrait;
  27.     public function process(ContainerBuilder $container)
  28.     {
  29.         if (!$container->hasDefinition('security.access.decision_manager')) {
  30.             return;
  31.         }
  32.         $voters $this->findAndSortTaggedServices('security.voter'$container);
  33.         if (!$voters) {
  34.             throw new LogicException('No security voters found. You need to tag at least one with "security.voter".');
  35.         }
  36.         $debug $container->getParameter('kernel.debug');
  37.         $voterServices = [];
  38.         foreach ($voters as $voter) {
  39.             $voterServiceId = (string) $voter;
  40.             $definition $container->getDefinition($voterServiceId);
  41.             $class $container->getParameterBag()->resolveValue($definition->getClass());
  42.             if (!is_a($classVoterInterface::class, true)) {
  43.                 throw new LogicException(sprintf('"%s" must implement the "%s" when used as a voter.'$classVoterInterface::class));
  44.             }
  45.             if ($debug) {
  46.                 $voterServices[] = new Reference($debugVoterServiceId 'debug.security.voter.'.$voterServiceId);
  47.                 $container
  48.                     ->register($debugVoterServiceIdTraceableVoter::class)
  49.                     ->addArgument($voter)
  50.                     ->addArgument(new Reference('event_dispatcher'));
  51.             } else {
  52.                 $voterServices[] = $voter;
  53.             }
  54.         }
  55.         $container->getDefinition('security.access.decision_manager')
  56.             ->replaceArgument(0, new IteratorArgument($voterServices));
  57.     }
  58. }