ZF2 ldap分页

时间:2013-06-03 08:28:08

标签: php zend-framework2

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

是否有我缺少的东西,或者我是否必须创建自己的方法来实现这一目标?

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

2 个答案:

答案 0 :(得分: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方法相同的格式返回它们。

答案 1 :(得分: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);
}

然后,您可以使用带有Paginator的Iterator适配器使其正常工作:

use Zend\Paginator\Adapter\Iterator as IteratorAdapter;
// ..
$users = $this->_getMyLdapService()->getUsers();
$paginator = new Paginator(new IteratorAdapter($users));