1 16 package org.directwebremoting.impl; 17 18 import java.lang.reflect.Method ; 19 import java.lang.reflect.Modifier ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.directwebremoting.WebContextFactory; 31 import org.directwebremoting.extend.AccessControl; 32 import org.directwebremoting.extend.AccessDeniedException; 33 import org.directwebremoting.extend.Creator; 34 import org.directwebremoting.extend.LoginRequiredException; 35 import org.directwebremoting.util.Messages; 36 37 41 public class DefaultAccessControl implements AccessControl 42 { 43 46 public void assertExecutionIsPossible(Creator creator, String className, Method method) throws SecurityException 47 { 48 assertIsRestrictedByRole(className, method); 49 assertIsDisplayable(creator, className, method); 50 } 51 52 55 public void assertIsDisplayable(Creator creator, String className, Method method) throws SecurityException 56 { 57 assertIsMethodPublic(method); 58 assertIsExecutable(className, method.getName()); 59 assertIsNotOnBaseObject(method); 60 61 if (!exposeInternals) 62 { 63 assertIsClassDwrInternal(creator); 64 assertAreParametersDwrInternal(method); 65 } 66 } 67 68 71 public void addRoleRestriction(String scriptName, String methodName, String role) 72 { 73 String key = scriptName + '.' + methodName; 74 Set roles = (Set ) roleRestrictMap.get(key); 75 if (roles == null) 76 { 77 roles = new HashSet (); 78 roleRestrictMap.put(key, roles); 79 } 80 81 roles.add(role); 82 } 83 84 87 public void addIncludeRule(String scriptName, String methodName) 88 { 89 Policy policy = getPolicy(scriptName); 90 91 if (policy.defaultAllow) 94 { 95 if (policy.rules.size() > 0) 96 { 97 throw new IllegalArgumentException (Messages.getString("DefaultAccessControl.MixedIncludesAndExcludes", scriptName)); 98 } 99 100 policy.defaultAllow = false; 101 } 102 103 policy.rules.add(methodName); 105 } 106 107 110 public void addExcludeRule(String scriptName, String methodName) 111 { 112 Policy policy = getPolicy(scriptName); 113 114 if (!policy.defaultAllow) 117 { 118 if (policy.rules.size() > 0) 119 { 120 throw new IllegalArgumentException (Messages.getString("DefaultAccessControl.MixedIncludesAndExcludes", scriptName)); 121 } 122 123 policy.defaultAllow = true; 124 } 125 126 policy.rules.add(methodName); 128 } 129 130 134 protected void assertIsRestrictedByRole(String scriptName, Method method) 135 { 136 String methodName = method.getName(); 137 138 Set roles = getRoleRestrictions(scriptName, methodName); 140 if (roles != null && !roles.isEmpty()) 141 { 142 HttpServletRequest req = WebContextFactory.get().getHttpServletRequest(); 143 144 assertAuthenticationIsValid(req); 145 assertAllowedByRoles(req, roles); 146 } 147 } 148 149 154 protected Set getRoleRestrictions(String scriptName, String methodName) 155 { 156 String key = scriptName + '.' + methodName; 157 return (Set ) roleRestrictMap.get(key); 158 } 159 160 165 protected void assertAuthenticationIsValid(HttpServletRequest req) throws SecurityException 166 { 167 req.getSession(); 169 170 if (!req.isRequestedSessionIdValid()) 172 { 173 throw new LoginRequiredException(Messages.getString("DefaultAccessControl.DeniedByInvalidSession")); 174 } 175 176 if (req.getRemoteUser() == null) 177 { 178 throw new LoginRequiredException(Messages.getString("DefaultAccessControl.DeniedByAuthenticationRequired")); 179 } 180 } 181 182 188 protected void assertAllowedByRoles(HttpServletRequest req, Set roles) throws SecurityException 189 { 190 for (Iterator it = roles.iterator(); it.hasNext();) 191 { 192 String role = (String ) it.next(); 193 if ("*".equals(role) || req.isUserInRole(role)) 194 { 195 return; 196 } 197 } 198 199 throw new AccessDeniedException(Messages.getString("DefaultAccessControl.DeniedByJ2EERoles", roles.toString())); 200 } 201 202 206 protected void assertIsMethodPublic(Method method) 207 { 208 if (!Modifier.isPublic(method.getModifiers())) 209 { 210 throw new SecurityException (Messages.getString("DefaultAccessControl.DeniedNonPublic")); 211 } 212 } 213 214 218 protected void assertIsNotOnBaseObject(Method method) 219 { 220 if (method.getDeclaringClass() == Object .class) 221 { 222 throw new SecurityException (Messages.getString("DefaultAccessControl.DeniedObjectMethod")); 223 } 224 } 225 226 233 protected void assertIsExecutable(String scriptName, String methodName) throws SecurityException 234 { 235 Policy policy = (Policy) policyMap.get(scriptName); 236 if (policy == null) 237 { 238 return; 239 } 240 241 String match = null; 243 for (Iterator it = policy.rules.iterator(); it.hasNext() && match == null;) 244 { 245 String test = (String ) it.next(); 246 247 if (methodName.equals(test)) 250 { 251 match = test; 252 } 253 } 254 255 if (policy.defaultAllow && match != null) 256 { 257 throw new SecurityException (Messages.getString("DefaultAccessControl.DeniedByAccessRules")); 261 } 262 263 267 if (!policy.defaultAllow && match == null) 268 { 269 throw new SecurityException (Messages.getString("DefaultAccessControl.DeniedByAccessRules")); 273 } 274 } 275 276 280 protected void assertAreParametersDwrInternal(Method method) 281 { 282 for (int j = 0; j < method.getParameterTypes().length; j++) 283 { 284 Class paramType = method.getParameterTypes()[j]; 285 286 if (paramType.getName().startsWith(PACKAGE_DWR_DENY)) 287 { 288 throw new SecurityException (Messages.getString("DefaultAccessControl.DeniedParamDWR")); 289 } 290 } 291 } 292 293 297 protected void assertIsClassDwrInternal(Creator creator) 298 { 299 if (creator.getType().getName().startsWith(PACKAGE_DWR_DENY)) 300 { 301 throw new SecurityException (Messages.getString("DefaultAccessControl.DeniedCoreDWR")); 302 } 303 } 304 305 310 protected Policy getPolicy(String type) 311 { 312 Policy policy = (Policy) policyMap.get(type); 313 if (policy == null) 314 { 315 policy = new Policy(); 316 policyMap.put(type, policy); 317 } 318 319 return policy; 320 } 321 322 325 public void setExposeInternals(boolean exposeInternals) 326 { 327 this.exposeInternals = exposeInternals; 328 } 329 330 334 protected boolean exposeInternals = false; 335 336 339 protected Map policyMap = new HashMap (); 340 341 344 protected Map roleRestrictMap = new HashMap (); 345 346 349 static class Policy 350 { 351 boolean defaultAllow = true; 352 List rules = new ArrayList (); 353 } 354 355 358 protected static final String PACKAGE_DWR_DENY = "org.directwebremoting."; 359 } 360 | Popular Tags |