2009-06-24 99 views
7

我通過LDAP(來自Java和PHP)查詢Active Directory以構建用戶所屬的所有組的列表。該列表必須包含所有至少包含用戶直接參與的組的所有組(組織單位可選)。例如:AD via LDAP - 如何返回查詢中的所有祖先組?

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; 
} 

回答

2

您需要映射的目錄樹,你通過它移動,這樣你就可以檢查,看看是否您以前探索出了DN,一些活動目錄包含環狀組包含物。所以你需要警惕它。

該解決方案也不需要遞歸。

在一些僞代碼

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 
7

Active Directory中有一個特殊的搜索過濾選項,允許其通過連鎖目標篩選,像嵌套組。該能力被描述爲here

下面是如何檢索所有用戶的組,包括嵌套組中的一個示例:

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

其中{0}是父組的DN。