1 16 package com.blandware.atleap.webapp.taglib.core.logic; 17 18 import com.blandware.atleap.webapp.util.core.WebappUtil; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.struts.taglib.TagUtils; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.JspTagException ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.JspFragment ; 28 import javax.servlet.jsp.tagext.SimpleTagSupport ; 29 import java.io.IOException ; 30 import java.util.Arrays ; 31 import java.util.Collection ; 32 33 85 public class IsUserInRoleTag extends SimpleTagSupport { 86 87 protected transient final Log log = LogFactory.getLog(IsUserInRoleTag.class); 88 89 94 protected Object roles; 95 96 101 protected Boolean all; 102 103 106 protected String property; 107 108 111 protected String var; 112 113 117 protected String scope; 118 119 129 public Object getRoles() { 130 return roles; 131 } 132 133 139 public void setRoles(Object roles) { 140 this.roles = roles; 141 } 142 143 153 public Boolean getAll() { 154 return all; 155 } 156 157 163 public void setAll(Boolean all) { 164 this.all = all; 165 } 166 167 177 public String getProperty() { 178 return property; 179 } 180 181 188 public void setProperty(String property) { 189 this.property = property; 190 } 191 192 202 public String getVar() { 203 return var; 204 } 205 206 212 public void setVar(String var) { 213 this.var = var; 214 } 215 216 226 public String getScope() { 227 return scope; 228 } 229 230 236 public void setScope(String scope) { 237 this.scope = scope; 238 } 239 240 246 public void doTag() throws JspException , IOException { 247 248 PageContext pageContext = (PageContext ) getJspContext(); 249 250 if ( all == null ) { 251 all = Boolean.FALSE; 252 } 253 254 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 255 256 Collection rolesCollection = null; 257 if ( roles instanceof String ) { 258 rolesCollection = Arrays.asList(((String ) roles).split(",")); 259 } else if ( roles instanceof Collection ) { 260 rolesCollection = ((Collection ) roles); 261 } else if ( roles instanceof Object [] ) { 262 rolesCollection = Arrays.asList((Object []) roles); 263 } else { 264 String errorMessage = "Only instances of 'java.util.Collection', " + 265 "'java.lang.Object[]' and 'java.lang.String' are supported in 'roles' attribute"; 266 JspTagException e = new JspTagException (errorMessage); 267 throw e; 268 } 269 270 boolean isUserInRole = false; 271 try { 272 isUserInRole = WebappUtil.isUserInRole(rolesCollection, all.booleanValue(), property, request); 273 } catch ( Exception e ) { 274 JspTagException ex = new JspTagException (e); 275 throw ex; 276 } 277 278 if ( var != null ) { 279 int varScope = PageContext.PAGE_SCOPE; 280 if ( scope != null ) { 281 varScope = TagUtils.getInstance().getScope(scope); 282 } 283 pageContext.setAttribute(var, Boolean.valueOf(isUserInRole), varScope); 284 } else if ( isUserInRole ) { 285 JspFragment body = getJspBody(); 286 if ( body != null ) { 287 body.invoke(null); 288 } 289 } 290 } 291 } 292 | Popular Tags |