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

View File

@ -207,16 +207,19 @@ class Connection
return true; 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()) public function fetchDN($query, $fields = array())
{ {
$rows = $this->fetchAll($query, $fields); $rows = $this->fetchAll($query, $fields);
if (count($rows) !== 1) { if (count($rows) !== 1) {
throw new \Exception( return false;
sprintf(
'Cannot fetch single DN for %s',
$query
)
);
} }
return key($rows); return key($rows);
} }