SearchScope scope = SearchScope.valueOf(config.getProperty(CONFIGKEY_LDAP_SEARCH_SCOPE, SearchScope.ONELEVEL.name()));
EntryCursor cursor = conn.search(baseDn, searchQ, scope, "*");
while (cursor.next()) {
if (userDn != null) {
log.error("more than 1 user found in LDAP");
throw new OmException(-1L);
}
Entry e = cursor.get();
userDn = e.getDn();
if (useAdminForAttrs) {
entry = e;
}
}
cursor.close();
if (userDn == null) {
log.error("NONE users found in LDAP");
throw new OmException(-11L);
}
conn.bind(userDn, passwd);
}
break;
case SIMPLEBIND:
{
userDn = getUserDn(config, user);
conn.bind(userDn, passwd);
}
break;
case NONE:
default:
authenticated = false;
break;
}
u = authenticated ? userDao.getByName(user, Type.ldap) : userDao.login(user, passwd);
if (u == null && Provisionning.AUTOCREATE != prov) {
log.error("User not found in OM DB and Provisionning.AUTOCREATE was not set");
throw new OmException(-11L);
} else if (u != null && !domainId.equals(u.getDomainId())) {
log.error("User found in OM DB, but domains are differ");
throw new OmException(-11L);
}
if (authenticated && entry == null) {
if (useAdminForAttrs) {
bindAdmin(conn, ldap_admin_dn, ldap_admin_passwd);
}
entry = conn.lookup(userDn);
}
switch (prov) {
case AUTOUPDATE:
case AUTOCREATE:
if (entry == null) {
log.error("LDAP entry is null, search or lookup by Dn failed");
throw new OmException(-11L);
}
if (u == null) {
Set<Right> rights = UserDao.getDefaultRights();
rights.remove(Right.Login);
u = new User();
u.setType(Type.ldap);
u.setRights(rights);
u.setDomainId(domainId);
u.getOrganisation_users().add(new Organisation_Users(orgDao.get(cfgDao.getConfValue("default_domain_id", Long.class, "-1"))));
u.setLogin(user);
u.setAge(new Date());
u.setShowContactDataToContacts(true);
u.setAdresses(new Address());
u.setLanguage_id(cfgDao.getConfValue(CONFIG_DEFAUT_LANG_KEY, Long.class, "1"));
u.setSalutations_id(1L);
}
if ("true".equals(config.getProperty(CONFIGKEY_LDAP_SYNC_PASSWD_OM, ""))) {
u.updatePassword(cfgDao, passwd);
}
u.setLastname(getAttr(config, entry, CONFIGKEY_LDAP_KEY_LASTNAME, LDAP_KEY_LASTNAME));
u.setFirstname(getAttr(config, entry, CONFIGKEY_LDAP_KEY_FIRSTNAME, LDAP_KEY_FIRSTNAME));
u.getAdresses().setEmail(getAttr(config, entry, CONFIGKEY_LDAP_KEY_MAIL, LDAP_KEY_MAIL));
u.getAdresses().setStreet(getAttr(config, entry, CONFIGKEY_LDAP_KEY_STREET, LDAP_KEY_STREET));
u.getAdresses().setAdditionalname(getAttr(config, entry, CONFIGKEY_LDAP_KEY_ADDITIONAL_NAME, LDAP_KEY_ADDITIONAL_NAME));
u.getAdresses().setFax(getAttr(config, entry, CONFIGKEY_LDAP_KEY_FAX, LDAP_KEY_FAX));
u.getAdresses().setZip(getAttr(config, entry, CONFIGKEY_LDAP_KEY_ZIP, LDAP_KEY_ZIP));
u.getAdresses().setStates(stateDao.getStateByName(getAttr(config, entry, CONFIGKEY_LDAP_KEY_COUNTRY, LDAP_KEY_COUNTRY)));
u.getAdresses().setTown(getAttr(config, entry, CONFIGKEY_LDAP_KEY_TOWN, LDAP_KEY_TOWN));
u.getAdresses().setPhone(getAttr(config, entry, CONFIGKEY_LDAP_KEY_PHONE, LDAP_KEY_PHONE));
String tz = getAttr(config, entry, CONFIGKEY_LDAP_TIMEZONE_NAME, LDAP_KEY_TIMEZONE);
if (tz == null) {
tz = config.getProperty(CONFIGKEY_LDAP_TIMEZONE_NAME, null);
}
u.setTimeZoneId(timezoneUtil.getTimeZone(tz).getID());
String picture = getAttr(config, entry, CONFIGKEY_LDAP_PICTURE_URI, LDAP_KEY_PICTURE_URI);
if (picture == null) {
picture = config.getProperty(CONFIGKEY_LDAP_PICTURE_URI, null);
}
u.setPictureuri(picture);
u = userDao.update(u, null);
break;
case NONE:
default:
break;
}
} catch (LdapAuthenticationException ae) {
log.error("Not authenticated.", ae);
throw new OmException(-11L);
} catch (OmException e) {
throw e;
} catch (Exception e) {
log.error("Unexpected exception.", e);
throw new OmException(e);
} finally {
if (conn != null) {
try {
conn.unBind();
conn.close();
} catch (Exception e) {
log.error("Unexpected exception.", e);
throw new OmException(e);
}
}
}
return u;
}