3

我在使用 LDAP 库和 Active Directory 时遇到了一些问题。如果参数返回超过 1000 个结果,我无法使用 LDAP 库进行搜索,因为 AD 的限制。此外,似乎没有办法使用 ZF2 LDAP 库对结果进行分页。in-built我知道如何分页,但如果存在,我宁愿使用 ZF2方法。

有什么我遗漏的还是我必须创建自己的方法来实现这一点?

PS 我已经查看了手册和代码,但我看不到任何方法来实现这一点。

4

2 回答 2

3

这就是我为解决这个问题所做的

/**
 * An LDAP search routine for finding information and returning paginated results
 *
 * 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  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, array $attributes = array(), $sort = null,
    $collectionClass = null, $timelimit = 0, $pageSize = 1000
)
{
    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();

    $results = new \ArrayIterator;

    Stdlib\ErrorHandler::start(E_WARNING);
    $cookie = '';
    do {
        ldap_control_paged_result($resource, $pageSize, true, $cookie);

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

        if ($sort !== null && is_string($sort)) {
            $isSorted = ldap_sort($resource, $result, $sort);

            if ($isSorted === false) {
                throw new Exception\LdapException($this, 'sorting: ' . $sort);
            }
        }

        $entries = new \ArrayIterator(ldap_get_entries($resource, $result));

        foreach ($entries as $e) {
            $results[] = $e;
        }

        ldap_control_paged_result_response($resource, $result, $cookie);
    } while($cookie !== null && $cookie != '');
    Stdlib\ErrorHandler::stop();

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

    return $results;
}

这个类/方法是为了扩展 Zend\Ldap\Ldap 类,它允许返回 1000 多个结果,但它不会以与 Ldap::search 方法相同的格式返回它们。

于 2013-06-03T13:44:56.940 回答
1

查看 Zend LDAP 库:Zend\Ldap\Ldap.php

/**
 * A global LDAP search routine for finding information.
 *
 * 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  integer                            $scope
 * @param  array                              $attributes
 * @param  string|null                        $sort
 * @param  string|null                        $collectionClass
 * @param  integer                            $sizelimit
 * @param  integer                            $timelimit
 * @return Collection
 * @throws Exception\LdapException
 */
public function search($filter, $basedn = null, $scope = self::SEARCH_SCOPE_SUB, array $attributes = array(),
                       $sort = null, $collectionClass = null, $sizelimit = 0, $timelimit = 0
)
{
     // ..
}

从签名中可以看出可以传入一个参数来限制结果集。

此方法的一个非常简单的包装器:

/**
 * Search for entries.
 * 
 * @parram  string
 * @param   string
 * @param   int
 * @param   int
 * @return  array
 */
public function search($filter, $basedn = null, $scope = \Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $sizelimit = 0)
{
    $attributes = array(); 
    $sort = null;
    $collectionClass = null;

    $result = $this->_getLdap()
        ->search($filter, $basedn, $scope, $attributes, $sort, $collectionClass, $sizelimit)
    ;

    return $result;
}

一个查找用户的简单示例可能是这样的:

/**
 * Get all Users..
 * 
 * @param   string
 * @return  \Zend\Ldap\Collection
 */
public function getUsers($baseDn, $sizelimit = 0)
{
    return $this->search('(objectCategory=user)', $baseDn, $sizelimit);
}

然后,您可以将 Iterator Adapter 与 Paginator 一起使用以使其正常工作:

use Zend\Paginator\Adapter\Iterator as IteratorAdapter;
// ..
$users = $this->_getMyLdapService()->getUsers();
$paginator = new Paginator(new IteratorAdapter($users));
于 2013-06-03T10:53:18.373 回答