HttpBasicLdapFactory.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\DependencyInjection\ChildDefinition;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\Security\Core\Exception\LogicException;
  16. /**
  17. * HttpBasicFactory creates services for HTTP basic authentication.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  21. * @author Charles Sarrazin <charles@sarraz.in>
  22. *
  23. * @internal
  24. */
  25. class HttpBasicLdapFactory extends HttpBasicFactory
  26. {
  27. use LdapFactoryTrait;
  28. public function create(ContainerBuilder $container, string $id, array $config, string $userProvider, ?string $defaultEntryPoint)
  29. {
  30. $provider = 'security.authentication.provider.ldap_bind.'.$id;
  31. $definition = $container
  32. ->setDefinition($provider, new ChildDefinition('security.authentication.provider.ldap_bind'))
  33. ->replaceArgument(0, new Reference($userProvider))
  34. ->replaceArgument(1, new Reference('security.user_checker.'.$id))
  35. ->replaceArgument(2, $id)
  36. ->replaceArgument(3, new Reference($config['service']))
  37. ->replaceArgument(4, $config['dn_string'])
  38. ->replaceArgument(6, $config['search_dn'])
  39. ->replaceArgument(7, $config['search_password'])
  40. ;
  41. // entry point
  42. $entryPointId = $defaultEntryPoint;
  43. if (null === $entryPointId) {
  44. $entryPointId = 'security.authentication.basic_entry_point.'.$id;
  45. $container
  46. ->setDefinition($entryPointId, new ChildDefinition('security.authentication.basic_entry_point'))
  47. ->addArgument($config['realm']);
  48. }
  49. if (!empty($config['query_string'])) {
  50. if ('' === $config['search_dn'] || '' === $config['search_password']) {
  51. throw new LogicException('Using the "query_string" config without using a "search_dn" and a "search_password" is not supported.');
  52. }
  53. $definition->addMethodCall('setQueryString', [$config['query_string']]);
  54. }
  55. // listener
  56. $listenerId = 'security.authentication.listener.basic.'.$id;
  57. $listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.basic'));
  58. $listener->replaceArgument(2, $id);
  59. $listener->replaceArgument(3, new Reference($entryPointId));
  60. return [$provider, $listenerId, $entryPointId];
  61. }
  62. public function addConfiguration(NodeDefinition $node)
  63. {
  64. parent::addConfiguration($node);
  65. $node
  66. ->children()
  67. ->scalarNode('service')->defaultValue('ldap')->end()
  68. ->scalarNode('dn_string')->defaultValue('{username}')->end()
  69. ->scalarNode('query_string')->end()
  70. ->scalarNode('search_dn')->defaultValue('')->end()
  71. ->scalarNode('search_password')->defaultValue('')->end()
  72. ->end()
  73. ;
  74. }
  75. public function getKey()
  76. {
  77. return 'http-basic-ldap';
  78. }
  79. }