2013-02-24 72 views

回答

0

下面的函數僅適用於Active Directory,因爲它生成成員資格屬性memberOf。如果我將找到一個通用LDAP的方式,我將添加它。

Entry userEntry = ldapConnection.getEntry(userDN); 

    List<Entry> entryList = new ArrayList(); 
    String[] memberValues = userEntry.getAttributeValues("memberOf"); 
    if (memberValues != null) { 
     DNEntrySource entrySource = new DNEntrySource(ldapConnection, memberValues); 
     while (true) { 
      Entry memberEntry = entrySource.nextEntry(); 
      if (memberEntry == null) { 
       break; 
      } 

      entryList.add(memberEntry); 
     } 
    } 

    return entryList; 
2

我有同樣的問題。然而,邁克爾的解決方案並不適合我,因爲它不能遞歸地工作。

顯然,有一個「神奇的」 AD查詢得到的所有組遞歸,見 Find Recursive Group Membership (Active Directory) using C#How do I filter an LDAP query for groups containing a specific user?

雖然我們的AD管理員已使用LDIFDE獲取所有組的命令行,我不能得到的查詢使用UnboundID。我們的CN裏面有空格,UnboundID增加了一個奇怪的'5c' - 但即使沒有空格的(技術)用戶,我也沒有得到任何結果。


無論如何,這裏是我的工作(和低效率)源代碼(使用谷歌番石榴)。我省略了一些方法和常量,我想你能猜出爲恆OBJECT_CLASS值:-)

/** 
    * Gets the groups for a user which is identified by the filter. 
    * @param filter The filter that identifies the user 
    * @return The groups for the user 
    */ 
    private List<String> getGroups(final Filter filter) { 
    LDAPConnection connection = null; 
    try { 
     connection = getConnection(); 

     String userDN = getUserDN(connection, filter); 
     if (userDN == null) { 
     return Collections.emptyList(); // No user found 
     } 

     Multimap<String, String> groupsByDN = ArrayListMultimap.create(); 
     getGroupsRecursively(connection, userDN, groupsByDN); 
     Set<String> groups = new HashSet<>(groupsByDN.values()); 
     for (String dn : groupsByDN.keySet()) { 
     // The user is not a group... 
     if (!dn.equals(userDN)) { 
      DN distinguishedName = new DN(dn); 
      for (RDN rdn : distinguishedName.getRDNs()) { 
      if (rdn.hasAttribute(CN)) { 
       groups.add(rdn.getAttributeValues()[0]); 
       break; 
      } 
      } 
     } 
     } 
     return new ArrayList<String>(groups); 
    } catch (LDAPException e) { 
     throw new RuntimeException("Can't search roles for " + filter, e); 
    } finally { 
     if (connection != null) { 
     connection.close(); 
     } 
    } 
    } 

    /** 
    * Since LDAP groups are stored as a tree, this fetches all groups for a user, starting with the user's DN and then 
    * fetching every group's groups and so on. 
    * @param connection The LDAP connection 
    * @param distinguishedName The distinguished name for which groups are searched 
    * @param groupsByDN Contains a distinguished name as key and its group name as result; keys are only searched once! 
    * @throws LDAPSearchException if the LDAP search fails 
    */ 
    private void getGroupsRecursively(final LDAPConnection connection, final String distinguishedName, 
     final Multimap<String, String> groupsByDN) throws LDAPSearchException { 
    if (!groupsByDN.containsKey(distinguishedName)) { 
     Filter groupFilter = Filter.createANDFilter(Filter.createEqualityFilter(OBJECT_CLASS, GROUP), 
      Filter.createEqualityFilter(MEMBER, distinguishedName)); 
     SearchResult result = getSearchResult(connection, groupFilter, CN); 
     List<SearchResultEntry> searchResults = result.getSearchEntries(); 
     for (SearchResultEntry searchResult : searchResults) { 
     String groupName = searchResult.getAttributeValue(CN); 
     groupsByDN.put(distinguishedName, groupName); 
     getGroupsRecursively(connection, searchResult.getDN(), groupsByDN); 
     } 
    } 
    }