我有Windows身份验证,在IIS中设置了两个规则。 我正在将其与PHP站点一起使用。 我知道我可以用 $_SERVER['Remote_User'] 获得登录用户。 如何从授权规则中获取用户所在的组? 我希望能够根据他们所在的 AD 组在网站上显示特定内容。
用户所在的组
一个用户可以在多个组中,这些组可以位于其他组中。因此,您必须预料到,每个用户将有多个组。
IIS 不会将组成员身份发送到调用的 php 脚本。您需要自己查找组(例如,从互联网上使用此功能(:
<?php
function get_groups($user) {
// Active Directory server
$ldap_host = "ad.domain";
// Active Directory DN, base path for our querying user
$ldap_dn = "CN=Users,DC=ad,DC=domain";
// Active Directory user for querying
$query_user = "jane@".$ldap_host;
$password = "password1234!";
// Connect to AD
$ldap = ldap_connect($ldap_host) or die("Could not connect to LDAP");
ldap_bind($ldap,$query_user,$password) or die("Could not bind to LDAP");
// Search AD
$results = ldap_search($ldap,$ldap_dn,"(samaccountname=$user)",array("memberof","primarygroupid"));
$entries = ldap_get_entries($ldap, $results);
// No information found, bad user
if($entries['count'] == 0) return false;
// Get groups and primary group token
$output = $entries[0]['memberof'];
$token = $entries[0]['primarygroupid'][0];
// Remove extraneous first entry
array_shift($output);
// We need to look up the primary group, get list of all groups
$results2 = ldap_search($ldap,$ldap_dn,"(objectcategory=group)",array("distinguishedname","primarygrouptoken"));
$entries2 = ldap_get_entries($ldap, $results2);
// Remove extraneous first entry
array_shift($entries2);
// Loop through and find group with a matching primary group token
foreach($entries2 as $e) {
if($e['primarygrouptoken'][0] == $token) {
// Primary group found, add it to output array
$output[] = $e['distinguishedname'][0];
// Break loop
break;
}
}
return $output;
}
来源:https://samjlevy.com/php-ldap-membership/
要使函数正常工作,您需要将前 4 个变量设置为正确的值(取决于您的环境(。Active Directory 需要先进行身份验证,然后才能枚举用户/组列表(这是默认值(。因此,您将需要一些有效的凭据来执行查找。