HttpSession session = request.getSession(true);
LogonForm logonForm = (LogonForm) form;
ServletContext ctx = getServlet().getServletContext();
WebUser webUser = null;
Map<String, Boolean> userGlobalPermissionsMap = new HashMap<String, Boolean>();
boolean needsRegistration = false;
try {
// authenticate the credentials
SubjectManagerLocal subjectManager = LookupUtil.getSubjectManager();
Subject subject = subjectManager.loginLocal(logonForm.getJ_username(), logonForm.getJ_password());
Integer sessionId = subject.getSessionId(); // this is the RHQ session ID, not related to the HTTP session
log.debug("Logged in as [" + logonForm.getJ_username() + "] with session id [" + sessionId + "]");
boolean hasPrincipal = true;
if (subject.getId() == 0) {
// Subject with a ID of 0 means the subject wasn't in the database but the login succeeded.
// This means the login method detected that LDAP authenticated the user and just gave us a dummy subject.
// Set the needs-registration flag so we can eventually steer the user to the LDAP registration workflow.
needsRegistration = true;
}
if (!needsRegistration) {
subject = subjectManager.loadUserConfiguration(subject.getId());
subject.setSessionId(sessionId); // put the transient data back into our new subject
if (subject.getUserConfiguration() == null) {
subject.setUserConfiguration((Configuration) ctx.getAttribute(Constants.DEF_USER_PREFS));
subject = subjectManager.updateSubject(subject, subject);
subject.setSessionId(sessionId); // put the transient data back into our new subject
}
// look up the user's permissions
Set<Permission> all_permissions = LookupUtil.getAuthorizationManager().getExplicitGlobalPermissions(
subject);
for (Permission permission : all_permissions) {
userGlobalPermissionsMap.put(permission.toString(), Boolean.TRUE);
}
}
webUser = new WebUser(subject, hasPrincipal);
} catch (Exception e) {
String msg = e.getMessage().toLowerCase();
if ((msg.indexOf("username") >= 0) || (msg.indexOf("password") >= 0)) {
request.setAttribute(Constants.LOGON_STATUS, "login.info.bad");
} else {
log.error("Could not log into the web application", e);
request.setAttribute(Constants.LOGON_STATUS, "login.bad.backend");
}
return (mapping.findForward("bad"));
}
// compute the post-login destination
ActionForward af;
if (needsRegistration) {
// Since we are authenticating the user with LDAP and the user has never logged in before,
// that user has no subject record yet. We need to send him through the LDAP registration workflow.
log.debug("LDAP registration required for user [" + logonForm.getJ_username() + "]");
af = new ActionForward(URL_REGISTER);
} else {
// if the user's session timed out, we "bookmarked" the url that he was going to
// so that we can send him there after login. otherwise, he gets the dashboard.
String url = getBookmarkedUrl(session);
if ((url == null) || url.equals("/Logout.do")) {
url = URL_DASHBOARD;
}
if (url.toLowerCase().indexOf("ajax") != -1) {
// we can't return to a URL that was a partial page request
// because the view no longer exists, and will blow up.
// instead, redirect back to the last saved URL
url = webUser.getWebPreferences().getLastVisitedURL(2);
log.info("Bypassing partial-page with " + url);
}
af = new ActionForward(url);
}