NIHVIVO-2696 de-couple the LoginRedirector from the LoginProcessBean.

This commit is contained in:
j2blake 2011-06-13 17:44:42 +00:00
parent 26ed55027d
commit 22507b11c0
4 changed files with 45 additions and 16 deletions

View file

@ -133,7 +133,7 @@ public class UserAccountsUserController extends FreemarkerHttpServlet {
} }
private ResponseValues showLoginRedirection(VitroRequest vreq) { private ResponseValues showLoginRedirection(VitroRequest vreq) {
LoginRedirector lr = new LoginRedirector(vreq); LoginRedirector lr = new LoginRedirector(vreq, null);
DisplayMessage.setMessage(vreq, lr.assembleWelcomeMessage()); DisplayMessage.setMessage(vreq, lr.assembleWelcomeMessage());
String uri = lr.getRedirectionUriForLoggedInUser(); String uri = lr.getRedirectionUriForLoggedInUser();
return new RedirectResponseValues(uri); return new RedirectResponseValues(uri);

View file

@ -16,6 +16,7 @@ import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.LoginStatusBean.AuthenticationSource; import edu.cornell.mannlib.vedit.beans.LoginStatusBean.AuthenticationSource;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean;
/** /**
* Handle the return from the external authorization login server. If we are * Handle the return from the external authorization login server. If we are
@ -36,6 +37,13 @@ public class LoginExternalAuthReturn extends BaseLoginServlet {
* - User corresponds to a User acocunt. Record the login. * - User corresponds to a User acocunt. Record the login.
* - User corresponds to an Individual (self-editor). * - User corresponds to an Individual (self-editor).
* - User is not recognized. * - User is not recognized.
*
* On entry, we expect to find:
* - A LoginProcessBean, which will give us the afterLoginUrl if the login
* succeeds.
* - A referrer URL, to which we will redirect if the login fails.
* TODO: is this equal to LoginProcessBean.getLoginPageUrl()?
* These are removed on exit.
* </pre> * </pre>
*/ */
@Override @Override
@ -50,14 +58,16 @@ public class LoginExternalAuthReturn extends BaseLoginServlet {
return; return;
} }
String afterLoginUrl = LoginProcessBean.getBean(req).getAfterLoginUrl();
removeLoginProcessArtifacts(req);
UserAccount userAccount = getAuthenticator(req) UserAccount userAccount = getAuthenticator(req)
.getAccountForExternalAuth(externalAuthId); .getAccountForExternalAuth(externalAuthId);
if (userAccount != null) { if (userAccount != null) {
log.debug("Logging in as " + userAccount.getUri()); log.debug("Logging in as " + userAccount.getUri());
getAuthenticator(req).recordLoginAgainstUserAccount(userAccount, getAuthenticator(req).recordLoginAgainstUserAccount(userAccount,
AuthenticationSource.EXTERNAL); AuthenticationSource.EXTERNAL);
removeLoginProcessArtifacts(req); new LoginRedirector(req, afterLoginUrl).redirectLoggedInUser(resp);
new LoginRedirector(req).redirectLoggedInUser(resp);
return; return;
} }
@ -70,19 +80,19 @@ public class LoginExternalAuthReturn extends BaseLoginServlet {
String uri = associatedUris.get(0); String uri = associatedUris.get(0);
getAuthenticator(req).recordLoginWithoutUserAccount(uri); getAuthenticator(req).recordLoginWithoutUserAccount(uri);
removeLoginProcessArtifacts(req); new LoginRedirector(req, afterLoginUrl).redirectLoggedInUser(resp);
new LoginRedirector(req).redirectLoggedInUser(resp);
return; return;
} }
log.debug("User is not recognized: " + externalAuthId); log.debug("User is not recognized: " + externalAuthId);
removeLoginProcessArtifacts(req); removeLoginProcessArtifacts(req);
new LoginRedirector(req).redirectUnrecognizedExternalUser(resp, new LoginRedirector(req, afterLoginUrl).redirectUnrecognizedExternalUser(resp,
externalAuthId); externalAuthId);
} }
private void removeLoginProcessArtifacts(HttpServletRequest req) { private void removeLoginProcessArtifacts(HttpServletRequest req) {
req.getSession().removeAttribute(ATTRIBUTE_REFERRER); req.getSession().removeAttribute(ATTRIBUTE_REFERRER);
LoginProcessBean.removeBean(req);
} }
@Override @Override

View file

@ -20,7 +20,6 @@ import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage; import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers; import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean;
/** /**
* A user has just completed the login process. What page do we direct them to? * A user has just completed the login process. What page do we direct them to?
@ -34,15 +33,12 @@ public class LoginRedirector {
private final String uriOfAssociatedIndividual; private final String uriOfAssociatedIndividual;
private final String afterLoginPage; private final String afterLoginPage;
public LoginRedirector(HttpServletRequest request) { public LoginRedirector(HttpServletRequest request, String afterLoginPage) {
this.request = request; this.request = request;
this.session = request.getSession(); this.session = request.getSession();
this.afterLoginPage = afterLoginPage;
uriOfAssociatedIndividual = getAssociatedIndividualUri(); uriOfAssociatedIndividual = getAssociatedIndividualUri();
LoginProcessBean processBean = LoginProcessBean.getBean(request);
log.debug("process bean is: " + processBean);
afterLoginPage = processBean.getAfterLoginUrl();
} }
/** Is there an Individual associated with this user? */ /** Is there an Individual associated with this user? */
@ -106,7 +102,6 @@ public class LoginRedirector {
try { try {
DisplayMessage.setMessage(request, assembleWelcomeMessage()); DisplayMessage.setMessage(request, assembleWelcomeMessage());
response.sendRedirect(getRedirectionUriForLoggedInUser()); response.sendRedirect(getRedirectionUriForLoggedInUser());
LoginProcessBean.removeBean(request);
} catch (IOException e) { } catch (IOException e) {
log.debug("Problem with re-direction", e); log.debug("Problem with re-direction", e);
response.sendRedirect(getApplicationHomePageUrl()); response.sendRedirect(getApplicationHomePageUrl());
@ -142,7 +137,6 @@ public class LoginRedirector {
throws IOException { throws IOException {
try { try {
response.sendRedirect(getRedirectionUriForCancellingUser()); response.sendRedirect(getRedirectionUriForCancellingUser());
LoginProcessBean.removeBean(request);
} catch (IOException e) { } catch (IOException e) {
log.debug("Problem with re-direction", e); log.debug("Problem with re-direction", e);
response.sendRedirect(getApplicationHomePageUrl()); response.sendRedirect(getApplicationHomePageUrl());

View file

@ -124,7 +124,7 @@ public class Authenticate extends VitroHttpServlet {
// Send them on their way. // Send them on their way.
switch (exitState) { switch (exitState) {
case NOWHERE: case NOWHERE:
new LoginRedirector(vreq).redirectCancellingUser(response); showLoginCanceled(response, vreq);
break; break;
case LOGGING_IN: case LOGGING_IN:
showLoginScreen(vreq, response); showLoginScreen(vreq, response);
@ -133,7 +133,7 @@ public class Authenticate extends VitroHttpServlet {
showLoginScreen(vreq, response); showLoginScreen(vreq, response);
break; break;
default: // LOGGED_IN: default: // LOGGED_IN:
new LoginRedirector(vreq).redirectLoggedInUser(response); showLoginComplete(response, vreq);
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
@ -478,6 +478,31 @@ public class Authenticate extends VitroHttpServlet {
return; return;
} }
/**
* Exit: user has completed the login. Redirect appropriately and clear the bean.
*/
private void showLoginComplete(HttpServletResponse response,
VitroRequest vreq) throws IOException {
getLoginRedirector(vreq).redirectLoggedInUser(response);
LoginProcessBean.removeBean(vreq);
}
/**
* Exit: user has canceled. Redirect and clear the bean.
*/
private void showLoginCanceled(HttpServletResponse response,
VitroRequest vreq) throws IOException {
getLoginRedirector(vreq).redirectCancellingUser(response);
LoginProcessBean.removeBean(vreq);
}
private LoginRedirector getLoginRedirector(VitroRequest vreq) {
String afterLoginUrl = LoginProcessBean.getBean(vreq).getAfterLoginUrl();
return new LoginRedirector(vreq, afterLoginUrl);
}
/** Get a reference to the Authenticator. */ /** Get a reference to the Authenticator. */
private Authenticator getAuthenticator(HttpServletRequest request) { private Authenticator getAuthenticator(HttpServletRequest request) {
return Authenticator.getInstance(request); return Authenticator.getInstance(request);