LdapUserProvider.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Component\Security\Core\User;
  11. use Symfony\Component\Ldap\Exception\ConnectionException;
  12. use Symfony\Component\Ldap\LdapClientInterface;
  13. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  14. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  15. /**
  16. * LdapUserProvider is a simple user provider on top of ldap.
  17. *
  18. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  19. * @author Charles Sarrazin <charles@sarraz.in>
  20. */
  21. class LdapUserProvider implements UserProviderInterface
  22. {
  23. private $ldap;
  24. private $baseDn;
  25. private $searchDn;
  26. private $searchPassword;
  27. private $defaultRoles;
  28. private $defaultSearch;
  29. /**
  30. * @param LdapClientInterface $ldap
  31. * @param string $baseDn
  32. * @param string $searchDn
  33. * @param string $searchPassword
  34. * @param array $defaultRoles
  35. * @param string $uidKey
  36. * @param string $filter
  37. */
  38. public function __construct(LdapClientInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})')
  39. {
  40. $this->ldap = $ldap;
  41. $this->baseDn = $baseDn;
  42. $this->searchDn = $searchDn;
  43. $this->searchPassword = $searchPassword;
  44. $this->defaultRoles = $defaultRoles;
  45. $this->defaultSearch = str_replace('{uid_key}', $uidKey, $filter);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function loadUserByUsername($username)
  51. {
  52. try {
  53. $this->ldap->bind($this->searchDn, $this->searchPassword);
  54. $username = $this->ldap->escape($username, '', LDAP_ESCAPE_FILTER);
  55. $query = str_replace('{username}', $username, $this->defaultSearch);
  56. $search = $this->ldap->find($this->baseDn, $query);
  57. } catch (ConnectionException $e) {
  58. throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e);
  59. }
  60. if (!$search) {
  61. throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
  62. }
  63. if ($search['count'] > 1) {
  64. throw new UsernameNotFoundException('More than one user found');
  65. }
  66. $user = $search[0];
  67. return $this->loadUser($username, $user);
  68. }
  69. public function loadUser($username, $user)
  70. {
  71. $password = isset($user['userpassword']) ? $user['userpassword'] : null;
  72. $roles = $this->defaultRoles;
  73. return new User($username, $password, $roles);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function refreshUser(UserInterface $user)
  79. {
  80. if (!$user instanceof User) {
  81. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
  82. }
  83. return new User($user->getUsername(), null, $user->getRoles());
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function supportsClass($class)
  89. {
  90. return 'Symfony\Component\Security\Core\User\User' === $class;
  91. }
  92. }