LdapFactoryTrait.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Exception\InvalidConfigurationException;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\Ldap\Security\CheckLdapCredentialsListener;
  16. use Symfony\Component\Ldap\Security\LdapAuthenticator;
  17. /**
  18. * A trait decorating the authenticator with LDAP functionality.
  19. *
  20. * @author Wouter de Jong <wouter@wouterj.nl>
  21. *
  22. * @internal
  23. */
  24. trait LdapFactoryTrait
  25. {
  26. public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
  27. {
  28. $key = str_replace('-', '_', $this->getKey());
  29. if (!class_exists(LdapAuthenticator::class)) {
  30. throw new \LogicException(sprintf('The "%s" authenticator requires the "symfony/ldap" package version "5.1" or higher.', $key));
  31. }
  32. $authenticatorId = parent::createAuthenticator($container, $firewallName, $config, $userProviderId);
  33. $container->setDefinition('security.listener.'.$key.'.'.$firewallName, new Definition(CheckLdapCredentialsListener::class))
  34. ->addTag('kernel.event_subscriber', ['dispatcher' => 'security.event_dispatcher.'.$firewallName])
  35. ->addArgument(new Reference('security.ldap_locator'))
  36. ;
  37. $ldapAuthenticatorId = 'security.authenticator.'.$key.'.'.$firewallName;
  38. $definition = $container->setDefinition($ldapAuthenticatorId, new Definition(LdapAuthenticator::class))
  39. ->setArguments([
  40. new Reference($authenticatorId),
  41. $config['service'],
  42. $config['dn_string'],
  43. $config['search_dn'],
  44. $config['search_password'],
  45. ]);
  46. if (!empty($config['query_string'])) {
  47. if ('' === $config['search_dn'] || '' === $config['search_password']) {
  48. throw new InvalidConfigurationException('Using the "query_string" config without using a "search_dn" and a "search_password" is not supported.');
  49. }
  50. $definition->addArgument($config['query_string']);
  51. }
  52. return $ldapAuthenticatorId;
  53. }
  54. }