Skip to content

Commit

Permalink
[Security] LdapUserProvider should not throw an exception if the UID …
Browse files Browse the repository at this point in the history
…key does not exist in an LDAP entry
  • Loading branch information
csarrazi authored and fabpot committed Feb 11, 2017
1 parent fa0e27a commit 46f1fd9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 1 addition & 4 deletions Tests/User/LdapUserProviderTest.php
Expand Up @@ -151,10 +151,7 @@ public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry()
);
}

/**
* @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();
Expand Down
11 changes: 9 additions & 2 deletions User/LdapUserProvider.php
Expand Up @@ -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;
Expand Down Expand Up @@ -87,7 +87,13 @@ public function loadUserByUsername($username)
}

$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);
}
Expand Down Expand Up @@ -123,6 +129,7 @@ public function supportsClass($class)
protected function loadUser($username, Entry $entry)
{
$password = null;

if (null !== $this->passwordAttribute) {
$password = $this->getAttributeValue($entry, $this->passwordAttribute);
}
Expand Down

0 comments on commit 46f1fd9

Please sign in to comment.