}
final AuthValveContext authContext = (AuthValveContext) context;
final HttpServletRequest httpServletRequest = authContext.getRequest();
JahiaUser theUser = null;
boolean ok = false;
if (isLoginRequested(httpServletRequest)) {
final String username = httpServletRequest.getParameter("username");
final String password = httpServletRequest.getParameter("password");
if ((username != null) && (password != null)) {
final ServicesRegistry theRegistry = ServicesRegistry.getInstance();
if (theRegistry != null) {
JahiaUserManagerService theService = theRegistry.getJahiaUserManagerService();
if (theService != null) {
// Check if the user has site access ( even though it is not a user of this site )
theUser = ServicesRegistry.getInstance().getJahiaUserManagerService().lookupUser(username);
if (theUser != null) {
if (theUser.verifyPassword(password)) {
if (!isAccounteLocked(theUser)) {
ok = true;
} else {
logger.warn("Login failed: account for user " + theUser.getUsername() + " is locked.");
httpServletRequest.setAttribute(VALVE_RESULT, ACCOUNT_LOCKED);
}
} else {
logger.warn("Login failed: user " + theUser.getUsername() + " provided bad password.");
httpServletRequest.setAttribute(VALVE_RESULT, BAD_PASSWORD);
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Login failed. Unknown username " + username + ".");
}
httpServletRequest.setAttribute(VALVE_RESULT, UNKNOWN_USER);
}
}
}
}
}
if (ok) {
if (logger.isDebugEnabled()) {
logger.debug("User " + theUser + " logged in.");
}
if (httpServletRequest.getSession(false) != null) {
httpServletRequest.getSession().invalidate();
}
httpServletRequest.setAttribute(VALVE_RESULT, OK);
authContext.getSessionFactory().setCurrentUser(theUser);
// do a switch to the user's preferred language
if (SettingsBean.getInstance().isConsiderPreferredLanguageAfterLogin()) {
Locale preferredUserLocale = UserPreferencesHelper.getPreferredLocale(theUser, LanguageCodeConverters.resolveLocaleForGuest(httpServletRequest));
JahiaSite site = (JahiaSite) authContext.getRequest().getSession().getAttribute(ProcessingContext.SESSION_SITE);
if (site != null) {
List<Locale> siteLocales = site.getLanguagesAsLocales();
if (siteLocales.contains(preferredUserLocale)) {
httpServletRequest.getSession()
.setAttribute(ProcessingContext.SESSION_LOCALE, preferredUserLocale);
}
}
}
String useCookie = httpServletRequest.getParameter(USE_COOKIE);
if ((useCookie != null) && ("on".equals(useCookie))) {
// the user has indicated he wants to use cookie authentication
// now let's create a random identifier to store in the cookie.
String cookieUserKey = null;
// now let's look for a free random cookie value key.
while (cookieUserKey == null) {
cookieUserKey = CookieAuthValveImpl.generateRandomString(cookieAuthConfig.getIdLength());
Properties searchCriterias = new Properties();
searchCriterias.setProperty(cookieAuthConfig.getUserPropertyName(), cookieUserKey);
Set<Principal> foundUsers =
ServicesRegistry.getInstance().getJahiaUserManagerService().searchUsers(searchCriterias);
if (foundUsers.size() > 0) {
cookieUserKey = null;
}
}
// let's save the identifier for the user in the database
theUser.setProperty(cookieAuthConfig.getUserPropertyName(), cookieUserKey);
// now let's save the same identifier in the cookie.
Cookie authCookie = new Cookie(cookieAuthConfig.getCookieName(), cookieUserKey);
authCookie.setPath(StringUtils.isNotEmpty(httpServletRequest.getContextPath()) ?
httpServletRequest.getContextPath() : "/");
authCookie.setMaxAge(cookieAuthConfig.getMaxAgeInSeconds());