bug #21579 [Security] LdapUserProvider should not throw an exception if the UID key does not exist in an LDAP entry (csarrazi)

This PR was submitted for the 3.1 branch but it was merged into the 3.2 branch instead (closes #21579).

Discussion
----------

[Security] LdapUserProvider should not throw an exception if the UID key does not exist in an LDAP entry

| Q             | A
| ------------- | ---
| Branch?       | 3.1+
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21577
| License       | MIT
| Doc PR        |

This ticket should fix #21577, which was introduced by commit 6641b79d58

LdapUserProvider should not throw an exception if the uid key does not exist in the entry.

Commits
-------

ee4d9a70c1 [Security] LdapUserProvider should not throw an exception if the UID key does not exist in an LDAP entry
This commit is contained in:
Fabien Potencier 2017-02-11 09:51:37 +01:00
commit f376080b4b
2 changed files with 10 additions and 6 deletions

View File

@ -151,10 +151,7 @@ class LdapUserProviderTest extends \PHPUnit_Framework_TestCase
);
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\InvalidArgumentException
*/
public function testLoadUserByUsernameFailsIfEntryHasNoUidKeyAttribute()
public function testLoadUserByUsernameShouldNotFailIfEntryHasNoUidKeyAttribute()
{
$result = $this->getMockBuilder(CollectionInterface::class)->getMock();
$query = $this->getMockBuilder(QueryInterface::class)->getMock();

View File

@ -48,7 +48,7 @@ class LdapUserProvider implements UserProviderInterface
public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})', $passwordAttribute = null)
{
if (null === $uidKey) {
$uidKey = 'uid';
$uidKey = 'sAMAccountName';
}
$this->ldap = $ldap;
@ -87,7 +87,13 @@ class LdapUserProvider implements UserProviderInterface
}
$entry = $entries[0];
$username = $this->getAttributeValue($entry, $this->uidKey);
try {
if (null !== $this->uidKey) {
$username = $this->getAttributeValue($entry, $this->uidKey);
}
} catch (InvalidArgumentException $e) {
}
return $this->loadUser($username, $entry);
}
@ -123,6 +129,7 @@ class LdapUserProvider implements UserProviderInterface
protected function loadUser($username, Entry $entry)
{
$password = null;
if (null !== $this->passwordAttribute) {
$password = $this->getAttributeValue($entry, $this->passwordAttribute);
}