【问题标题】:AD via LDAP - How can I return all ancestor groups from a query?AD via LDAP - 如何从查询中返回所有祖先组?
【发布时间】:2009-06-24 16:44:37
【问题描述】:

我正在通过 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;
}

【问题讨论】:

    标签: java php active-directory ldap


    【解决方案1】:

    Active Directory 有一个特殊的搜索过滤器选项,允许它过滤链接的对象,例如嵌套组。该功能描述为here

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

    (&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:={0}))
    

    其中{0} 是父组的DN。

    【讨论】:

      【解决方案2】:

      您需要在浏览目录树时映射目录树,以便检查您之前是否浏览过 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
      

      【讨论】:

        猜你喜欢
        • 2017-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-23
        相关资源
        最近更新 更多