summaryrefslogtreecommitdiffstats
path: root/Core/User
diff options
context:
space:
mode:
authorCharles Sarrazin <charles@sarraz.in>2016-01-28 14:54:41 +0100
committerCharles Sarrazin <charles@sarraz.in>2016-02-11 18:54:23 +0100
commita07df7b1ba2f304191e4b2232072f34475bd5bcc (patch)
tree5c14602213e50803d6ae1b50b83d2e8cccebc0fa /Core/User
parentb5c4b14ce46387314b6bd17f13ac151b446c6847 (diff)
downloadsymfony-security-a07df7b1ba2f304191e4b2232072f34475bd5bcc.zip
symfony-security-a07df7b1ba2f304191e4b2232072f34475bd5bcc.tar.gz
symfony-security-a07df7b1ba2f304191e4b2232072f34475bd5bcc.tar.bz2
Improved the Ldap Component
* Moved connection logic to dedicated class * Added support for Ldap result entries iterator and renamed LdapClient to Ldap * Added support for multiple adapters * Attempt anonymous bind if the connection is not bound beforehand * Finalized API * Updated the Security component to use v3.1 of the Ldap component * Updated unit tests * Added support for functional tests * Updated README file
Diffstat (limited to 'Core/User')
-rw-r--r--Core/User/LdapUserProvider.php48
1 files changed, 23 insertions, 25 deletions
diff --git a/Core/User/LdapUserProvider.php b/Core/User/LdapUserProvider.php
index 1593564..a37981c 100644
--- a/Core/User/LdapUserProvider.php
+++ b/Core/User/LdapUserProvider.php
@@ -11,10 +11,11 @@
namespace Symfony\Component\Security\Core\User;
+use Symfony\Component\Ldap\Entry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Ldap\Exception\ConnectionException;
-use Symfony\Component\Ldap\LdapClientInterface;
+use Symfony\Component\Ldap\LdapInterface;
/**
* LdapUserProvider is a simple user provider on top of ldap.
@@ -32,15 +33,15 @@ class LdapUserProvider implements UserProviderInterface
private $defaultSearch;
/**
- * @param LdapClientInterface $ldap
- * @param string $baseDn
- * @param string $searchDn
- * @param string $searchPassword
- * @param array $defaultRoles
- * @param string $uidKey
- * @param string $filter
+ * @param LdapInterface $ldap
+ * @param string $baseDn
+ * @param string $searchDn
+ * @param string $searchPassword
+ * @param array $defaultRoles
+ * @param string $uidKey
+ * @param string $filter
*/
- public function __construct(LdapClientInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})')
+ public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})')
{
$this->ldap = $ldap;
$this->baseDn = $baseDn;
@@ -57,33 +58,25 @@ class LdapUserProvider implements UserProviderInterface
{
try {
$this->ldap->bind($this->searchDn, $this->searchPassword);
- $username = $this->ldap->escape($username, '', LDAP_ESCAPE_FILTER);
+ $username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_FILTER);
$query = str_replace('{username}', $username, $this->defaultSearch);
- $search = $this->ldap->find($this->baseDn, $query);
+ $search = $this->ldap->query($this->baseDn, $query);
} catch (ConnectionException $e) {
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e);
}
- if (!$search) {
+ $entries = $search->execute();
+ $count = count($entries);
+
+ if (!$count) {
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
}
- if ($search['count'] > 1) {
+ if ($count > 1) {
throw new UsernameNotFoundException('More than one user found');
}
- $user = $search[0];
-
- return $this->loadUser($username, $user);
- }
-
- public function loadUser($username, $user)
- {
- $password = isset($user['userpassword']) ? $user['userpassword'] : null;
-
- $roles = $this->defaultRoles;
-
- return new User($username, $password, $roles);
+ return $this->loadUser($username, $entries[0]);
}
/**
@@ -105,4 +98,9 @@ class LdapUserProvider implements UserProviderInterface
{
return $class === 'Symfony\Component\Security\Core\User\User';
}
+
+ private function loadUser($username, Entry $entry)
+ {
+ return new User($username, $entry->getAttribute('userpassword'), $this->defaultRoles);
+ }
}