【发布时间】:2016-02-04 04:40:35
【问题描述】:
我有一个 LDAP 身份验证系统,可以从 AD 中查找和检索 memberOf 属性,但我不确定如何获取嵌套组成员资格。我知道 memberOf:1.2.840.113556.1.4.1941: extension 但不确定如何使其适应我的代码。我的工作代码如下(它只是不做嵌套组)。有经验的人可以帮忙吗?
在 CONFIG.PHP 中
$adbase = "some.domain.com";
$adtree = "OU=All Departments,DC=some,DC=domain,DC=com";
$group_admins = "Test App Admins";
$group_staff = "Test App Staff";
在 RUN.PHP 中
function extract_unit($string, $start, $end) {
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}
extract($_POST);
if($action == "logon") {
$ldap = ldap_connect($adbase);
$userID = "$user@$adbase";
$bind = @ldap_bind($ldap, $userID, $pass);
if($bind == true) {
// assign session variables
$_SESSION['username'] = $user;
// get group memberships
$results = ldap_search($ldap, $adtree, "(samaccountname=$user)",array("memberof", "mail"));
$entries = ldap_get_entries($ldap, $results);
// get group count (first entry) and group listings
$beat = 0; $str = ""; foreach($entries[0]['memberof'] as $temp) {
if($beat == 0) {
$count = $temp;
}
else {
$temp = extract_unit($temp, "CN=", ",OU=");
$str .= "$temp, ";
$groups[] = $temp;
}
$beat++;
}
// return the mail address, stackoverflow.com/questions/16224720/searching-for-email-address-ldap-active-directory
$mail = $entries[0]["mail"][0];
// assign session variables
$_SESSION['mail'] = $mail;
// groups defined in config.php
$client = "You are logged in as a client.";
if(in_array($group_admins, $groups)) {
$_SESSION['admin'] = true;
$client = "You are logged in as an administrator.";
}
else {
$_SESSION['admin'] = false;
}
// groups defined in config.php
if(in_array($group_staff, $groups)) {
$_SESSION['staff'] = true;
$client = "You are logged in as staff.";
}
else {
$_SESSION['staff'] = false;
}
// if it returns true user was found
print "<h2>Signed in as $user</h2>\n";
print "$client<br>\n";
print "<img src=\"./images/next.png\"> <a href=\"./?d=welcome\">Continue ...</a>\n";
print "<!-- count is $count -->";
print "<!-- groups are $str -->";
print "<!-- mail is $mail -->";
}
else {
print "<h2>Login attempt failed</h2>\n";
print "<img src=\"./images/logon.png\"> <a href=\"./?d=user/logon\">You may try again ...</a>\n";
}
}
【问题讨论】:
-
你看过stojg.se/blog/…吗? (在我最喜欢的搜索引擎中查询
memberOf:1.2.840.113556.1.4.1941 php的第三个结果 -
我有,实际上,我只是尝试调整我的代码以适应,但我无法让它工作。具体来说,我让我的代码返回用户帐户的完整 DN 路径,尝试运行该函数,但似乎每次都失败。