LdapFactory.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\UserProvider;
  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. /**
  16. * LdapFactory creates services for Ldap user provider.
  17. *
  18. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  19. * @author Charles Sarrazin <charles@sarraz.in>
  20. */
  21. class LdapFactory implements UserProviderFactoryInterface
  22. {
  23. public function create(ContainerBuilder $container, string $id, array $config)
  24. {
  25. $container
  26. ->setDefinition($id, new ChildDefinition('security.user.provider.ldap'))
  27. ->replaceArgument(0, new Reference($config['service']))
  28. ->replaceArgument(1, $config['base_dn'])
  29. ->replaceArgument(2, $config['search_dn'])
  30. ->replaceArgument(3, $config['search_password'])
  31. ->replaceArgument(4, $config['default_roles'])
  32. ->replaceArgument(5, $config['uid_key'])
  33. ->replaceArgument(6, $config['filter'])
  34. ->replaceArgument(7, $config['password_attribute'])
  35. ->replaceArgument(8, $config['extra_fields'])
  36. ;
  37. }
  38. public function getKey()
  39. {
  40. return 'ldap';
  41. }
  42. public function addConfiguration(NodeDefinition $node)
  43. {
  44. $node
  45. ->fixXmlConfig('extra_field')
  46. ->fixXmlConfig('default_role')
  47. ->children()
  48. ->scalarNode('service')->isRequired()->cannotBeEmpty()->defaultValue('ldap')->end()
  49. ->scalarNode('base_dn')->isRequired()->cannotBeEmpty()->end()
  50. ->scalarNode('search_dn')->defaultNull()->end()
  51. ->scalarNode('search_password')->defaultNull()->end()
  52. ->arrayNode('extra_fields')
  53. ->prototype('scalar')->end()
  54. ->end()
  55. ->arrayNode('default_roles')
  56. ->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
  57. ->requiresAtLeastOneElement()
  58. ->prototype('scalar')->end()
  59. ->end()
  60. ->scalarNode('uid_key')->defaultValue('sAMAccountName')->end()
  61. ->scalarNode('filter')->defaultValue('({uid_key}={username})')->end()
  62. ->scalarNode('password_attribute')->defaultNull()->end()
  63. ->end()
  64. ;
  65. }
  66. }