I have created an application which uses LDAP for authentication. I need to find out all the group name in which the User is assign to. Is there any way to find out that. I have written code but somehow it return only one group name that to randomly.
Below is my code to get all memberOf the users.
private class UserAttributesMapper implements AttributesMapper {
@Override
public Object mapFromAttributes(Attributes attributes) throws NamingException {
LdapUser user = new LdapUser();
user.setCn((String)attributes.get("cn").get());
user.setMemberOf((String)attributes.get("memberOf").get());
/*String member = (String)attributes.get("memberOf").get();
int length = attributes.get("memberOf").size();
if(member != null){
for(int i = 0;i<= length; i++){
user.setMemberOf(member);
}
}*/
//user.setMemberOf(attributes.get("memberOf").getID());
user.setsAMAccountName((String)attributes.get("sAMAccountName").get());
return user;
}
}
This class is used to set attribute and return attribute for the user.
It was pretty much easier than I thought. Please find the code below. In this you just need to Enumerate the all the memberOf in for loop and assign int in List, then return the List along with all other attribute.
Below is the code.
private class UserAttributesMapper implements AttributesMapper {
@Override
public Object mapFromAttributes(Attributes attributes) throws NamingException {
LdapUser user = new LdapUser();
user.setCn((String)attributes.get("cn").get());
List<String> memberOf = new ArrayList<String>();
for(Enumeration vals = attributes.get("memberOf").getAll(); vals.hasMoreElements();){
memberOf.add((String)vals.nextElement());
}
user.setMemberOf(memberOf);
user.setsAMAccountName((String)attributes.get("sAMAccountName").get());
user.setMail((String)attributes.get("mail").get());
return user;
}
}