Reputation: 91
I have to extract the email id of all members of a group from LDAP. My group name is reportMember (String groupName=reportMember;) and I want to get all user of that group along their email address. so I wrote a code to get it. I provide a query as: "String searchFilter="cn="+groupName;" this query gave me the names of all member of that group as:
member1 :CN=Alex,OU=InfoWorker,OU=People,DC=abc,DC=xyz,DC=com
member2 :............... and so on.
But through this query I won't be able to get the email address of those members my code is below.
Many thanks in advance.
Code:
public List<LDAPUser> searchGroupDetails(String currentUserName, String groupName) {
List<LDAPUser> LDAPUsers=new ArrayList<LDAPUser>();
ArrayList<String> listOfMembersInGroup= new ArrayList<String>();
int maxResults=Integer.parseInt("2000");
DirContext dirSearchContext = utilusr.getLDAPDirContext().get(currentUserName);
String searchbase = "DC=abc,DC=xyz,DC=com";
String searchFilter="cn="+groupName;
String member="";
try{
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtls.setReturningAttributes(returnAttributes);
try{
NamingEnumeration<?> users = dirSearchContext.search(searchbase, searchFilter, searchCtls);
if(users.hasMoreElements() == false){
System.out.println("Not find any object with this filter " + searchFilter + " and searchBase " + searchbase);
}
int k = 0;
String attValue = "";
while (users.hasMoreElements()){
if(k >= maxResults)
break;
SearchResult sr = (SearchResult)users.next();
Attributes attrs = sr.getAttributes();
if (attrs.size() == 0){
System.out.println("Could not find attribute " + returnAttributes[0] + " for this object.");
}else{
try{
//-- Code to extract members of a given group Start.
for (NamingEnumeration<?> ae = attrs.getAll();ae.hasMore();){
Attribute attr = (Attribute)ae.next();
String id = attr.getID();
for (NamingEnumeration<?> e = attr.getAll();e.hasMore();){
attValue = (String)e.next();
if(id.equalsIgnoreCase("member")){
member = attValue;
System.out.println("member :"+member);
String memberName=member.substring(member.indexOf("=")+1, member.indexOf(","));
listOfMembersInGroup.add(memberName);
}
//-- Code to extract members of a given group Ends.
else
{
System.out.println("empty");
}
}
}
}catch(NamingException e){
e.printStackTrace();
}
}
k++;
}
}catch (NamingException e){
e.printStackTrace();
}
dirSearchContext=null;
}catch (Exception e){
e.printStackTrace();
}
}
Upvotes: 0
Views: 1948
Reputation: 91
public List<LDAPUser> search_MemberAndEmailAddresFromGroup(String currentUserName, String groupName) {
UserSingleton utilusr = UserSingleton.getInstance();
List<LDAPUser> LDAPUsers=new ArrayList<LDAPUser>();
ArrayList<String> listOfMembersInGroup= new ArrayList<String>();
int maxResults=Integer.parseInt("2000");
DirContext dirSearchContext = utilusr.getLDAPDirContext().get(currentUserName);//TODO call the singleton to get this object
String searchbase = "DC=ABC,DC=LMN,DC=XYZ";
String searchFilter="cn="+groupName;
String member="";
try{
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtls.setReturningAttributes(returnAttributes);
try{
NamingEnumeration<?> membersOfGroup = dirSearchContext.search(searchbase, searchFilter, searchCtls);
if(membersOfGroup.hasMoreElements() == false){
System.out.println("Not find any object/members with this filter " + searchFilter + " and searchBase " + searchbase);
}
int k = 0;
String attValue = "";
while (membersOfGroup.hasMoreElements()){
if(k >= maxResults)
break;
SearchResult sr = (SearchResult)membersOfGroup.next();
Attributes attrs = sr.getAttributes();
if (attrs.size() == 0){
System.out.println("Could not find attribute " + returnAttributes[0] + " for this object.");
}else{
try{
//-- Code to extract members of a given group Start.
for (NamingEnumeration<?> ae = attrs.getAll();ae.hasMore();){
Attribute attr = (Attribute)ae.next();
String id = attr.getID();
for (NamingEnumeration<?> e = attr.getAll();e.hasMore();){
attValue = (String)e.next();
if(id.equalsIgnoreCase("member")){
member = attValue;
String memberName=member.substring(member.indexOf("=")+1, member.indexOf(","));
System.out.println("member Name is: "+memberName);
listOfMembersInGroup.add(memberName);//-- adding member name in a list
}
//-- Code to extract members of a given group Ends.
else
{
System.out.println("empty");
}
}
}
}catch(NamingException e){
e.printStackTrace();
System.out.println("Problem listing membership:"+e);
}
}
k++;
}
//--After getting member name of a group extracting their email_Id code start
if((listOfMembersInGroup!=null)||(!(listOfMembersInGroup.isEmpty()))){
for(String name:listOfMembersInGroup){
searchFilter="(name="+name.replace(" ", "\\ ")+")";
System.out.println("Search Filter to find email address of a member: "+searchFilter);
queryRes = dirSearchContext.search(searchbase, searchFilter, searchCtls);
while (queryRes.hasMoreElements()){
SearchResult srchResObj = (SearchResult) queryRes.next();
Attributes attributesList = srchResObj.getAttributes();
if (attributesList != null) {
if(attributesList.get("mail") !=null && attributesList.get("memberOf")!=null){
if(attributesList.get("memberOf").toString().split(":")[1].trim().contains(groupName)){
System.out.println(attributesList.get("mail").toString().split(":")[1].trim());
LDAPUser user=new LDAPUser();
user.setEmail(attributesList.get("mail").toString().split(":")[1].trim());
user.setUserName(attributesList.get("displayname").toString().split(":")[1].trim());
LDAPUsers.add(user);
}
}
}
}
}
}
//--After getting member name of a group extracting their email_Id code End
}catch (NamingException e){
e.printStackTrace();
System.out.println("Problem searching directory: "+e);
}
}catch (Exception e){
e.printStackTrace();
System.out.println("Exception while fetching the users from LDAP::"+e);
}
return LDAPUsers;
}
Upvotes: 0
Reputation: 10986
Assume if Microsoft Active Directory (or is memberOF is available as you do not disclose)
Something similar to this should work (Of course with your environment mods)
String[] attrIDs = {"mail"};
String groupName = "reportMember";
String groupDN = "CN=Groups,DC=abc,DC=xyz,DC=com"; // Where groups are in LDAP
Attributes matchAttrs = new BasicAttributes(true); // ignore case
matchAttrs.put(new BasicAttribute("memberOf", "CN=" + groupName + ","+ groupDN)); // finds group by CN Value
String searchFilter = "(&(objectCategory=Person)(objectClass=User)(memberOf=" + groupDN + "))";
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase, searchFilter, searchControls, attrIDs);
while (results.hasMore()) {
SearchResult result = results.next();
Attributes attributes = result.getAttributes();
Attribute email = attributes.get("mail");
System.out.println(email);
//get/iterate the values of the attribute
}
System.out.println("DONE");
Upvotes: 0