NIHVIVO-2279 Remove username from LoginStatusBean

This commit is contained in:
j2blake 2011-06-06 15:47:56 +00:00
parent ccb6cc549d
commit 6f67a4da86
7 changed files with 54 additions and 57 deletions

View file

@ -22,7 +22,7 @@ public class LoginStatusBean {
/** A bean to return when the user has not logged in. */ /** A bean to return when the user has not logged in. */
private static final LoginStatusBean DUMMY_BEAN = new LoginStatusBean("", private static final LoginStatusBean DUMMY_BEAN = new LoginStatusBean("",
"", AuthenticationSource.UNKNOWN); AuthenticationSource.UNKNOWN);
/** The bean is attached to the session by this name. */ /** The bean is attached to the session by this name. */
private static final String ATTRIBUTE_NAME = "loginStatus"; private static final String ATTRIBUTE_NAME = "loginStatus";
@ -96,17 +96,26 @@ public class LoginStatusBean {
return null; return null;
} }
if (!getBean(session).isLoggedIn()) {
return null;
}
ServletContext ctx = session.getServletContext(); ServletContext ctx = session.getServletContext();
WebappDaoFactory wadf = (WebappDaoFactory) ctx WebappDaoFactory wadf = (WebappDaoFactory) ctx
.getAttribute("webappDaoFactory"); .getAttribute("webappDaoFactory");
UserDao userDao = wadf.getUserDao(); if (wadf == null) {
log.error("No WebappDaoFactory");
if (getBean(session).isLoggedIn()) {
String userUri = getBean(session).getUserURI();
return userDao.getUserByURI(userUri);
} else {
return null; 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 userURI;
private final String username;
private final AuthenticationSource authenticationSource; private final AuthenticationSource authenticationSource;
public LoginStatusBean(String userURI, String username, public LoginStatusBean(String userURI,
AuthenticationSource authenticationSource) { AuthenticationSource authenticationSource) {
this.userURI = userURI; this.userURI = userURI;
this.username = username;
this.authenticationSource = authenticationSource; this.authenticationSource = authenticationSource;
} }
@ -132,10 +139,6 @@ public class LoginStatusBean {
return userURI; return userURI;
} }
public String getUsername() {
return username;
}
public AuthenticationSource getAuthenticationSource() { public AuthenticationSource getAuthenticationSource() {
return authenticationSource; return authenticationSource;
} }
@ -150,8 +153,8 @@ public class LoginStatusBean {
@Override @Override
public String toString() { public String toString() {
return "LoginStatusBean[userURI=" + userURI + ", username=" + username return "LoginStatusBean[userURI=" + userURI + ", authenticationSource="
+ ", authenticationSource=" + authenticationSource + "]"; + authenticationSource + "]";
} }
} }

View file

@ -11,7 +11,6 @@ import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration; 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.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
@ -104,18 +104,12 @@ public class CommonIdentifierBundleFactory implements IdentifierBundleFactory {
HttpServletRequest req) { HttpServletRequest req) {
Collection<Individual> individuals = new ArrayList<Individual>(); Collection<Individual> individuals = new ArrayList<Individual>();
LoginStatusBean bean = LoginStatusBean.getBean(req); User user = LoginStatusBean.getCurrentUser(req);
String username = bean.getUsername(); if (user == null) {
if (!bean.isLoggedIn()) {
log.debug("No Associated Individuals: not logged in."); log.debug("No Associated Individuals: not logged in.");
return individuals; return individuals;
} }
String username = user.getUsername();
if (StringUtils.isEmpty(username)) {
log.debug("No Associated Individuals: username is empty.");
return individuals;
}
WebappDaoFactory wdf = (WebappDaoFactory) context WebappDaoFactory wdf = (WebappDaoFactory) context
.getAttribute("webappDaoFactory"); .getAttribute("webappDaoFactory");

View file

@ -102,20 +102,20 @@ public class BasicAuthenticator extends Authenticator {
recordLoginOnUserRecord(user); recordLoginOnUserRecord(user);
String userUri = user.getURI(); String userUri = user.getURI();
recordLoginWithOrWithoutUserAccount(username, userUri, authSource); recordLoginWithOrWithoutUserAccount(userUri, authSource);
} }
@Override @Override
public void recordLoginWithoutUserAccount(String username, public void recordLoginWithoutUserAccount(String username,
String individualUri, AuthenticationSource authSource) { 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. */ /** This much is in common on login, whether or not you have a user account. */
private void recordLoginWithOrWithoutUserAccount(String username, private void recordLoginWithOrWithoutUserAccount(String userUri,
String userUri, AuthenticationSource authSource) { AuthenticationSource authSource) {
HttpSession session = request.getSession(); HttpSession session = request.getSession();
createLoginStatusBean(username, userUri, authSource, session); createLoginStatusBean(userUri, authSource, session);
setSessionTimeoutLimit(session); setSessionTimeoutLimit(session);
recordInUserSessionMap(userUri, session); recordInUserSessionMap(userUri, session);
notifyOtherUsers(userUri, session); notifyOtherUsers(userUri, session);
@ -135,9 +135,9 @@ public class BasicAuthenticator extends Authenticator {
/** /**
* Put the login bean into the session. * Put the login bean into the session.
*/ */
private void createLoginStatusBean(String username, String userUri, private void createLoginStatusBean(String userUri,
AuthenticationSource authSource, HttpSession session) { AuthenticationSource authSource, HttpSession session) {
LoginStatusBean lsb = new LoginStatusBean(userUri, username, authSource); LoginStatusBean lsb = new LoginStatusBean(userUri, authSource);
LoginStatusBean.setBean(session, lsb); LoginStatusBean.setBean(session, lsb);
log.debug("Adding status bean: " + lsb); log.debug("Adding status bean: " + lsb);
} }
@ -243,10 +243,10 @@ public class BasicAuthenticator extends Authenticator {
return; return;
} }
String username = loginBean.getUsername(); String userUri = loginBean.getUserURI();
User user = userDao.getUserByUsername(username); User user = userDao.getUserByURI(userUri);
if (user == null) { if (user == null) {
log.error("Unable to retrieve user " + username + " from model"); log.error("Unable to retrieve user " + userUri + " from model");
return; return;
} }

View file

@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.beans.User;
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; 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? * 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? */ /** Is there an Individual associated with this user? */
private String getAssociatedIndividualUri() { private String getAssociatedIndividualUri() {
String username = LoginStatusBean.getBean(request).getUsername(); User user = LoginStatusBean.getCurrentUser(request);
if (username == null) { if (user == null) {
log.warn("Not logged in? How did we get here?"); log.warn("Not logged in? How did we get here?");
return null; return null;
} }
String username = user.getUsername();
List<String> uris = Authenticator.getInstance(request) List<String> uris = Authenticator.getInstance(request)
.getAssociatedIndividualUris(username); .getAssociatedIndividualUris(username);
@ -105,19 +108,17 @@ public class LoginRedirector {
+ "but the system contains no profile for you."; + "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 backString = "";
String greeting = bean.getUsername(); String greeting = "";
User user = LoginStatusBean.getCurrentUser(request);
if (user != null) { if (user != null) {
greeting = user.getUsername();
if (user.getLoginCount() > 1) { if (user.getLoginCount() > 1) {
backString = " back"; backString = " back";
} }
String name = user.getFirstName(); String name = user.getFirstName();
if ((name != null) && (name.length() > 0)) { if (!StringUtils.isEmpty(name)) {
greeting = name; greeting = name;
} }
} }

View file

@ -152,8 +152,7 @@ public class AuthenticatorStub extends Authenticator {
recordedLogins.add(username); recordedLogins.add(username);
User user = getUserByUsername(username); User user = getUserByUsername(username);
LoginStatusBean lsb = new LoginStatusBean(user.getURI(), username, LoginStatusBean lsb = new LoginStatusBean(user.getURI(), authSource);
authSource);
LoginStatusBean.setBean(request.getSession(), lsb); LoginStatusBean.setBean(request.getSession(), lsb);
} }

View file

@ -416,7 +416,7 @@ public class AuthenticateTest extends AbstractTestClass {
@Test @Test
public void alreadyLoggedIn() { public void alreadyLoggedIn() {
LoginStatusBean statusBean = new LoginStatusBean(OLD_DBA_URI, LoginStatusBean statusBean = new LoginStatusBean(OLD_DBA_URI,
OLD_DBA_NAME, AuthenticationSource.INTERNAL); AuthenticationSource.INTERNAL);
LoginStatusBean.setBean(session, statusBean); LoginStatusBean.setBean(session, statusBean);
setRequestFromLoginLink(URL_WITH_LINK); setRequestFromLoginLink(URL_WITH_LINK);

View file

@ -71,11 +71,11 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
boolean expectedResult; boolean expectedResult;
public String getUsername() { public String getUserUri() {
if (loginStatus == null) { if (loginStatus == null) {
return "nobody"; return "nobody";
} else { } else {
return loginStatus.getUsername(); return loginStatus.getUserURI();
} }
} }
@ -170,7 +170,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
private static LoginStatusBean loginStatusBean(User user, private static LoginStatusBean loginStatusBean(User user,
AuthenticationSource auth) { 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", private static final VClass PUBLIC_VCLASS = vClass("PUBLIC_vclass",
@ -568,7 +568,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
@Override @Override
public String describeTest() { public String describeTest() {
String message = "IndividualTest, login=" + getRoleLevel() + "(" String message = "IndividualTest, login=" + getRoleLevel() + "("
+ getUsername() + ")"; + getUserUri() + ")";
if (individual == null) { if (individual == null) {
message += ", individual=null"; message += ", individual=null";
} else { } else {
@ -596,7 +596,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
@Override @Override
public String describeTest() { public String describeTest() {
String message = "VClassTest, login=" + getRoleLevel() + "(" String message = "VClassTest, login=" + getRoleLevel() + "("
+ getUsername() + ")"; + getUserUri() + ")";
if (vClass == null) { if (vClass == null) {
message += ", vClass=null"; message += ", vClass=null";
} else { } else {
@ -624,7 +624,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
@Override @Override
public String describeTest() { public String describeTest() {
String message = "DataPropertyTest, login=" + getRoleLevel() + "(" String message = "DataPropertyTest, login=" + getRoleLevel() + "("
+ getUsername() + ")"; + getUserUri() + ")";
if (dataProperty == null) { if (dataProperty == null) {
message += ", dataProperty=null"; message += ", dataProperty=null";
} else { } else {
@ -652,7 +652,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
@Override @Override
public String describeTest() { public String describeTest() {
String message = "ObjectPropertyTest, login=" + getRoleLevel() String message = "ObjectPropertyTest, login=" + getRoleLevel()
+ "(" + getUsername() + ")"; + "(" + getUserUri() + ")";
if (objectProperty == null) { if (objectProperty == null) {
message += ", objectProperty=null"; message += ", objectProperty=null";
} else { } else {
@ -695,7 +695,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
@Override @Override
public String describeTest() { public String describeTest() {
String message = "DataPropertyStatementTest, login=" String message = "DataPropertyStatementTest, login="
+ getRoleLevel() + "(" + getUsername() + ")"; + getRoleLevel() + "(" + getUserUri() + ")";
if (subject == null) { if (subject == null) {
message += ", subject=null"; message += ", subject=null";
@ -752,7 +752,7 @@ public class HiddenFromDisplayBelowRoleLevelFilterTest extends
@Override @Override
public String describeTest() { public String describeTest() {
String message = "ObjectPropertyStatementTest, login=" String message = "ObjectPropertyStatementTest, login="
+ getRoleLevel() + "(" + getUsername() + ")"; + getRoleLevel() + "(" + getUserUri() + ")";
if (subject == null) { if (subject == null) {
message += ", subject=null"; message += ", subject=null";