AddSecurityVotersPass.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Exception\LogicException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Adds all configured security voters to the access decision manager.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class AddSecurityVotersPass implements CompilerPassInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function process(ContainerBuilder $container)
  26. {
  27. if (!$container->hasDefinition('security.access.decision_manager')) {
  28. return;
  29. }
  30. $voters = array();
  31. foreach ($container->findTaggedServiceIds('security.voter') as $id => $attributes) {
  32. $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
  33. $voters[$priority][] = new Reference($id);
  34. }
  35. krsort($voters);
  36. $voters = \call_user_func_array('array_merge', $voters);
  37. if (!$voters) {
  38. throw new LogicException('No security voters found. You need to tag at least one with "security.voter"');
  39. }
  40. $container->getDefinition('security.access.decision_manager')->addMethodCall('setVoters', array($voters));
  41. }
  42. }