NIHVIVO-2279 Start transitioning toward UserAccount

This commit is contained in:
j2blake 2011-06-01 21:21:41 +00:00
parent d113061b91
commit dbf7f5b809
2 changed files with 45 additions and 27 deletions

View file

@ -2,12 +2,17 @@
package edu.cornell.mannlib.vedit.beans; package edu.cornell.mannlib.vedit.beans;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.User;
import edu.cornell.mannlib.vitro.webapp.dao.UserDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
/** /**
* An immutable object that records the user's login info as a session * An immutable object that records the user's login info as a session
* attribute. * attribute.
@ -91,6 +96,33 @@ public class LoginStatusBean {
return (LoginStatusBean) o; return (LoginStatusBean) o;
} }
/**
* Get the current user, or null if not logged in.
*/
public static User getCurrentUser(HttpServletRequest request) {
if (request == null) {
return null;
}
return getCurrentUser(request.getSession(false));
}
/**
* Get the current user, or null if not logged in.
*/
public static User getCurrentUser(HttpSession session) {
if (session == null) {
return null;
}
ServletContext ctx = session.getServletContext();
WebappDaoFactory wadf = (WebappDaoFactory) ctx
.getAttribute("webappDaoFactory");
UserDao userDao = wadf.getUserDao();
String userUri = getBean(session).getUserURI();
return userDao.getUserByURI(userUri);
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// the bean // the bean
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@ -129,7 +161,7 @@ public class LoginStatusBean {
} }
public boolean isLoggedIn() { public boolean isLoggedIn() {
return securityLevel > ANYBODY; return authenticationSource != AuthenticationSource.UNKNOWN;
} }
public boolean isLoggedInExactly(int level) { public boolean isLoggedInExactly(int level) {

View file

@ -2,48 +2,34 @@
package edu.cornell.mannlib.vitro.webapp.web.templatemodels; package edu.cornell.mannlib.vitro.webapp.web.templatemodels;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.LoginStatusBean; import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper; import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.RevisionInfoController; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.RevisionInfoController;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.SiteAdminController; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.SiteAdminController;
public class User extends BaseTemplateModel { public class User extends BaseTemplateModel {
private final VitroRequest vreq;
private static final Log log = LogFactory.getLog(User.class);
// TODO JB Modify this to use UserAccount instead of User.
private enum Access { private final edu.cornell.mannlib.vitro.webapp.beans.User currentUser;
FILTER_SECURITY(LoginStatusBean.EDITOR);
private final int requiredLoginLevel;
Access(int requiredLoginLevel) {
this.requiredLoginLevel = requiredLoginLevel;
}
int requiredLoginLevel() {
return this.requiredLoginLevel;
}
}
private LoginStatusBean loginBean = null;
private VitroRequest vreq = null;
public User(VitroRequest vreq) { public User(VitroRequest vreq) {
this.vreq = vreq; this.vreq = vreq;
loginBean = LoginStatusBean.getBean(vreq); this.currentUser = LoginStatusBean.getCurrentUser(vreq);
} }
public boolean isLoggedIn() { public boolean isLoggedIn() {
return loginBean.isLoggedIn(); return currentUser != null;
}
public String getEmailAddress() {
return (currentUser == null) ? "" : currentUser.getUsername();
} }
public String getLoginName() { public String getLoginName() {
return loginBean.getUsername(); return (currentUser == null) ? ""
: (currentUser.getFirstName() + " " + currentUser.getLastName());
} }
public boolean getHasSiteAdminAccess() { public boolean getHasSiteAdminAccess() {