LdapUserGroupBackend: Add support for custom query filters

refs #7343
This commit is contained in:
Johannes Meyer 2015-06-05 09:57:40 +02:00
parent 90d946f149
commit 3fd0d99db2
1 changed files with 81 additions and 4 deletions

View File

@ -4,6 +4,7 @@
namespace Icinga\Authentication\UserGroup; namespace Icinga\Authentication\UserGroup;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Protocol\Ldap\Expression;
use Icinga\Repository\LdapRepository; use Icinga\Repository\LdapRepository;
use Icinga\Repository\RepositoryQuery; use Icinga\Repository\RepositoryQuery;
use Icinga\User; use Icinga\User;
@ -59,6 +60,20 @@ class LdapUserGroupBackend /*extends LdapRepository*/ implements UserGroupBacken
*/ */
protected $groupMemberAttribute; protected $groupMemberAttribute;
/**
* The custom LDAP filter to apply on a user query
*
* @var string
*/
protected $userFilter;
/**
* The custom LDAP filter to apply on a group query
*
* @var string
*/
protected $groupFilter;
/** /**
* The columns which are not permitted to be queried * The columns which are not permitted to be queried
* *
@ -327,6 +342,58 @@ class LdapUserGroupBackend /*extends LdapRepository*/ implements UserGroupBacken
return $this->groupMemberAttribute; return $this->groupMemberAttribute;
} }
/**
* Set the custom LDAP filter to apply on a user query
*
* @param string $filter
*
* @return $this
*/
public function setUserFilter($filter)
{
if (($filter = trim($filter))) {
$this->userFilter = $filter;
}
return $this;
}
/**
* Return the custom LDAP filter to apply on a user query
*
* @return string
*/
public function getUserFilter()
{
return $this->userFilter;
}
/**
* Set the custom LDAP filter to apply on a group query
*
* @param string $filter
*
* @return $this
*/
public function setGroupFilter($filter)
{
if (($filter = trim($filter))) {
$this->groupFilter = $filter;
}
return $this;
}
/**
* Return the custom LDAP filter to apply on a group query
*
* @return string
*/
public function getGroupFilter()
{
return $this->groupFilter;
}
/** /**
* Return a new query for the given columns * Return a new query for the given columns
* *
@ -338,6 +405,11 @@ class LdapUserGroupBackend /*extends LdapRepository*/ implements UserGroupBacken
{ {
$query = parent::select($columns); $query = parent::select($columns);
$query->getQuery()->setBase($this->groupBaseDn); $query->getQuery()->setBase($this->groupBaseDn);
if ($this->groupFilter) {
// TODO(jom): This should differentiate between groups and their memberships
$query->getQuery()->where(new Expression($this->groupFilter));
}
return $query; return $query;
} }
@ -430,15 +502,17 @@ class LdapUserGroupBackend /*extends LdapRepository*/ implements UserGroupBacken
*/ */
public function getMemberships(User $user) public function getMemberships(User $user)
{ {
$userDn = $this->ds $userQuery = $this->ds
->select() ->select()
->from($this->userClass) ->from($this->userClass)
->where($this->userNameAttribute, $user->getUsername()) ->where($this->userNameAttribute, $user->getUsername())
->setBase($this->userBaseDn) ->setBase($this->userBaseDn)
->setUsePagedResults(false) ->setUsePagedResults(false);
->fetchDn(); if ($this->userFilter) {
$userQuery->where(new Expression($this->userFilter));
}
if ($userDn === null) { if (($userDn = $userQuery->fetchDn()) === null) {
return array(); return array();
} }
@ -447,6 +521,9 @@ class LdapUserGroupBackend /*extends LdapRepository*/ implements UserGroupBacken
->from($this->groupClass, array($this->groupNameAttribute)) ->from($this->groupClass, array($this->groupNameAttribute))
->where($this->groupMemberAttribute, $userDn) ->where($this->groupMemberAttribute, $userDn)
->setBase($this->groupBaseDn); ->setBase($this->groupBaseDn);
if ($this->groupFilter) {
$groupQuery->where(new Expression($this->groupFilter));
}
$groups = array(); $groups = array();
foreach ($groupQuery as $row) { foreach ($groupQuery as $row) {