Do not throw an exception when the username does not exist

refs #6457
This commit is contained in:
Matthias Jentsch 2014-06-11 13:14:05 +02:00
parent 159d765f14
commit bca166c644
2 changed files with 17 additions and 12 deletions

View File

@ -94,16 +94,18 @@ class LdapUserBackend extends UserBackend
* @param User $user
* @param string $password
*
* @return bool|null
* @throws AuthenticationException
* @return bool True when the authentication was successful, false when the username or password was invalid
* @throws AuthenticationException When an error occurred during authentication
*/
public function authenticate(User $user, $password)
{
try {
return $this->conn->testCredentials(
$this->conn->fetchDN($this->createQuery($user->getUsername())),
$password
);
$userDn = $this->conn->fetchDN($this->createQuery($user->getUsername()));
if (!$userDn) {
// User does not exist
return false;
}
return $this->conn->testCredentials($userDn, $password);
} catch (Exception $e) {
throw new AuthenticationException(
sprintf(

View File

@ -207,16 +207,19 @@ class Connection
return true;
}
/**
* Fetch the distinguished name of the first result of the given query
*
* @param $query
* @param array $fields
*
* @return bool|String Returns the distinguished name, or false when the given query yields no results
*/
public function fetchDN($query, $fields = array())
{
$rows = $this->fetchAll($query, $fields);
if (count($rows) !== 1) {
throw new \Exception(
sprintf(
'Cannot fetch single DN for %s',
$query
)
);
return false;
}
return key($rows);
}