Respect base_dn in LdapUserBackend

This commit is contained in:
Matthias Jentsch 2014-10-14 14:37:21 +02:00
parent 448991eec5
commit 9a9aa84e23
1 changed files with 21 additions and 24 deletions

View File

@ -30,12 +30,25 @@ class LdapUserBackend extends UserBackend
public function __construct(Connection $conn, $userClass, $userNameAttribute, $baseDn, $groupOptions = null) public function __construct(Connection $conn, $userClass, $userNameAttribute, $baseDn, $groupOptions = null)
{ {
$this->conn = $conn; $this->conn = $conn;
$this->baseDn = $baseDn; $this->baseDn = trim($baseDn) !== '' ? $baseDn : $conn->getDN();
$this->userClass = $userClass; $this->userClass = $userClass;
$this->userNameAttribute = $userNameAttribute; $this->userNameAttribute = $userNameAttribute;
$this->groupOptions = $groupOptions; $this->groupOptions = $groupOptions;
} }
/**
* @return \Icinga\Protocol\Ldap\Query
*/
protected function selectUsers()
{
return $this->conn->select()->setBase($this->baseDn)->from(
$this->userClass,
array(
$this->userNameAttribute
)
);
}
/** /**
* Create query * Create query
* *
@ -43,14 +56,9 @@ class LdapUserBackend extends UserBackend
* *
* @return \Icinga\Protocol\Ldap\Query * @return \Icinga\Protocol\Ldap\Query
**/ **/
protected function createQuery($username) protected function selectUser($username)
{ {
return $this->conn->select() return $this->selectUsers()->where(
->from(
$this->userClass,
array($this->userNameAttribute)
)
->where(
$this->userNameAttribute, $this->userNameAttribute,
str_replace('*', '', $username) str_replace('*', '', $username)
); );
@ -70,7 +78,7 @@ class LdapUserBackend extends UserBackend
*/ */
public function assertAuthenticationPossible() public function assertAuthenticationPossible()
{ {
$q = $this->conn->select()->from($this->userClass); $q = $this->conn->select()->setBase($this->baseDn)->from($this->userClass);
$result = $q->fetchRow(); $result = $q->fetchRow();
if (! isset($result)) { if (! isset($result)) {
throw new AuthenticationException( throw new AuthenticationException(
@ -137,7 +145,7 @@ class LdapUserBackend extends UserBackend
public function hasUser(User $user) public function hasUser(User $user)
{ {
$username = $user->getUsername(); $username = $user->getUsername();
return $this->conn->fetchOne($this->createQuery($username)) === $username; return $this->conn->fetchOne($this->selectUser($username)) === $username;
} }
/** /**
@ -170,7 +178,7 @@ class LdapUserBackend extends UserBackend
return false; return false;
} }
try { try {
$userDn = $this->conn->fetchDN($this->createQuery($user->getUsername())); $userDn = $this->conn->fetchDN($this->selectUser($user->getUsername()));
$authenticated = $this->conn->testCredentials( $authenticated = $this->conn->testCredentials(
$userDn, $userDn,
$password $password
@ -197,15 +205,7 @@ class LdapUserBackend extends UserBackend
*/ */
public function count() public function count()
{ {
return $this->conn->count($this->selectUsers());
return $this->conn->count(
$this->conn->select()->from(
$this->userClass,
array(
$this->userNameAttribute
)
)
);
} }
/** /**
@ -215,13 +215,10 @@ class LdapUserBackend extends UserBackend
*/ */
public function listUsers() public function listUsers()
{ {
$query = $this->conn->select()->from($this->userClass, array($this->userNameAttribute));
$users = array(); $users = array();
foreach ($query->fetchAll() as $row) { foreach ($this->selectUsers()->fetchAll() as $row) {
$users[] = $row->{$this->userNameAttribute}; $users[] = $row->{$this->userNameAttribute};
} }
return $users; return $users;
} }
} }