通过ldap从Active Directory中获取不同的城市列表


get distinct list of cities from Active Directory via ldap

我想为搜索表单创建一个城市的选择列表。该列表应该只包括在我将要搜索的Active Directory中Usersl字段中输入的城市。据我所知,我需要从我的AD中获取所有Users,并以这种方式生成城市列表。(然后我会缓存列表)。由于这将是一项繁重的操作,而且我的AD搜索结果上限为1000,我有一种感觉,我可能无法获得所有的Users,因此可能会错过一些城市。

有更好的方法吗?我使用PHP,目前使用ldap_searchldap_get_values

您需要执行分页搜索操作,以返回比每个查询默认1000限制更多的数据。分页搜索的工作原理是,每次查询仍能获得1000个项目,但服务器会记住您的搜索停止位置,并向您发送cookie,您可以在以后的搜索请求中使用此cookie。然后,服务器将返回另外1000个项目,依此类推,直到服务器返回空cookie->结果集完成。

这通常需要一些代码才能将所有事情都做好。我建议,如果您计划对ldap做更多的工作,请查看一些成熟的php库,如adLDAP或AD-X。

至于搜索查询本身,我建议采用以下方式:

  1. 您将搜索所有已填充l属性的用户:(&(objectcategory=person)(l=*))(这也将包括联系人对象-如果不需要,请将第一部分替换为objectclass=user
  2. 您可以获得l属性的值,并对集合进行uniquification,也许可以使用array_unique函数

如果你曾经决定使用AD-X,这个任务是一个简单的几行。

$link = new ADX'Core'Link( 'domain.com' ); // open connection
$link->bind ('username', 'pass'); // Authenticate
// The Task is a configuration object for your search
// request - let's configure it
$task = new ADX'Core'Task( ADX'Enums'Operation::OpSearch, $link );
$task->attributes( 'l' ); // Get these attributes
$task->filter( '(&(objectcategory=person)(l=*))' ); // use this search filter
// Do the search using paged searching, returning
// ALL matching objects
$result = $task->run_paged();
print_r( $result->unique( 'l' ); // Get all unique 'l' values from the set

如果你不想使用库,那么你肯定应该看看这两个组成分页结果搜索功能的函数:

  • ldap_control_paged_result
  • ldap_control_paged_sult_response

我希望这能有所帮助!祝你好运