| 1 28 package net.sf.jguard.jee.taglib; 29 30 31 32 33 import java.security.Principal ; 34 import java.util.ArrayList ; 35 import java.util.Arrays ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Set ; 39 40 import javax.security.auth.Subject ; 41 import javax.servlet.jsp.JspException ; 42 import javax.servlet.jsp.JspTagException ; 43 import javax.servlet.jsp.jstl.core.ConditionalTagSupport; 44 45 import net.sf.jguard.core.CoreConstants; 46 import net.sf.jguard.core.principals.RolePrincipal; 47 import net.sf.jguard.ext.principals.PrincipalUtils; 48 49 import org.apache.commons.logging.Log; 50 import org.apache.commons.logging.LogFactory; 51 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; 52 53 54 61 public class HasPrincipal extends ConditionalTagSupport { 62 63 64 private static final Log logger = LogFactory.getLog(HasPrincipal.class); 65 68 private static final long serialVersionUID = 3257284721280235318L; 69 private String principals; 70 private List principalsArray; 71 private static final String ALL ="ALL"; 72 private static final String ANY ="ANY"; 73 private static final String NONE ="NONE"; 74 75 private String operator = ANY; 77 private Class defaultClassName = RolePrincipal.class; 78 private Class clazz = defaultClassName; 79 private Class [] defaultParameterTypes = new Class []{String .class,String .class}; 80 private Class [] parameterTypes = defaultParameterTypes; 81 private String applicationName; 82 83 84 87 public void setPrincipals(String strUri) { 88 principals = strUri; 89 principalsArray = Arrays.asList(principals.split(",")); 90 } 91 92 93 98 protected boolean condition() throws JspTagException { 99 100 try { 101 this.principals=(String )ExpressionEvaluatorManager.evaluate ("principalsArray", this.principals, String .class, this, pageContext); 102 principalsArray = Arrays.asList(principals.split(",")); 103 } catch (JspException e1) { 104 logger.error("condition()", e1); 105 throw new JspTagException (e1.getMessage()); 106 } 107 if(logger.isDebugEnabled()){ 108 logger.debug("<jguard:authorized> tag uri="+principals); 109 logger.debug("<jguard:authorized> tag operator="+operator); 110 } 111 112 Subject subject = TagUtils.getSubject(this.pageContext); 113 if(subject == null){ 114 return false; 115 } 116 117 Set principals = subject.getPrincipals(); 118 for(int j=0;j<principalsArray.size();j++){ 119 Principal ppal = null; 120 if(clazz.getName().equals(RolePrincipal.class.getName())){ 121 List args = new ArrayList (); 122 args.add(principalsArray.get(j)); 123 124 if(applicationName == null){ 126 applicationName = (String )pageContext.getServletContext().getAttribute(CoreConstants.APPLICATION_NAME); 127 } 128 args.add(applicationName); 129 ppal = PrincipalUtils.getPrincipal(this.clazz,this.parameterTypes,args.toArray()); 130 }else{ 131 ppal = PrincipalUtils.getPrincipal(this.clazz,this.parameterTypes,new Object []{((String )this.principalsArray.get(j)).split(";")}); 132 } 133 134 if (ppal == null){ 135 logger.warn(" wrong arguments in the HasPrincipal tag \n class="+clazz.getName()+"\n parameterTypes="+parameterTypes+"\n principalsArray="+principalsArray); 136 return false; 137 } 138 139 if(!principals.contains(ppal)){ 140 if(operator.equals(ALL)){ 141 return false; 142 } 143 }else{ 144 Iterator it = principals.iterator(); 145 boolean active = false; 146 while(it.hasNext()){ 147 Principal principal = (Principal )it.next(); 148 if(ppal.equals(principal)&& ((RolePrincipal)principal).isActive()){ 149 active = true; 150 break; 151 } 152 } 153 if(active==false){ 154 return false; 155 } 156 if(operator.equals(ANY)){ 158 return true; 159 }else if(operator.equals(NONE)){ 160 return false; 161 } 162 return false; 163 } 164 } 165 166 167 if(operator.equals(ALL) ||operator.equals(NONE) ){ 168 return true; 169 }else if (operator.equals(ANY)){ 170 return false; 171 } 172 173 return false; 174 175 } 176 177 178 181 public String getPrincipals() { 182 return principals; 183 } 184 187 public String getOperator() { 188 return operator; 189 } 190 193 public void setOperator(String operator) { 194 String upper = operator.toUpperCase(); 195 if(upper.equals(ALL)||upper.equals(ANY)||upper.equals(NONE)){ 196 this.operator = upper; 197 } 198 } 199 200 201 public final Class getClazz() { 202 return clazz; 203 } 204 205 206 public final void setClassName(String className) { 207 try { 208 this.clazz = Class.forName(className); 209 } catch (ClassNotFoundException e) { 210 logger.info(" 'className' attribute does not map to an existing or reachable class "); 211 } 212 } 213 214 215 public final Class [] getParameterTypes() { 216 return parameterTypes; 217 } 218 219 220 public String getApplicationName() { 221 return applicationName; 222 } 223 224 225 public void setApplicationName(String applicationName) { 226 this.applicationName = applicationName; 227 } 228 229 } 230 | Popular Tags |