Get rid of FakeSelfEditing.

This commit is contained in:
j2blake 2011-04-26 21:21:21 +00:00
parent 34c858500d
commit 6c8c6bcfae
6 changed files with 0 additions and 496 deletions

View file

@ -1,70 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.auth.identifier;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.SelfEditingIdentifierFactory.NetId;
import edu.cornell.mannlib.vitro.webapp.auth.policy.RoleBasedPolicy;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
/**
* Attempts to simulate the action of SelfEditingIdentifierFactory.java using the
* request attribute FAKE_SELF_EDIT_NETID.
*/
public class FakeSelfEditingIdentifierFactory implements IdentifierBundleFactory{
public static final String FAKE_SELF_EDIT_NETID = "fakeSelfEditingNetid";
public IdentifierBundle getIdentifierBundle(ServletRequest request,
HttpSession session, ServletContext context) {
WebappDaoFactory wdf = ((WebappDaoFactory)context.getAttribute("webappDaoFactory"));
IdentifierBundle ib = new ArrayIdentifierBundle();
ib.add( RoleBasedPolicy.AuthRole.ANYBODY);
String netid = null;
if( session != null )
netid = (String)session.getAttribute(FAKE_SELF_EDIT_NETID );
if( netid != null ){
NetId netIdToken = new NetId(netid);
ib.add(netIdToken);
SelfEditingConfiguration sec = SelfEditingConfiguration.getBean(request);
String uri = sec.getIndividualUriFromUsername(wdf.getIndividualDao(), netid);
if( uri != null ){
Individual ind = wdf.getIndividualDao().getIndividualByURI(uri);
if( ind != null ){
String causeOfBlacklist = SelfEditingIdentifierFactory.checkForBlacklisted(ind, context);
if( causeOfBlacklist == SelfEditingIdentifierFactory.NOT_BLACKLISTED )
ib.add( new SelfEditingIdentifierFactory.SelfEditing( ind, SelfEditingIdentifierFactory.NOT_BLACKLISTED, true ) );
else
ib.add( new SelfEditingIdentifierFactory.SelfEditing( ind, causeOfBlacklist, true) );
}
}
}
return ib;
}
public static void putFakeIdInSession(String netid, HttpSession session){
session.setAttribute(FAKE_SELF_EDIT_NETID , netid);
}
public static void clearFakeIdInSession( HttpSession session){
session.removeAttribute(FAKE_SELF_EDIT_NETID);
}
public static String getFakeIdFromSession(HttpSession session) {
Object netid = session.getAttribute(FAKE_SELF_EDIT_NETID);
if (netid instanceof String) {
return (String) netid;
} else {
return null;
}
}
}

View file

@ -1,35 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.auth.identifier;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
public class SetupFakeSelfEditingIdentifierFactory implements ServletContextListener{
private static final Log log = LogFactory.getLog(SetupFakeSelfEditingIdentifierFactory.class.getName());
@Override
public void contextInitialized(ServletContextEvent sce) {
WebappDaoFactory wdf = (WebappDaoFactory)sce.getServletContext().getAttribute("webappDaoFactory");
if( wdf == null ){
log.debug("SetupFakeSelfEditingIdentifierFactory: need a " +
"WebappDaoFactory in ServletContext, none found, factory will " +
"not be created");
return;
}
IdentifierBundleFactory ibfToAdd = new FakeSelfEditingIdentifierFactory();
ActiveIdentifierBundleFactories.addFactory(sce, ibfToAdd);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// Nothing to do.
}
}

View file

@ -1,145 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.FakeSelfEditingIdentifierFactory;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseMiscellaneousAdminPages;
/**
* TODO This is caught in the middle of the transition from LoginFormBean to LoginStatusBean.
*/
public class FakeSelfEditController extends VitroHttpServlet {
// TODO When the LoginFormBean goes away, these should too.
private static final String ATTRIBUTE_LOGIN_FORM_BEAN = "loginHandler";
private static final String ATTRIBUTE_LOGIN_FORM_SAVE = "saveLoginHandler";
private static final String ATTRIBUTE_LOGIN_STATUS_BEAN = "loginStatus";
private static final String ATTRIBUTE_LOGIN_STATUS_SAVE = "saveLoginStatus";
private static final Log log = LogFactory
.getLog(FakeSelfEditController.class.getName());
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
super.doGet(request, response);
VitroRequest vreq = new VitroRequest(request);
HttpSession session = request.getSession();
if (!isAuthorized(vreq, session)) {
redirectToLoginPage(request, response);
} else if (vreq.getParameter("force") != null) {
startFaking(vreq, response);
} else if (vreq.getParameter("stopfaking") != null) {
stopFaking(vreq, response, session);
} else {
showTheForm(vreq, response, session);
}
} catch (Exception e) {
log.error("FakeSelfEditController could not forward to view.");
log.error(e, e);
}
}
private boolean isAuthorized(VitroRequest vreq, HttpSession session) {
boolean isFakingAlready = (session.getAttribute(ATTRIBUTE_LOGIN_STATUS_SAVE) != null);
boolean isAdmin = PolicyHelper.isAuthorizedForActions(vreq, new UseMiscellaneousAdminPages());
log.debug("isFakingAlready: " + isFakingAlready + ", isAdmin: " + isAdmin);
return isAdmin || isFakingAlready;
}
private void startFaking(VitroRequest vreq, HttpServletResponse response)
throws IOException {
HttpSession session = vreq.getSession();
String id = vreq.getParameter("netid");
FakeSelfEditingIdentifierFactory.putFakeIdInSession(id, session);
// Remove the login bean - so we are ONLY self-editing
moveAttribute(session, ATTRIBUTE_LOGIN_FORM_BEAN,
ATTRIBUTE_LOGIN_FORM_SAVE);
moveAttribute(session, ATTRIBUTE_LOGIN_STATUS_BEAN,
ATTRIBUTE_LOGIN_STATUS_SAVE);
log.debug("Start faking as " + id);
response.sendRedirect(vreq.getContextPath() + Controllers.ENTITY
+ "?netid=" + id);
}
private void stopFaking(VitroRequest request, HttpServletResponse response,
HttpSession session) throws IOException {
FakeSelfEditingIdentifierFactory.clearFakeIdInSession(session);
// Restore our original login status.
restoreAttribute(session, ATTRIBUTE_LOGIN_FORM_BEAN,
ATTRIBUTE_LOGIN_FORM_SAVE);
restoreAttribute(session, ATTRIBUTE_LOGIN_STATUS_BEAN,
ATTRIBUTE_LOGIN_STATUS_SAVE);
log.debug("Stop faking.");
response.sendRedirect(request.getContextPath() + "/");
}
private void showTheForm(VitroRequest request,
HttpServletResponse response, HttpSession session)
throws ServletException, IOException {
// Logged in as site admin: Form not yet submitted
request.setAttribute("msg", figureMessage(session));
request.setAttribute("title", "Self-Edit Test");
request.setAttribute("bodyJsp", "/admin/fakeselfedit.jsp");
RequestDispatcher rd = request
.getRequestDispatcher(Controllers.BASIC_JSP);
rd.forward(request, response);
}
/**
* Check if already logged in from previous form submission
*/
private String figureMessage(HttpSession session) {
String netid = FakeSelfEditingIdentifierFactory.getFakeIdFromSession(session);
if (netid != null) {
return "You are testing self-editing as '" + netid + "'.";
} else {
return "You have not configured a netid to test self-editing.";
}
}
private void moveAttribute(HttpSession session,
String realAttribute, String saveAttribute) {
Object value = session.getAttribute(realAttribute);
if (value != null) {
session.setAttribute(saveAttribute, value);
session.removeAttribute(realAttribute);
}
}
private void restoreAttribute(HttpSession session,
String realAttribute, String saveAttribute) {
Object value = session.getAttribute(saveAttribute);
if (value != null) {
session.setAttribute(realAttribute, value);
session.removeAttribute(saveAttribute);
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}