我无法让它工作,所以我建立了自己的。在具有多个组的 OU 中搜索的结果:
Name Members
Depth Object Path
groupname_users
1 user2
1 user1
groupname_admins
1 user2
groupname_group1
1 groupname_users
2 user2 groupname_users =>
2 user1 groupname_users =>
groupname_groups
1 groupname_group1
2 groupname_users groupname_group1 =>
3 user2 groupname_group1 => groupname_users =>
3 user1 groupname_group1 => groupname_users =>
使用以下函数创建:
public function ldap_get_groups($ld_prim_group){
$master = array();
function ldap_get_group_data($group,$con,$depth,$path,$parent) {
if ($parentData=ldap_read($con,$group, "(|(objectclass=person)(objectclass=groupOfNames))", array('cn','dn','member','objectclass'))){
$entry = ldap_get_entries($con, $parentData); #get all info from query
if($entry['count']>0){ //only if object person / group, will alway return 1 array!
$obj_group['objectclass']=$entry[0]['objectclass'][0];
$obj_group['cn']=$entry[0]['cn'][0];
$obj_group['dn']=$entry[0]['dn'];
$obj_group['memberCount']=$entry['0']['member']['count'] ?? 0;
unset($entry['0']['member']['count']); //remove awefull count key
$obj_group['member']=$entry[0]['member'] ?? null; //if entry has members than copy to object.
$obj_group['depth']=$depth;
$obj_group['path']=$path;
$parent['dn'] ? $obj_group['parentDN']= $parent['dn']:null; //create parentDN if parent['dn'] exist
$parent['cn'] ? $obj_group['parentCN']= $parent['cn']:null;
global $master;
if($depth ==0){ //modify self
$master[$depth][$obj_group['cn']]=$obj_group;
}
if($depth ==1){ //modify childs
//dept 1 = all users / groups under dept 0
$obj_group['top']=$parent['cn'];
$path .= $obj_group['cn'] . " => " ;
$master[1][$obj_group['top']][$obj_group['cn']]=$obj_group;
}
if($depth >1) { //modify grandchilds
//dept 2,3,4.. = all users / groups under dept 1
$obj_group['top']=$parent['top'];
$path .= $obj_group['cn'] . " => " ;
$master[1][$obj_group['top']][$obj_group['cn']]=$obj_group;
}
if($obj_group['objectclass']=='groupOfNames'){
#modify for next round
$depth+=1;
foreach($obj_group['member'] as $key=>$value){
ldap_get_group_data($value,$con,$depth,$path,$obj_group);
}
}
}
}
else {
#invalid primary group
return null;
}
}
$sr=ldap_search($this->cnx, $ld_prim_group, "(!(objectclass=organizationalUnit))", array('dn'));
$info = ldap_get_entries($this->cnx, $sr);
unset($info['count']);
foreach($info as $k=>$v){
if (ldap_get_group_data($v['dn'],$this->cnx,$depth=0,$path="",$parent=null)){
}
}
global $master;
return $master;
}
ldap_get_groups("DN_OF_GROUP_OU");
将返回
Array
(
[0] => Array
(
[groupname_users] => Array
(
[objectclass] => groupOfNames
[cn] => groupname_users
[dn] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[memberCount] => 2
[member] => Array
(
[0] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
[1] => cn=user1,ou=users,ou=domain.local,dc=domain,dc=local
)
[depth] => 0
[path] =>
)
[groupname_admins] => Array
(
[objectclass] => groupOfNames
[cn] => groupname_admins
[dn] => cn=groupname_admins,ou=groups,ou=domain.local,dc=domain,dc=local
[memberCount] => 2
[member] => Array
(
[0] => cn=admin,dc=domain,dc=local
[1] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
)
[depth] => 0
[path] =>
)
[groupname_general] => Array
(
[objectclass] => groupOfNames
[cn] => groupname_general
[dn] => cn=groupname_general,ou=groups,ou=domain.local,dc=domain,dc=local
[memberCount] => 1
[member] => Array
(
[0] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
)
[depth] => 0
[path] =>
)
[groupname_groups] => Array
(
[objectclass] => groupOfNames
[cn] => groupname_groups
[dn] => cn=groupname_groups,ou=groups,ou=domain.local,dc=domain,dc=local
[memberCount] => 1
[member] => Array
(
[0] => cn=groupname_general,ou=groups,ou=domain.local,dc=domain,dc=local
)
[depth] => 0
[path] =>
)
)
[1] => Array
(
[groupname_users] => Array
(
[user2] => Array
(
[objectclass] => inetOrgPerson
[cn] => user2
[dn] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
[memberCount] => 0
[member] =>
[depth] => 1
[path] =>
[parentDN] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_users
[top] => groupname_users
)
[user1] => Array
(
[objectclass] => inetOrgPerson
[cn] => user1
[dn] => cn=user1,ou=users,ou=domain.local,dc=domain,dc=local
[memberCount] => 0
[member] =>
[depth] => 1
[path] =>
[parentDN] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_users
[top] => groupname_users
)
)
[groupname_admins] => Array
(
[user2] => Array
(
[objectclass] => inetOrgPerson
[cn] => user2
[dn] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
[memberCount] => 0
[member] =>
[depth] => 1
[path] =>
[parentDN] => cn=groupname_admins,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_admins
[top] => groupname_admins
)
)
[groupname_general] => Array
(
[groupname_users] => Array
(
[objectclass] => groupOfNames
[cn] => groupname_users
[dn] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[memberCount] => 2
[member] => Array
(
[0] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
[1] => cn=user1,ou=users,ou=domain.local,dc=domain,dc=local
)
[depth] => 1
[path] =>
[parentDN] => cn=groupname_general,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_general
[top] => groupname_general
)
[user2] => Array
(
[objectclass] => inetOrgPerson
[cn] => user2
[dn] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
[memberCount] => 0
[member] =>
[depth] => 2
[path] => groupname_users =>
[parentDN] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_users
[top] => groupname_general
)
[user1] => Array
(
[objectclass] => inetOrgPerson
[cn] => user1
[dn] => cn=user1,ou=users,ou=domain.local,dc=domain,dc=local
[memberCount] => 0
[member] =>
[depth] => 2
[path] => groupname_users =>
[parentDN] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_users
[top] => groupname_general
)
)
[groupname_groups] => Array
(
[groupname_general] => Array
(
[objectclass] => groupOfNames
[cn] => groupname_general
[dn] => cn=groupname_general,ou=groups,ou=domain.local,dc=domain,dc=local
[memberCount] => 1
[member] => Array
(
[0] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
)
[depth] => 1
[path] =>
[parentDN] => cn=groupname_groups,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_groups
[top] => groupname_groups
)
[groupname_users] => Array
(
[objectclass] => groupOfNames
[cn] => groupname_users
[dn] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[memberCount] => 2
[member] => Array
(
[0] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
[1] => cn=user1,ou=users,ou=domain.local,dc=domain,dc=local
)
[depth] => 2
[path] => groupname_general =>
[parentDN] => cn=groupname_general,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_general
[top] => groupname_groups
)
[user2] => Array
(
[objectclass] => inetOrgPerson
[cn] => user2
[dn] => cn=user2,ou=users,ou=domain.local,dc=domain,dc=local
[memberCount] => 0
[member] =>
[depth] => 3
[path] => groupname_general => groupname_users =>
[parentDN] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_users
[top] => groupname_groups
)
[user1] => Array
(
[objectclass] => inetOrgPerson
[cn] => user1
[dn] => cn=user1,ou=users,ou=domain.local,dc=domain,dc=local
[memberCount] => 0
[member] =>
[depth] => 3
[path] => groupname_general => groupname_users =>
[parentDN] => cn=groupname_users,ou=groups,ou=domain.local,dc=domain,dc=local
[parentCN] => groupname_users
[top] => groupname_groups
)
)
)
)