import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InvalidNameException;
+import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
"com.sun.jndi.ldap.LdapCtxFactory");
connEnv.put(Context.PROVIDER_URL, getUri().toString());
connEnv.put("java.naming.ldap.attributes.binary",
- LdifName.userpassword.name());
+ LdifName.userPassword.name());
initialLdapContext = new InitialLdapContext(connEnv, null);
// StartTlsResponse tls = (StartTlsResponse) ctx
+ name);
return res;
} catch (NamingException e) {
- throw new UserDirectoryException("Cannot get role for " + name, e);
+ return null;
}
}
@Override
protected List<DirectoryUser> doGetRoles(Filter f) {
- // TODO Auto-generated method stub
try {
String searchFilter = f != null ? f.toString() : "(|("
+ objectClass + "=" + getUserObjectClass() + ")("
searchBase, searchFilter, searchControls);
ArrayList<DirectoryUser> res = new ArrayList<DirectoryUser>();
- while (results.hasMoreElements()) {
+ results: while (results.hasMoreElements()) {
SearchResult searchResult = results.next();
Attributes attrs = searchResult.getAttributes();
+ Attribute objectClassAttr = attrs.get(objectClass.name());
+ LdapName dn = toDn(searchBase, searchResult);
LdifUser role;
- if (attrs.get(objectClass.name()).contains(
- getGroupObjectClass()))
- role = new LdifGroup(this, toDn(searchBase, searchResult),
- attrs);
- else if (attrs.get(objectClass.name()).contains(
- getUserObjectClass()))
- role = new LdifUser(this, toDn(searchBase, searchResult),
- attrs);
- else
- throw new UserDirectoryException(
- "Unsupported LDAP type for "
- + searchResult.getName());
+ if (objectClassAttr.contains(getGroupObjectClass()))
+ role = new LdifGroup(this, dn, attrs);
+ else if (objectClassAttr.contains(getUserObjectClass()))
+ role = new LdifUser(this, dn, attrs);
+ else {
+ log.warn("Unsupported LDAP type for "
+ + searchResult.getName());
+ continue results;
+ }
res.add(role);
}
return res;
}
// add
for (LdapName dn : wc.getNewUsers().keySet()) {
- if (!entryExists(dn))
+ if (entryExists(dn))
throw new UserDirectoryException("User to create found "
+ dn);
}
// modify
for (LdapName dn : wc.getModifiedUsers().keySet()) {
if (!entryExists(dn))
- throw new UserDirectoryException("User to modify no found "
+ throw new UserDirectoryException("User to modify not found "
+ dn);
}
} catch (NamingException e) {
}
private boolean entryExists(LdapName dn) throws NamingException {
- return getLdapContext().getAttributes(dn).size() != 0;
+ try {
+ return getLdapContext().getAttributes(dn).size() != 0;
+ } catch (NameNotFoundException e) {
+ return false;
+ }
}
@Override