Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Added Pagination while searching users. #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,98 @@ public function search(
return $this->createCollection($iterator, $collectionClass);
}

/**
* An LDAP search routine for finding information and returning paginated results
* https://stackoverflow.com/questions/16892693/zf2-ldap-pagination
*
* Options can be either passed as single parameters according to the
* method signature or as an array with one or more of the following keys
* - filter
* - baseDn
* - scope
* - attributes
* - sort
* - collectionClass
* - sizelimit
* - timelimit
*
* @param string|Filter\AbstractFilter|array $filter
* @param string|Dn|null $basedn
* @param int $scope
* @param array $attributes
* @param string|null $sort
* @param string|null $collectionClass
* @param integer $timelimit
* @param integer $pageSize
* @return Array
* @throws Exception\LdapException
*/
public function multiPageSearch(
$filter, $basedn = null, $scope, array $attributes = array(), $sort = null,
$collectionClass = null, $timelimit = 0, $pageSize = 10000
)
{
if (is_array($filter)) {
$options = array_change_key_case($filter, CASE_LOWER);
foreach ($options as $key => $value) {
switch ($key) {
case 'filter':
case 'basedn':
case 'scope':
case 'sort':
$$key = $value;
break;
case 'attributes':
if (is_array($value)) {
$attributes = $value;
}
break;
case 'collectionclass':
$collectionClass = $value;
break;
case 'sizelimit':
case 'timelimit':
$$key = (int) $value;
break;
}
}
}

if ($basedn === null) {
$basedn = $this->getBaseDn();
} elseif ($basedn instanceof Dn) {
$basedn = $basedn->toString();
}

if ($filter instanceof Filter\AbstractFilter) {
$filter = $filter->toString();
}

$resource = $this->getResource();
ErrorHandler::start(E_WARNING);
$cookie = '';
$results = [];
do {
ldap_control_paged_result($resource, $pageSize, true, $cookie);

$result = ldap_search($resource, $basedn, $filter,
$attributes
);

foreach (ldap_get_entries($resource, $result) as $item){
array_push($results, (array)$item);
}

ldap_control_paged_result_response($resource, $result, $cookie);
} while ($cookie);
ErrorHandler::stop();

if (count($results) == 0) {
throw new Exception\LdapException($this, 'searching: ' . $filter);
}
return $results;
}

/**
* Extension point for collection creation
*
Expand Down