Skip to content

Commit

Permalink
LdapConnection: Support unfolding multi value attributes
Browse files Browse the repository at this point in the history
refs #9772
  • Loading branch information
Johannes Meyer committed Sep 29, 2015
1 parent 876d281 commit 33c6f2e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
72 changes: 65 additions & 7 deletions library/Icinga/Protocol/Ldap/LdapConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,29 @@ protected function runQuery(LdapQuery $query, array $fields = null)
do {
$count += 1;
if (! $serverSorting || $offset === 0 || $offset < $count) {
$entries[ldap_get_dn($ds, $entry)] = $this->cleanupAttributes(
$row = $this->cleanupAttributes(
ldap_get_attributes($ds, $entry),
array_flip($fields)
array_flip($fields),
$query->getUnfoldAttribute()
);

if (is_array($row)) {
// TODO: Register the DN the same way as a section name in the ArrayDatasource!

$count -= 1;
foreach ($row as $additionalRow) {
$count += 1;
if (! $serverSorting || $offset === 0 || $offset < $count) {
$entries[] = $additionalRow;
}

if ($serverSorting && $limit > 0 && $limit === count($entries)) {
break;
}
}
} else {
$entries[ldap_get_dn($ds, $entry)] = $row;
}
}
} while ((! $serverSorting || $limit === 0 || $limit !== count($entries))
&& ($entry = ldap_next_entry($ds, $entry))
Expand Down Expand Up @@ -828,10 +847,29 @@ protected function runPagedQuery(LdapQuery $query, array $fields = null, $pageSi
do {
$count += 1;
if (! $serverSorting || $offset === 0 || $offset < $count) {
$entries[ldap_get_dn($ds, $entry)] = $this->cleanupAttributes(
$row = $this->cleanupAttributes(
ldap_get_attributes($ds, $entry),
array_flip($fields)
array_flip($fields),
$query->getUnfoldAttribute()
);

if (is_array($row)) {
// TODO: Register the DN the same way as a section name in the ArrayDatasource!

$count -= 1;
foreach ($row as $additionalRow) {
$count += 1;
if (! $serverSorting || $offset === 0 || $offset < $count) {
$entries[] = $additionalRow;
}

if ($serverSorting && $limit > 0 && $limit === count($entries)) {
break;
}
}
} else {
$entries[ldap_get_dn($ds, $entry)] = $row;
}
}
} while (
(! $serverSorting || $limit === 0 || $limit !== count($entries))
Expand Down Expand Up @@ -879,14 +917,16 @@ protected function runPagedQuery(LdapQuery $query, array $fields = null, $pageSi
/**
* Clean up the given attributes and return them as simple object
*
* Applies column aliases, aggregates multi-value attributes as array and sets null for each missing attribute.
* Applies column aliases, aggregates/unfolds multi-value attributes
* as array and sets null for each missing attribute.
*
* @param array $attributes
* @param array $requestedFields
* @param string $unfoldAttribute
*
* @return object
* @return object|array An array in case the object has been unfolded
*/
public function cleanupAttributes($attributes, array $requestedFields)
public function cleanupAttributes($attributes, array $requestedFields, $unfoldAttribute = null)
{
// In case the result contains attributes with a differing case than the requested fields, it is
// necessary to create another array to map attributes case insensitively to their requested counterparts.
Expand Down Expand Up @@ -927,6 +967,24 @@ public function cleanupAttributes($attributes, array $requestedFields)
}
}

if (
$unfoldAttribute !== null
&& isset($cleanedAttributes[$unfoldAttribute])
&& is_array($cleanedAttributes[$unfoldAttribute])
) {
$values = $cleanedAttributes[$unfoldAttribute];
unset($cleanedAttributes[$unfoldAttribute]);
$baseRow = (object) $cleanedAttributes;
$rows = array();
foreach ($values as $value) {
$row = clone $baseRow;
$row->{$unfoldAttribute} = $value;
$rows[] = $row;
}

return $rows;
}

return (object) $cleanedAttributes;
}

Expand Down
30 changes: 30 additions & 0 deletions library/Icinga/Protocol/Ldap/LdapQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class LdapQuery extends SimpleQuery
*/
protected $usePagedResults;

/**
* The name of the attribute used to unfold the result
*
* @var string
*/
protected $unfoldAttribute;

/**
* Initialize this query
*/
Expand Down Expand Up @@ -90,6 +97,29 @@ public function getUsePagedResults()
return $this->usePagedResults;
}

/**
* Set the attribute to be used to unfold the result
*
* @param string $attributeName
*
* @return $this
*/
public function setUnfoldAttribute($attributeName)
{
$this->unfoldAttribute = $attributeName;
return $this;
}

/**
* Return the attribute to use to unfold the result
*
* @return string
*/
public function getUnfoldAttribute()
{
return $this->unfoldAttribute;
}

/**
* Choose an objectClass and the columns you are interested in
*
Expand Down

0 comments on commit 33c6f2e

Please sign in to comment.