RegisterTokenUsageTrackingPass.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Bridge\Monolog\Processor\ProcessorInterface;
  12. use Symfony\Component\DependencyInjection\Argument\BoundArgument;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. /**
  18. * Injects the session tracker enabler in "security.context_listener" + binds "security.untracked_token_storage" to ProcessorInterface instances.
  19. *
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. *
  22. * @internal
  23. */
  24. class RegisterTokenUsageTrackingPass implements CompilerPassInterface
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function process(ContainerBuilder $container)
  30. {
  31. if (!$container->has('security.untracked_token_storage')) {
  32. return;
  33. }
  34. $processorAutoconfiguration = $container->registerForAutoconfiguration(ProcessorInterface::class);
  35. $processorAutoconfiguration->setBindings($processorAutoconfiguration->getBindings() + [
  36. TokenStorageInterface::class => new BoundArgument(new Reference('security.untracked_token_storage'), false),
  37. ]);
  38. if (!$container->has('session')) {
  39. $container->setAlias('security.token_storage', 'security.untracked_token_storage')->setPublic(true);
  40. $container->getDefinition('security.untracked_token_storage')->addTag('kernel.reset', ['method' => 'reset']);
  41. } elseif ($container->hasDefinition('security.context_listener')) {
  42. $tokenStorageClass = $container->getParameterBag()->resolveValue($container->findDefinition('security.token_storage')->getClass());
  43. if (method_exists($tokenStorageClass, 'enableUsageTracking')) {
  44. $container->getDefinition('security.context_listener')
  45. ->setArgument(6, [new Reference('security.token_storage'), 'enableUsageTracking']);
  46. }
  47. }
  48. }
  49. }