1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| Logger logger = Logger.getLogger(UtilisateursDao.class);
Hashtable<String, String> env = new Hashtable<String, String>();
String baseName = "ou=groups,dc=toto";
String server = "ldapServer";
String ldapDN = "ldapDN";
String ldapPassword = "ldapPassword";
// Set up the environment for creating the initial context
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, server);
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, ldapDN);
env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
ArrayList<String> listeIdUtilisateurs = new ArrayList<String>();
String filter = "(&(|(cn=admin-1)|(cn=admin-2)|(cn=admin-temp)))";
DirContext ctx;
DirContext ldapContext;
try {
ldapContext = new InitialDirContext(env);
ctx = new InitialDirContext(env);
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//ctls.setReturningAttributes(new String[] { "uniquemember"});
// String sortKey = "cn";
// ((InitialLdapContext) ctx).setRequestControls(new Control[] { new SortControl(sortKey,
// Control.CRITICAL) });
NamingEnumeration<SearchResult> resultEnum = ldapContext.search(baseName, filter , ctls);
while (resultEnum.hasMoreElements()) {
SearchResult result = resultEnum.next();
Attributes attrs = result.getAttributes();
logger.debug("groupe => "+result.getName());
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();)
{
Attribute atr = (Attribute) ae.next();
String attributeID = atr.getID();
Enumeration vals = atr.getAll();
while(vals.hasMoreElements()) {
String username = (String) vals.nextElement();
logger.debug("Username: " + username);
//Username: uid=LL0D76BX,ou=People,dc=toto
String kv = username.split(",")[0];
String uid = kv.split("=")[1];
ctx = new InitialDirContext(env);
DirContext o = (DirContext) ctx.lookup("uid=" + uid + ",ou=people,dc=toto");
String nom = "";
String prenom = "";
Attributes attributes = o.getAttributes("");
for (NamingEnumeration ne = attributes.getAll(); ne
.hasMoreElements();) {
Attribute attr2 = (Attribute) ne.next();
String Id = attr2.getID();
if(Id.equals("givenName") ){
for (NamingEnumeration values = attr2.getAll(); values.hasMore();) {
String thing2 = values.next().toString();
prenom = thing2;
}
}else if(Id.equals("sn")){
for (NamingEnumeration values = attr2.getAll(); values.hasMore();) {
String thing2 = values.next().toString();
nom = thing2;
}
}
}
if(!listeIdUtilisateurs.contains(uid)){
UtilisateurBean bean = new UtilisateurBean(nom, prenom, uid, 0);
listeIdUtilisateurs.add(uid);
}
ctx.close();
}
}
}
ldapContext.close();
} catch (AuthenticationException e) {
logger.error(utilisateur+" | "+"AuthenticationException",e);
} catch (NameNotFoundException e) {
logger.error(utilisateur+" | "+"NameNotFoundException",e);
} catch (NamingException e) {
logger.error(utilisateur+" | "+"NamingException",e);
} |
Partager