Retriving groups for a user using JNDI

P

Phani

HI All,
I have tried to retrive groups for a particular user using JNDI.
But i am getting only one group. Please some help help me to get all
the groups for a user. Here is my code.

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.ReferralException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class UserGroups {

static String group = "";

public static void main(String[] args) {
UserGroups userGroup = new UserGroups();
userGroup.getDepartmentName("phani");
}

public String getDepartmentName(String userId){

Properties env =
UserGroups.getProps("D:\\YUM\\properties\\MMLDAP.properties");
String deptName = "";
String base = env.getProperty("example.search.base");
base = "cn=" + userId + "," + base;
String filter = env.getProperty("example.search.filter");
SearchControls constraints = new SearchControls();
//String[] attrIDs = {"memberOf"};
//constraints.setReturningAttributes(attrIDs);
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
try {
DirContext ctx = new InitialDirContext(env);
NamingEnumeration results = ctx.search(base,filter,constraints);
deptName = UserGroups.printSearchEnums(results);
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
System.out.println("Search Failed "+e.getMessage());
}
return deptName;
}

//Loads properties from a file
static public Properties getProps(String fn){
Properties properties = new Properties();
File propsFile = new File(fn);
try {
FileInputStream in = new FileInputStream(propsFile);
properties.load(new BufferedInputStream(in));
in.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("getProps: "+e.getMessage());
}
return properties;
}

static String printSearchEnums(NamingEnumeration results) throws
NamingException {
if (results == null) {
System.out.println("Search Results are empty.");
return group;
}
try {
SearchResult si =(SearchResult) results.next();
//System.out.println(si.getName());
Attributes attrs = si.getAttributes();
if (attrs == null) {
System.out.println("No attributes");
return group;
}

NamingEnumeration ae = attrs.getAll();
while(ae.hasMoreElements()){
Attribute attr =(Attribute) ae.next();
String id = attr.getID();
if(id.equalsIgnoreCase("memberOf")){
group=(String)attr.get() + "," + group;
System.out.println(group);
}
}
} catch (ReferralException re) {
System.out.println(re);
do {
System.out.println(re.getReferralInfo());
} while (re.skipReferral());
}catch(Exception e){
}
return group;
}

}

Thanks and Regards,
Phani
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top