1 package org.saf.struts.taglib;
2
3 import org.apache.struts.taglib.logic.ConditionalTagBase;
4
5 import org.saf.struts.exception.SafAuthorizationException;
6 import org.saf.struts.util.SafUtils;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.jsp.JspException;
10
11
12 /***
13 * Checks if a user is authorized to see the body content of this tag.
14 *
15 * @author Wim Tobback
16 * @version 1.0
17 *
18 * @since 1.0
19 */
20 public class AuthorizedTag extends ConditionalTagBase {
21 private static final long serialVersionUID = 1L;
22 private String defId;
23
24 /***
25 * @param defId The defId to set.
26 */
27 public void setDefId(String defId) {
28 this.defId = defId;
29 }
30
31 /***
32 * @see org.apache.struts.taglib.logic.ConditionalTagBase#condition()
33 */
34 protected boolean condition() throws JspException {
35 return condition(true);
36 }
37
38 protected boolean condition(boolean desired) throws JspException {
39 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
40 boolean authorized = false;
41
42 try {
43 authorized = SafUtils.isAuthorized(request, defId);
44 } catch (SafAuthorizationException e) {
45 throw new JspException(e);
46 }
47
48 return (authorized == desired);
49 }
50 }