AnonymousFactory.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Security\Factory;
  11. use Symfony\Component\Config\Definition\Builder\NodeDefinition;
  12. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  13. use Symfony\Component\DependencyInjection\ChildDefinition;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Parameter;
  16. /**
  17. * @author Wouter de Jong <wouter@wouterj.nl>
  18. *
  19. * @internal
  20. */
  21. class AnonymousFactory implements SecurityFactoryInterface, AuthenticatorFactoryInterface
  22. {
  23. public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
  24. {
  25. if (null === $config['secret']) {
  26. $config['secret'] = new Parameter('container.build_hash');
  27. }
  28. $listenerId = 'security.authentication.listener.anonymous.'.$id;
  29. $container
  30. ->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.anonymous'))
  31. ->replaceArgument(1, $config['secret'])
  32. ;
  33. $providerId = 'security.authentication.provider.anonymous.'.$id;
  34. $container
  35. ->setDefinition($providerId, new ChildDefinition('security.authentication.provider.anonymous'))
  36. ->replaceArgument(0, $config['secret'])
  37. ;
  38. return [$providerId, $listenerId, $defaultEntryPoint];
  39. }
  40. public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
  41. {
  42. throw new InvalidConfigurationException(sprintf('The authenticator manager no longer has "anonymous" security. Please remove this option under the "%s" firewall'.($config['lazy'] ? ' and add "lazy: true"' : '').'.', $firewallName));
  43. }
  44. public function getPosition()
  45. {
  46. return 'anonymous';
  47. }
  48. public function getKey()
  49. {
  50. return 'anonymous';
  51. }
  52. public function addConfiguration(NodeDefinition $builder)
  53. {
  54. $builder
  55. ->beforeNormalization()
  56. ->ifTrue(function ($v) { return 'lazy' === $v; })
  57. ->then(function ($v) { return ['lazy' => true]; })
  58. ->end()
  59. ->children()
  60. ->booleanNode('lazy')->defaultFalse()->setDeprecated('symfony/security-bundle', '5.1', 'Using "anonymous: lazy" to make the firewall lazy is deprecated, use "anonymous: true" and "lazy: true" instead.')->end()
  61. ->scalarNode('secret')->defaultNull()->end()
  62. ->end()
  63. ;
  64. }
  65. }