diff --git a/webapp/src/edu/cornell/mannlib/vedit/beans/LoginStatusBean.java b/webapp/src/edu/cornell/mannlib/vedit/beans/LoginStatusBean.java index d69f8665a..15cdbbf43 100644 --- a/webapp/src/edu/cornell/mannlib/vedit/beans/LoginStatusBean.java +++ b/webapp/src/edu/cornell/mannlib/vedit/beans/LoginStatusBean.java @@ -22,7 +22,7 @@ public class LoginStatusBean { /** A bean to return when the user has not logged in. */ private static final LoginStatusBean DUMMY_BEAN = new LoginStatusBean("", - "", AuthenticationSource.UNKNOWN); + AuthenticationSource.UNKNOWN); /** The bean is attached to the session by this name. */ private static final String ATTRIBUTE_NAME = "loginStatus"; @@ -96,17 +96,26 @@ public class LoginStatusBean { return null; } + if (!getBean(session).isLoggedIn()) { + return null; + } + ServletContext ctx = session.getServletContext(); WebappDaoFactory wadf = (WebappDaoFactory) ctx .getAttribute("webappDaoFactory"); - UserDao userDao = wadf.getUserDao(); - - if (getBean(session).isLoggedIn()) { - String userUri = getBean(session).getUserURI(); - return userDao.getUserByURI(userUri); - } else { + if (wadf == null) { + log.error("No WebappDaoFactory"); return null; } + + UserDao userDao = wadf.getUserDao(); + if (userDao == null) { + log.error("No UserDao"); + return null; + } + + String userUri = getBean(session).getUserURI(); + return userDao.getUserByURI(userUri); } // ---------------------------------------------------------------------- @@ -118,13 +127,11 @@ public class LoginStatusBean { } private final String userURI; - private final String username; private final AuthenticationSource authenticationSource; - public LoginStatusBean(String userURI, String username, + public LoginStatusBean(String userURI, AuthenticationSource authenticationSource) { this.userURI = userURI; - this.username = username; this.authenticationSource = authenticationSource; } @@ -132,10 +139,6 @@ public class LoginStatusBean { return userURI; } - public String getUsername() { - return username; - } - public AuthenticationSource getAuthenticationSource() { return authenticationSource; } @@ -150,8 +153,8 @@ public class LoginStatusBean { @Override public String toString() { - return "LoginStatusBean[userURI=" + userURI + ", username=" + username - + ", authenticationSource=" + authenticationSource + "]"; + return "LoginStatusBean[userURI=" + userURI + ", authenticationSource=" + + authenticationSource + "]"; } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/common/CommonIdentifierBundleFactory.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/common/CommonIdentifierBundleFactory.java index 63635b914..8176ca3b2 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/common/CommonIdentifierBundleFactory.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/common/CommonIdentifierBundleFactory.java @@ -11,7 +11,6 @@ import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -23,6 +22,7 @@ import edu.cornell.mannlib.vitro.webapp.auth.identifier.IdentifierBundleFactory; import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration; +import edu.cornell.mannlib.vitro.webapp.beans.User; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; @@ -104,18 +104,12 @@ public class CommonIdentifierBundleFactory implements IdentifierBundleFactory { HttpServletRequest req) { Collection individuals = new ArrayList(); - LoginStatusBean bean = LoginStatusBean.getBean(req); - String username = bean.getUsername(); - - if (!bean.isLoggedIn()) { + User user = LoginStatusBean.getCurrentUser(req); + if (user == null) { log.debug("No Associated Individuals: not logged in."); return individuals; } - - if (StringUtils.isEmpty(username)) { - log.debug("No Associated Individuals: username is empty."); - return individuals; - } + String username = user.getUsername(); WebappDaoFactory wdf = (WebappDaoFactory) context .getAttribute("webappDaoFactory"); @@ -150,5 +144,5 @@ public class CommonIdentifierBundleFactory implements IdentifierBundleFactory { public String toString() { return this.getClass().getSimpleName() + " - " + hashCode(); } - + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java index e7b44267c..8f0fcfb6b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/BasicAuthenticator.java @@ -102,20 +102,20 @@ public class BasicAuthenticator extends Authenticator { recordLoginOnUserRecord(user); String userUri = user.getURI(); - recordLoginWithOrWithoutUserAccount(username, userUri, authSource); + recordLoginWithOrWithoutUserAccount(userUri, authSource); } @Override public void recordLoginWithoutUserAccount(String username, String individualUri, AuthenticationSource authSource) { - recordLoginWithOrWithoutUserAccount(username, individualUri, authSource); + recordLoginWithOrWithoutUserAccount(individualUri, authSource); } /** This much is in common on login, whether or not you have a user account. */ - private void recordLoginWithOrWithoutUserAccount(String username, - String userUri, AuthenticationSource authSource) { + private void recordLoginWithOrWithoutUserAccount(String userUri, + AuthenticationSource authSource) { HttpSession session = request.getSession(); - createLoginStatusBean(username, userUri, authSource, session); + createLoginStatusBean(userUri, authSource, session); setSessionTimeoutLimit(session); recordInUserSessionMap(userUri, session); notifyOtherUsers(userUri, session); @@ -135,9 +135,9 @@ public class BasicAuthenticator extends Authenticator { /** * Put the login bean into the session. */ - private void createLoginStatusBean(String username, String userUri, + private void createLoginStatusBean(String userUri, AuthenticationSource authSource, HttpSession session) { - LoginStatusBean lsb = new LoginStatusBean(userUri, username, authSource); + LoginStatusBean lsb = new LoginStatusBean(userUri, authSource); LoginStatusBean.setBean(session, lsb); log.debug("Adding status bean: " + lsb); } @@ -243,10 +243,10 @@ public class BasicAuthenticator extends Authenticator { return; } - String username = loginBean.getUsername(); - User user = userDao.getUserByUsername(username); + String userUri = loginBean.getUserURI(); + User user = userDao.getUserByURI(userUri); if (user == null) { - log.error("Unable to retrieve user " + username + " from model"); + log.error("Unable to retrieve user " + userUri + " from model"); return; } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginRedirector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginRedirector.java index 39a43a3ec..32e6c28d4 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginRedirector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/authenticate/LoginRedirector.java @@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -20,6 +21,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.DisplayMessage; import edu.cornell.mannlib.vitro.webapp.beans.User; import edu.cornell.mannlib.vitro.webapp.controller.Controllers; import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean; +import freemarker.template.utility.StringUtil; /** * A user has just completed the login process. What page do we direct them to? @@ -49,11 +51,12 @@ public class LoginRedirector { /** Is there an Individual associated with this user? */ private String getAssociatedIndividualUri() { - String username = LoginStatusBean.getBean(request).getUsername(); - if (username == null) { + User user = LoginStatusBean.getCurrentUser(request); + if (user == null) { log.warn("Not logged in? How did we get here?"); return null; } + String username = user.getUsername(); List uris = Authenticator.getInstance(request) .getAssociatedIndividualUris(username); @@ -105,19 +108,17 @@ public class LoginRedirector { + "but the system contains no profile for you."; } - LoginStatusBean bean = LoginStatusBean.getBean(request); - Authenticator auth = Authenticator.getInstance(request); - User user = auth.getUserByUsername(bean.getUsername()); - String backString = ""; - String greeting = bean.getUsername(); + String greeting = ""; + User user = LoginStatusBean.getCurrentUser(request); if (user != null) { + greeting = user.getUsername(); if (user.getLoginCount() > 1) { backString = " back"; } String name = user.getFirstName(); - if ((name != null) && (name.length() > 0)) { + if (!StringUtils.isEmpty(name)) { greeting = name; } } diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java index 40308967c..869812943 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/authenticate/AuthenticatorStub.java @@ -152,8 +152,7 @@ public class AuthenticatorStub extends Authenticator { recordedLogins.add(username); User user = getUserByUsername(username); - LoginStatusBean lsb = new LoginStatusBean(user.getURI(), username, - authSource); + LoginStatusBean lsb = new LoginStatusBean(user.getURI(), authSource); LoginStatusBean.setBean(request.getSession(), lsb); } diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/edit/AuthenticateTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/edit/AuthenticateTest.java index 2d23d45a5..624ca632f 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/edit/AuthenticateTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/edit/AuthenticateTest.java @@ -416,7 +416,7 @@ public class AuthenticateTest extends AbstractTestClass { @Test public void alreadyLoggedIn() { LoginStatusBean statusBean = new LoginStatusBean(OLD_DBA_URI, - OLD_DBA_NAME, AuthenticationSource.INTERNAL); + AuthenticationSource.INTERNAL); LoginStatusBean.setBean(session, statusBean); setRequestFromLoginLink(URL_WITH_LINK); diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/filtering/filters/HiddenFromDisplayBelowRoleLevelFilterTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/filtering/filters/HiddenFromDisplayBelowRoleLevelFilterTest.java index ca2a915c7..d4cdef924 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/filtering/filters/HiddenFromDisplayBelowRoleLevelFilterTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/filtering/filters/HiddenFromDisplayBelowRoleLevelFilterTest.java @@ -71,11 +71,11 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends boolean expectedResult; - public String getUsername() { + public String getUserUri() { if (loginStatus == null) { return "nobody"; } else { - return loginStatus.getUsername(); + return loginStatus.getUserURI(); } } @@ -170,7 +170,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends private static LoginStatusBean loginStatusBean(User user, AuthenticationSource auth) { - return new LoginStatusBean(user.getURI(), user.getUsername(), auth); + return new LoginStatusBean(user.getURI(), auth); } private static final VClass PUBLIC_VCLASS = vClass("PUBLIC_vclass", @@ -568,7 +568,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends @Override public String describeTest() { String message = "IndividualTest, login=" + getRoleLevel() + "(" - + getUsername() + ")"; + + getUserUri() + ")"; if (individual == null) { message += ", individual=null"; } else { @@ -596,7 +596,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends @Override public String describeTest() { String message = "VClassTest, login=" + getRoleLevel() + "(" - + getUsername() + ")"; + + getUserUri() + ")"; if (vClass == null) { message += ", vClass=null"; } else { @@ -624,7 +624,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends @Override public String describeTest() { String message = "DataPropertyTest, login=" + getRoleLevel() + "(" - + getUsername() + ")"; + + getUserUri() + ")"; if (dataProperty == null) { message += ", dataProperty=null"; } else { @@ -652,7 +652,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends @Override public String describeTest() { String message = "ObjectPropertyTest, login=" + getRoleLevel() - + "(" + getUsername() + ")"; + + "(" + getUserUri() + ")"; if (objectProperty == null) { message += ", objectProperty=null"; } else { @@ -695,7 +695,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends @Override public String describeTest() { String message = "DataPropertyStatementTest, login=" - + getRoleLevel() + "(" + getUsername() + ")"; + + getRoleLevel() + "(" + getUserUri() + ")"; if (subject == null) { message += ", subject=null"; @@ -752,7 +752,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends @Override public String describeTest() { String message = "ObjectPropertyStatementTest, login=" - + getRoleLevel() + "(" + getUsername() + ")"; + + getRoleLevel() + "(" + getUserUri() + ")"; if (subject == null) { message += ", subject=null";