AD通过LDAP - 如何从查询中返回所有祖先组?

Ada*_*nco 7 php java ldap active-directory

我通过LDAP(来自Java和PHP)查询Active Directory,以构建用户所属的所有组的列表.此列表必须包含所有包含用户直接成员的组的所有组(组织单位可选).例如:

User1是GroupA,GroupB和GroupC的成员.

GroupA是GroupD的成员.

我正在寻找一种方法来构建一个LDAP查询,它将立即返回GroupA ,GroupB ,GroupC GroupD.

我目前的实现如下,但我正在寻找一种更有效的方法来收集这些信息.

当前朴素实现(伪代码)

user = ldap_search('samaccountname=johndoe', baseDN);
allGroups = array();
foreach (user.getAttribute('memberOf') as groupDN) {
    allGroups.push(groupDN);
    allGroups = allGroups.merge(getAncestorGroups(groupDN));
}

function getAncestorGroups(groupDN) {
    allGroups = array();
    group = ldap_lookup(groupDN);
    parents = group.getAttribute('memberOf');
    foreach (parents as groupDN) {
        allGroups.push(groupDN);
        allGroups = allGroups.merge(getAncestorGroups(groupDN));
    }
    return allGroups;
}
Run Code Online (Sandbox Code Playgroud)

cde*_*zaq 8

Active Directory有一个特殊的搜索过滤器选项,允许它通过链接对象(如嵌套组)进行过滤.此功能在此处描述.

以下是如何检索组中所有用户的示例,包括嵌套组:

(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:={0}))
Run Code Online (Sandbox Code Playgroud)

其中{0}是父组的DN.


Sco*_*ell 1

当您在目录树中移动时,您需要映射目录树,以便您可以检查之前是否探索过 DN,某些 Active Directory 包含循环组包含项。所以你需要警惕它。

该解决方案也不需要递归。

在一些伪代码中

def getGroupsOfDN(userDN)

     groups = []
     groupsExplored = []
     groupsToExplore = []


     current = userDN
     groupsToExplore << userDN

     while(!groupsToExplore.empty?)


        ldapentry = ldap_lookup(current)

        if (!ldapentry.nil?)
           groups << current
           current_groups = ldapentry.getAttributes("memberOf")
           current_groups.each do |groupDN|
              if(groupsExplored.indexOf(groupDN) != -1)
                 groupsToExplore << groupDN
                 groupsExplored << groupDN
              end
           end
        end

        groupsToExplore.remove(current)
        if (!groupsToExplore.empty?)
           current = groupsToExplore.get(0)            
     end
     return groups
end
Run Code Online (Sandbox Code Playgroud)