NIHVIVO-736 Create the <vitro:confirmLoginStatus> custom JSP tag.
This commit is contained in:
parent
58089feaf3
commit
2f5580ab76
2 changed files with 142 additions and 0 deletions
47
webapp/config/tlds/VitroUtils.tld
Normal file
47
webapp/config/tlds/VitroUtils.tld
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<!-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
|
||||
|
||||
<taglib
|
||||
xmlns="http://java.sun.com/xml/ns/j2ee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
|
||||
version="2.0">
|
||||
|
||||
<display-name>Vitro Utilities taglib</display-name>
|
||||
<tlib-version>1.0</tlib-version>
|
||||
|
||||
<short-name>Library of JSP utility tags for Vitro</short-name>
|
||||
<!--
|
||||
This library has an assortment of tags for tasks that are common to many Vitro pages.
|
||||
-->
|
||||
|
||||
<tag>
|
||||
<name>confirmLoginStatus</name>
|
||||
<display-name>Confirm that the user is logged in as required</display-name>
|
||||
<description>
|
||||
Check to see that the user is logged in (optionally at a certain minimum level).
|
||||
If not, then redirect them to the login page, with the current request stored as
|
||||
a post-login destination.
|
||||
|
||||
If "level" is specified, then the user must be logged in at least at that level
|
||||
in order to avoid being redirected. "level" may be a String like "DBA" or "EDITOR",
|
||||
or it may be an integer like 50 or 4.
|
||||
|
||||
If "bean" is specified, it is taken as the name of a request attribute where the
|
||||
LoginStatusBean will be stored.
|
||||
</description>
|
||||
<tag-class>edu.cornell.mannlib.vitro.webapp.web.jsptags.ConfirmLoginStatus</tag-class>
|
||||
<body-content>empty</body-content>
|
||||
<attribute>
|
||||
<name>level</name>
|
||||
<required>false</required>
|
||||
<rtexprvalue>true</rtexprvalue>
|
||||
</attribute>
|
||||
<attribute>
|
||||
<name>bean</name>
|
||||
<required>false</required>
|
||||
<rtexprvalue>true</rtexprvalue>
|
||||
</attribute>
|
||||
</tag>
|
||||
|
||||
</taglib>
|
|
@ -0,0 +1,95 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.jsptags;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.tagext.BodyTagSupport;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
||||
|
||||
/**
|
||||
* JSP tag to generate the HTML of links for edit, delete or add of a Property.
|
||||
*
|
||||
* Maybe we should have a mode where it just sets a var to a map with "href" =
|
||||
* "edit/editDatapropDispatch.jsp?subjectUri=..." and "type" = "delete"
|
||||
*
|
||||
* @author bdc34
|
||||
*
|
||||
*/
|
||||
public class ConfirmLoginStatus extends BodyTagSupport {
|
||||
private static final Log log = LogFactory.getLog(ConfirmLoginStatus.class);
|
||||
|
||||
int level;
|
||||
String beanAttributeName;
|
||||
|
||||
public String getLevel() {
|
||||
return String.valueOf(level);
|
||||
}
|
||||
|
||||
public void setLevel(String levelString) {
|
||||
if ("DBA".equals(levelString)) {
|
||||
this.level = LoginStatusBean.DBA;
|
||||
} else if ("CURATOR".equals(levelString)) {
|
||||
this.level = LoginStatusBean.CURATOR;
|
||||
} else if ("EDITOR".equals(levelString)) {
|
||||
this.level = LoginStatusBean.EDITOR;
|
||||
} else if ("NON_EDITOR".equals(levelString)) {
|
||||
this.level = LoginStatusBean.NON_EDITOR;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Level attribute '"
|
||||
+ levelString + "' is not valid.");
|
||||
}
|
||||
}
|
||||
|
||||
public String getBean() {
|
||||
return this.beanAttributeName;
|
||||
}
|
||||
|
||||
public void setbean(String beanAttributeName) {
|
||||
this.beanAttributeName = beanAttributeName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int doEndTag() throws JspException {
|
||||
LoginStatusBean loginBean = LoginStatusBean.getBean(getRequest());
|
||||
if (loginBean.isLoggedInAtLeast(level)) {
|
||||
return setBeanAndReturn(loginBean);
|
||||
} else {
|
||||
return redirectAndSkipPage();
|
||||
}
|
||||
}
|
||||
|
||||
private int setBeanAndReturn(LoginStatusBean loginBean) {
|
||||
if (beanAttributeName != null) {
|
||||
getRequest().setAttribute(beanAttributeName, loginBean);
|
||||
}
|
||||
return EVAL_PAGE;
|
||||
}
|
||||
|
||||
private int redirectAndSkipPage() throws JspException {
|
||||
try {
|
||||
VitroHttpServlet.redirectToLoginPage(getRequest(), getResponse());
|
||||
return SKIP_PAGE;
|
||||
} catch (IOException ioe) {
|
||||
throw new JspException(
|
||||
"<vitro:confirmLoginStatus> could not redirect to login page",
|
||||
ioe);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpServletRequest getRequest() {
|
||||
return ((HttpServletRequest) pageContext.getRequest());
|
||||
}
|
||||
|
||||
private HttpServletResponse getResponse() {
|
||||
return (HttpServletResponse) pageContext.getResponse();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue