来自组的LDAP PHP数据

时间:2017-05-29 12:41:45

标签: php ldap

我是使用LDAP的新手,所以也许我不是在寻找合适的东西,或者这是不可能的。

我在Active Directory中工作,其中有学生成为他们班级的小组。现在我需要来自某个班级的所有学生的电子邮件地址。我确实通过LDAP连接到Active Directory,但由于我根本不知道如何处理这个问题,因此我没有任何代码可以显示。

2 个答案:

答案 0 :(得分:0)

由于它是Active Directory,因此您必须先使用服务帐户进行绑定,然后才能搜索目录。成功绑定后,您可以检索名为" ICS-1"的组中所有用户的电子邮件地址。像这样:

// $conn = ldap_connect()
// ldap_bind()
$basedn = "dc=rug,dc=nl"
$classname = "ICS-1"
$filter = "(&(objectClass=groupOfNames)(cn=" . $classname ."))"
$search = ldap_search($conn, $basedn, $filter, array("dn"))
$groupdn = $search[0]["dn"]
$filter = "(&(objectClass=user)(memberOf=" . $groupdn ."))"
$search = ldap_search($conn, $basedn, $filter, array("mail"))

$basedn是存储您的群组和用户的命名上下文。请查阅available documentation以获取ldap_ functions的正确语法。

答案 1 :(得分:0)

你需要识别你的dn类,如下所示:

cn=classname,ou=classes,dc=domain,dc=com

获得此信息后,检索邮件的最有效方法是:

// retrieve the members of this group : 
$result = ldap_read ( $AD , "<dn of the class>" , (objectClass=*) , ["member"]);
// Get the entry
$entry = ldap_get_entries ( $AD ,$result );
/* The entry should be an array like this : 
$entry = [ 
   "count" => 1, 
   "0" => [
     "dn" => "<dn of the class>",
     "member" => [...]
   ]
]
*/
// After that you can iterate through the members to read each user entry
$mails = [];
for ($i=0; $i< $entry[0]["member"]["count"]; $i++) {
   $studentDN = $entry[0]["member"][$i];
   $r = ldap_read ( $AD , $studentDN , (objectClass=*) , ["mail"]);
   $e = ldap_get_entries ( $AD ,$r );
   // $e is structured similarly to the previous $entry so the mail is at : 
   $mails[] = $e[0]["mail"][0];
}

我不太了解Active Directory,因此您应该仔细检查属性名称以满足您的需求,但总的来说,当您知道组的DN时,它是循环访问LDAP目录的最快/最有效的方法

由于LDAP目录针对快速阅读进行了优化,因此服务器可以更轻松地逐个读取每个条目以获取属性,而不是搜索所有用户到匹配属性,然后获取所有邮件和一气呵成地归还了一切。