8000 [ldap][2.8] Added search before bind to LdapClient using LdapUserProv… · symfony/symfony@b0e74e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0e74e8

Browse files
committed
[ldap][2.8] Added search before bind to LdapClient using LdapUserProvider functionality
1 parent 1254d41 commit b0e74e8

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

src/Symfony/Component/Ldap/LdapClient.php

100644100755
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Ldap\Exception\ConnectionException;
1515
use Symfony\Component\Ldap\Exception\LdapException;
16+
use Symfony\Component\Security\Core\User\LdapUserProvider;
1617

1718
/**
1819
* @author Grégoire Pineau <lyrixx@lyrixx.info>
@@ -42,7 +43,7 @@ class LdapClient implements LdapClientInterface
4243
* @param bool $useStartTls
4344
* @param bool $optReferrals
4445
*/
45-
public function __construct($host = null, $port = 389, $version = 3, $useSsl = false, $useStartTls = false, $optReferrals = false)
46+
public function __construct($host = null, $port = 389, $version = 3, $useSsl = false, $useStartTls = false, $optReferrals = false, $ldapBaseDn = null, $ldapSearchDn = null, $ldapSearchPassword = null, $ldapUidKey = null, $ldapFilter = null)
4647
{
4748
if (!extension_loaded('ldap')) {
4849
throw new LdapException('The ldap module is needed.');
@@ -54,6 +55,11 @@ public function __construct($host = null, $port = 389, $version = 3, $useSsl = f
5455
$this->useSsl = (bool) $useSsl;
5556
$this->useStartTls = (bool) $useStartTls;
5657
$this->optReferrals = (bool) $optReferrals;
58+
$this->ldapBaseDn = $ldapBaseDn;
59+
$this->ldapSearchDn = $ldapSearchDn;
60+
$this->ldapSearchPassword = $ldapSearchPassword;
61+
$this->ldapUidKey = $ldapUidKey;
62+
$this->ldapFilter = $ldapFilter;
5763
}
5864

5965
public function __destruct()
@@ -70,16 +76,14 @@ public function bind($dn = null, $password = null)
7076
$this->connect();
7177
}
7278

73-
$dnArr = explode(';',$dn);
74-
if(count($dnArr) > 1)
75-
{
76-
$searchResult = $this->find($dnArr[1], $dnArr[0], '*');
77-
78-
if(count($searchResult))
79-
{
80-
$dn = $searchResult[0]['dn'];
79+
if($this->ldapBaseDn && $this->ldapSearchDn){
80+
$ldapUserProvider = new LdapUserProvider($this, $this->ldapBaseDn, $this->ldapSearchDn, $this->ldapSearchPassword, null, $this->ldapUidKey, $this->ldapFilter);
81+
$ldapUser = $ldapUserProvider->getUser($dn);
82+
if(count($ldapUser)){
83+
$dn = $ldapUser['dn'];
8184
}
8285
}
86+
8387
if (false === @ldap_bind($this->connection, $dn, $password)) {
8488
throw new ConnectionException(ldap_error($this->connection));
8589
}

src/Symfony/Component/Security/Core/User/LdapUserProvider.php

100644100755
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,7 @@ public function __construct(LdapClientInterface $ldap, $baseDn, $searchDn = null
5555
*/
5656
public function loadUserByUsername($username)
5757
{
58-
try {
59-
$this->ldap->bind($this->searchDn, $this->searchPassword);
60-
$username = $this->ldap->escape($username, '', LDAP_ESCAPE_FILTER);
61-
$query = str_replace('{username}', $username, $this->defaultSearch);
62-
$search = $this->ldap->find($this->baseDn, $query);
63-
} catch (ConnectionException $e) {
64-
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e);
65-
}
66-
67-
if (!$search) {
68-
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
69-
}
70-
71-
if ($search['count'] > 1) {
72-
throw new UsernameNotFoundException('More than one user found');
73-
}
74-
75-
$user = $search[0];
58+
$user = $this->getUser($username);
7659

7760
return $this->loadUser($username, $user);
7861
}
@@ -105,4 +88,29 @@ public function supportsClass($class)
10588
{
10689
return $class === 'Symfony\Component\Security\Core\User\User';
10790
}
91+
92+
/**
93+
* {@inheritdoc}
94+
*/
95+
private function getUser($username)
96+
{
97+
try {
98+
$this->ldap->bind($this->searchDn, $this->searchPassword);
99+
$username = $this->ldap->escape($username, '', LDAP_ESCAPE_FILTER);
100+
$query = str_replace('{username}', $username, $this->defaultSearch);
101+
$search = $this->ldap->find($this->baseDn, $query);
102+
} catch (ConnectionException $e) {
103+
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e);
104+
}
105+
106+
if (!$search) {
107+
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
108+
}
109+
110+
if ($search['count'] > 1) {
111+
throw new UsernameNotFoundException('More than one user found');
112+
}
113+
114+
return $search[0];
115+
}
108116
}

0 commit comments

Comments
 (0)
0