Sumit Sahu
Sumit Sahu

Reputation: 21

How to get number of members in ldap group without using ismemberof?

I have a group called

dn: cn =myGroup,ou=manegedGroup,dc=example,dc=com in ldap and I want to get the number of members of this group without using ismemberof. I dont want any dn or cn, instead just need no of members.

Thanks in advance

Upvotes: 0

Views: 2628

Answers (1)

Roshith
Roshith

Reputation: 2175

Different LDAP servers have different Group attributes to denote members. MSAD uses the attribute "member" , OID uses "uniquemember" etc.

To get the count of members, search for the group entry "cn =myGroup" and get the size of the "member" attributes.

Pseudocode:

//Create initial dir context to dc=example,dc=com
env.put(DirContext.PROVIDER_URL, "ldap://<host>:<port>/dc=example,dc=com");
ctx = new InitialDirContext(env);

//Set returning attributes in search control
SearchControls controls = new SearchControls();
controls.setReturningAttributes(new String[] { "cn", "uniquemember"});
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

//Set search filter
String filter = "(cn=myGroup)";

//Search for the group under the correct OU passing filter and control
NamingEnumeration<SearchResult> searchResult =ctx.search("ou=manegedGroup",
                filter, controls);

//Get the size of the member attributes which is count of group members
while (searchResult.hasMoreElements()) {
    SearchResult ser = searchResult.next();
    Attributes attribs = ser.getAttributes();
    Attribute attrib = attribs.get("member");
    System.out.println("member count : "+attrib.size());
}   

Upvotes: 1

Related Questions