NIHVIVO-2696 Refactor LoginRedirector to make it more flexible - usable from Freemarker controllers.

This commit is contained in:
j2blake 2011-06-10 18:53:35 +00:00
parent 0f00fd7a08
commit ac97b5792a
3 changed files with 49 additions and 43 deletions

View file

@ -57,7 +57,7 @@ public class LoginExternalAuthReturn extends BaseLoginServlet {
getAuthenticator(req).recordLoginAgainstUserAccount(userAccount, getAuthenticator(req).recordLoginAgainstUserAccount(userAccount,
AuthenticationSource.EXTERNAL); AuthenticationSource.EXTERNAL);
removeLoginProcessArtifacts(req); removeLoginProcessArtifacts(req);
new LoginRedirector(req, resp).redirectLoggedInUser(); new LoginRedirector(req).redirectLoggedInUser(resp);
return; return;
} }
@ -71,14 +71,14 @@ public class LoginExternalAuthReturn extends BaseLoginServlet {
getAuthenticator(req).recordLoginWithoutUserAccount(uri); getAuthenticator(req).recordLoginWithoutUserAccount(uri);
removeLoginProcessArtifacts(req); removeLoginProcessArtifacts(req);
new LoginRedirector(req, resp).redirectLoggedInUser(); 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, resp) new LoginRedirector(req).redirectUnrecognizedExternalUser(resp,
.redirectUnrecognizedExternalUser(externalAuthId); externalAuthId);
} }
private void removeLoginProcessArtifacts(HttpServletRequest req) { private void removeLoginProcessArtifacts(HttpServletRequest req) {

View file

@ -29,17 +29,14 @@ public class LoginRedirector {
private static final Log log = LogFactory.getLog(LoginRedirector.class); private static final Log log = LogFactory.getLog(LoginRedirector.class);
private final HttpServletRequest request; private final HttpServletRequest request;
private final HttpServletResponse response;
private final HttpSession session; private final HttpSession session;
private final String uriOfAssociatedIndividual; private final String uriOfAssociatedIndividual;
private final String afterLoginPage; private final String afterLoginPage;
public LoginRedirector(HttpServletRequest request, public LoginRedirector(HttpServletRequest request) {
HttpServletResponse response) {
this.request = request; this.request = request;
this.session = request.getSession(); this.session = request.getSession();
this.response = response;
uriOfAssociatedIndividual = getAssociatedIndividualUri(); uriOfAssociatedIndividual = getAssociatedIndividualUri();
@ -70,28 +67,45 @@ public class LoginRedirector {
} }
} }
public void redirectLoggedInUser() throws IOException { public String getRedirectionUriForLoggedInUser() {
DisplayMessage.setMessage(request, assembleWelcomeMessage()); if (isSelfEditorWithIndividual()) {
log.debug("Going to Individual home page.");
try { return getAssociatedIndividualHomePage();
if (isSelfEditorWithIndividual()) { } else if (isMerelySelfEditor()) {
log.debug("Going to Individual home page."); log.debug("User not recognized. Going to application home.");
response.sendRedirect(getAssociatedIndividualHomePage()); return getApplicationHomePageUrl();
} else if (isMerelySelfEditor()) { } else {
log.debug("User not recognized. Going to application home."); if (isLoginPage(afterLoginPage)) {
response.sendRedirect(getApplicationHomePageUrl()); log.debug("Coming from /login. Going to site admin page.");
return getSiteAdminPageUrl();
} else if (null != afterLoginPage) {
log.debug("Returning to requested page: " + afterLoginPage);
return afterLoginPage;
} else { } else {
if (isLoginPage(afterLoginPage)) { log.debug("Don't know what to do. Go home.");
log.debug("Coming from /login. Going to site admin page."); return getApplicationHomePageUrl();
response.sendRedirect(getSiteAdminPageUrl());
} else if (null != afterLoginPage) {
log.debug("Returning to requested page: " + afterLoginPage);
response.sendRedirect(afterLoginPage);
} else {
log.debug("Don't know what to do. Go home.");
response.sendRedirect(getApplicationHomePageUrl());
}
} }
}
}
public String getRedirectionUriForCancellingUser() {
if (isLoginPage(afterLoginPage)) {
log.debug("Coming from /login. Going to home.");
return getApplicationHomePageUrl();
} else if (null != afterLoginPage) {
log.debug("Returning to requested page: " + afterLoginPage);
return afterLoginPage;
} else {
log.debug("Don't know what to do. Go home.");
return getApplicationHomePageUrl();
}
}
public void redirectLoggedInUser(HttpServletResponse response)
throws IOException {
try {
DisplayMessage.setMessage(request, assembleWelcomeMessage());
response.sendRedirect(getRedirectionUriForLoggedInUser());
LoginProcessBean.removeBean(request); LoginProcessBean.removeBean(request);
} catch (IOException e) { } catch (IOException e) {
log.debug("Problem with re-direction", e); log.debug("Problem with re-direction", e);
@ -124,18 +138,10 @@ public class LoginRedirector {
return "Welcome" + backString + ", " + greeting; return "Welcome" + backString + ", " + greeting;
} }
public void redirectCancellingUser() throws IOException { public void redirectCancellingUser(HttpServletResponse response)
throws IOException {
try { try {
if (isLoginPage(afterLoginPage)) { response.sendRedirect(getRedirectionUriForCancellingUser());
log.debug("Coming from /login. Going to home.");
response.sendRedirect(getApplicationHomePageUrl());
} else if (null != afterLoginPage) {
log.debug("Returning to requested page: " + afterLoginPage);
response.sendRedirect(afterLoginPage);
} else {
log.debug("Don't know what to do. Go home.");
response.sendRedirect(getApplicationHomePageUrl());
}
LoginProcessBean.removeBean(request); LoginProcessBean.removeBean(request);
} catch (IOException e) { } catch (IOException e) {
log.debug("Problem with re-direction", e); log.debug("Problem with re-direction", e);
@ -143,8 +149,8 @@ public class LoginRedirector {
} }
} }
public void redirectUnrecognizedExternalUser(String username) public void redirectUnrecognizedExternalUser(HttpServletResponse response,
throws IOException { String username) throws IOException {
log.debug("Redirecting unrecognized external user: " + username); log.debug("Redirecting unrecognized external user: " + username);
DisplayMessage.setMessage(request, DisplayMessage.setMessage(request,
"VIVO cannot find a profile for your account."); "VIVO cannot find a profile for your account.");

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, response).redirectCancellingUser(); new LoginRedirector(vreq).redirectCancellingUser(response);
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, response).redirectLoggedInUser(); new LoginRedirector(vreq).redirectLoggedInUser(response);
break; break;
} }
} catch (Exception e) { } catch (Exception e) {