NIHVIVO-1207 Make LoginProcessBean more encapsulated.
This commit is contained in:
parent
c6ff668647
commit
dcb358d4c8
5 changed files with 103 additions and 63 deletions
|
@ -130,7 +130,7 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
String username = request.getParameter(PARAMETER_USERNAME);
|
||||
String password = request.getParameter(PARAMETER_PASSWORD);
|
||||
|
||||
LoginProcessBean bean = getLoginProcessBean(request);
|
||||
LoginProcessBean bean = LoginProcessBean.getBean(request);
|
||||
bean.clearMessage();
|
||||
log.trace("username=" + username + ", password=" + password + ", bean="
|
||||
+ bean);
|
||||
|
@ -170,7 +170,7 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
private void whatNextForThisGuy(HttpServletRequest request, User user) {
|
||||
if (user.getLoginCount() == 0) {
|
||||
log.debug("Forcing first-time password change");
|
||||
LoginProcessBean bean = getLoginProcessBean(request);
|
||||
LoginProcessBean bean = LoginProcessBean.getBean(request);
|
||||
bean.setState(State.FORCED_PASSWORD_CHANGE);
|
||||
} else {
|
||||
recordLoginInfo(request, user.getUsername());
|
||||
|
@ -191,7 +191,7 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
* If they want to cancel the login, let them.
|
||||
*/
|
||||
private void recordLoginCancelled(HttpServletRequest request) {
|
||||
getLoginProcessBean(request).setState(State.CANCELLED);
|
||||
LoginProcessBean.getBean(request).setState(State.CANCELLED);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -200,7 +200,7 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
private User checkChangeProgress(HttpServletRequest request) {
|
||||
String newPassword = request.getParameter(PARAMETER_NEW_PASSWORD);
|
||||
String confirm = request.getParameter(PARAMETER_CONFIRM_PASSWORD);
|
||||
LoginProcessBean bean = getLoginProcessBean(request);
|
||||
LoginProcessBean bean = LoginProcessBean.getBean(request);
|
||||
bean.clearMessage();
|
||||
log.trace("newPassword=" + newPassword + ", confirm=" + confirm
|
||||
+ ", bean=" + bean);
|
||||
|
@ -261,8 +261,7 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
getAuthenticator(request).setLoggedIn(user);
|
||||
|
||||
// Remove the login process info from the session.
|
||||
request.getSession()
|
||||
.removeAttribute(LoginProcessBean.SESSION_ATTRIBUTE);
|
||||
LoginProcessBean.removeBean(request);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -280,10 +279,8 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
*/
|
||||
private void redirectCancellingUser(HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {
|
||||
request.getSession()
|
||||
.removeAttribute(LoginProcessBean.SESSION_ATTRIBUTE);
|
||||
|
||||
log.debug("User cancelled the login. Redirect to site admin page.");
|
||||
LoginProcessBean.removeBean(request);
|
||||
response.sendRedirect(getHomeUrl(request));
|
||||
}
|
||||
|
||||
|
@ -384,15 +381,15 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
return State.LOGGED_IN;
|
||||
}
|
||||
|
||||
if (session.getAttribute(LoginProcessBean.SESSION_ATTRIBUTE) == null) {
|
||||
if (LoginProcessBean.isBean(request)) {
|
||||
State state = LoginProcessBean.getBean(request).getState();
|
||||
log.debug("state from LoginProcessBean is " + state);
|
||||
return state;
|
||||
} else {
|
||||
log.debug("no LoginSessionBean, no LoginProcessBean: "
|
||||
+ "current state is NOWHERE");
|
||||
return State.NOWHERE;
|
||||
}
|
||||
|
||||
State state = getLoginProcessBean(request).getState();
|
||||
log.debug("state from LoginProcessBean is " + state);
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -432,11 +429,6 @@ public class Authenticate extends FreemarkerHttpServlet {
|
|||
return request.getContextPath();
|
||||
}
|
||||
|
||||
/** Where do we stand in the login process? */
|
||||
private LoginProcessBean getLoginProcessBean(HttpServletRequest request) {
|
||||
return LoginProcessBean.getBeanFromSession(request);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public utility methods.
|
||||
// ----------------------------------------------------------------------
|
||||
|
|
|
@ -53,7 +53,7 @@ public class Login extends HttpServlet {
|
|||
* Otherwise, set up as if they had filled in the login form, and send
|
||||
* them to authenticate it.
|
||||
*/
|
||||
LoginProcessBean bean = LoginProcessBean.getBeanFromSession(request);
|
||||
LoginProcessBean bean = LoginProcessBean.getBean(request);
|
||||
bean.setState(LoginProcessBean.State.LOGGING_IN);
|
||||
request.getRequestDispatcher(Controllers.AUTHENTICATE).forward(request,
|
||||
response);
|
||||
|
|
|
@ -8,31 +8,91 @@ import java.util.Arrays;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Where are we in the process of logging on? What message should we show to the
|
||||
* user?
|
||||
*/
|
||||
public class LoginProcessBean {
|
||||
private static final Log log = LogFactory.getLog(LoginProcessBean.class);
|
||||
|
||||
private static Object[] NO_ARGUMENTS = new Object[0];
|
||||
|
||||
public static final String SESSION_ATTRIBUTE = LoginProcessBean.class
|
||||
private static final String SESSION_ATTRIBUTE = LoginProcessBean.class
|
||||
.getName();
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// static methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get the login process bean from the session. If there is none, create
|
||||
* Is there currently a login process bean in the session?
|
||||
*/
|
||||
public static boolean isBean(HttpServletRequest request) {
|
||||
return (null != getBeanFromSession(request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the login process bean from the session. If there is no bean, create
|
||||
* one.
|
||||
*/
|
||||
public static LoginProcessBean getBeanFromSession(HttpServletRequest request) {
|
||||
HttpSession session = request.getSession();
|
||||
LoginProcessBean bean = (LoginProcessBean) session
|
||||
.getAttribute(SESSION_ATTRIBUTE);
|
||||
if (bean == null) {
|
||||
bean = new LoginProcessBean();
|
||||
session.setAttribute(SESSION_ATTRIBUTE, bean);
|
||||
public static LoginProcessBean getBean(HttpServletRequest request) {
|
||||
if (isBean(request)) {
|
||||
return getBeanFromSession(request);
|
||||
} else {
|
||||
setBean(request, new LoginProcessBean());
|
||||
return getBeanFromSession(request);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store this login process bean in the session.
|
||||
*/
|
||||
public static void setBean(HttpServletRequest request, LoginProcessBean bean) {
|
||||
HttpSession session = request.getSession();
|
||||
session.setAttribute(SESSION_ATTRIBUTE, bean);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the login process bean from the session. If there is no bean, do
|
||||
* nothing.
|
||||
*/
|
||||
public static void removeBean(HttpServletRequest request) {
|
||||
if (isBean(request)) {
|
||||
request.getSession().removeAttribute(SESSION_ATTRIBUTE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bean from the session, or null if there is no bean.
|
||||
*/
|
||||
private static LoginProcessBean getBeanFromSession(
|
||||
HttpServletRequest request) {
|
||||
HttpSession session = request.getSession(false);
|
||||
if (session == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object bean = session.getAttribute(SESSION_ATTRIBUTE);
|
||||
if (bean == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!(bean instanceof LoginProcessBean)) {
|
||||
log.warn("Tried to get login process bean, but found an instance of "
|
||||
+ bean.getClass().getName() + ": " + bean);
|
||||
return null;
|
||||
}
|
||||
|
||||
return (LoginProcessBean) bean;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// helper classes
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
public enum State {
|
||||
NOWHERE, LOGGING_IN, FORCED_PASSWORD_CHANGE, CANCELLED, LOGGED_IN
|
||||
}
|
||||
|
@ -89,6 +149,10 @@ public class LoginProcessBean {
|
|||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// the bean
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/** Where are we in the process? */
|
||||
private State currentState = State.NOWHERE;
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
@ -101,7 +100,7 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase {
|
|||
*/
|
||||
private TemplateResponseValues showLoginScreen(VitroRequest vreq)
|
||||
throws IOException {
|
||||
LoginProcessBean bean = getLoginProcessBean(vreq);
|
||||
LoginProcessBean bean = LoginProcessBean.getBean(vreq);
|
||||
bean.setState(State.LOGGING_IN);
|
||||
log.trace("Going to login screen: " + bean);
|
||||
|
||||
|
@ -126,7 +125,7 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase {
|
|||
* change it (unless they cancel out).
|
||||
*/
|
||||
private TemplateResponseValues showPasswordChangeScreen(VitroRequest vreq) {
|
||||
LoginProcessBean bean = getLoginProcessBean(vreq);
|
||||
LoginProcessBean bean = LoginProcessBean.getBean(vreq);
|
||||
bean.setState(State.FORCED_PASSWORD_CHANGE);
|
||||
log.trace("Going to password change screen: " + bean);
|
||||
|
||||
|
@ -173,27 +172,10 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase {
|
|||
if (LoginStatusBean.getBean(request).isLoggedIn()) {
|
||||
return State.LOGGED_IN;
|
||||
} else {
|
||||
return getLoginProcessBean(request).getState();
|
||||
return LoginProcessBean.getBean(request).getState();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* How is the login process coming along?
|
||||
*/
|
||||
private LoginProcessBean getLoginProcessBean(HttpServletRequest request) {
|
||||
HttpSession session = request.getSession();
|
||||
|
||||
LoginProcessBean bean = (LoginProcessBean) session
|
||||
.getAttribute(LoginProcessBean.SESSION_ATTRIBUTE);
|
||||
|
||||
if (bean == null) {
|
||||
bean = new LoginProcessBean();
|
||||
session.setAttribute(LoginProcessBean.SESSION_ATTRIBUTE, bean);
|
||||
}
|
||||
|
||||
return bean;
|
||||
}
|
||||
|
||||
/** What's the URL for this servlet? */
|
||||
private String getAuthenticateUrl(HttpServletRequest request) {
|
||||
String contextPath = request.getContextPath();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue