"Username and password can not be empty");
}
String encodedPassword = passwordEncoderService.encodePassword(
clearPassword, username);
User user = getUserFromDB(username);
// If user != null then exists in LibrePlan
if (null != user && user.isLibrePlanUser()) {
// is a LibrePlan user, then we must authenticate against DB
return authenticateInDatabase(username, user, encodedPassword);
}
// If it's a LDAP or null user, then we must authenticate against LDAP
// Load LDAPConfiguration properties
configuration = loadLDAPConfiguration();
if (configuration.getLdapAuthEnabled()) {
// Sets the new context to ldapTemplate
ldapTemplate.setContextSource(loadLDAPContext());
try {
// Test authentication for user against LDAP
if (authenticateAgainstLDAP(username, clearPassword)) {
// Authentication against LDAP was ok
if (null == user) {
// User does not exist in LibrePlan must be imported
user = createLDAPUserWithRoles(username, encodedPassword);
} else {
// Update password
if (configuration.isLdapSavePasswordsDB()) {
user.setPassword(encodedPassword);
}
// Update roles from LDAP
setRoles(user);
}
saveUserOnTransaction(user);