1 19 20 package com.sslexplorer.policyframework; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.util.LabelValueBean; 31 32 import com.sslexplorer.properties.ProfilesFactory; 33 import com.sslexplorer.properties.Property; 34 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey; 35 import com.sslexplorer.security.AuthenticationScheme; 36 import com.sslexplorer.security.Constants; 37 import com.sslexplorer.security.LogonControllerFactory; 38 import com.sslexplorer.security.SessionInfo; 39 import com.sslexplorer.security.SystemDatabaseFactory; 40 import com.sslexplorer.security.User; 41 42 47 public class ResourceUtil { 48 49 final static Log log = LogFactory.getLog(ResourceUtil.class); 50 51 54 private ResourceUtil() { 55 } 56 57 68 public static List filterResources(User user, List resources, boolean includeSuperUser) throws Exception { 69 List validResources = new ArrayList (); 70 for (Iterator i = resources.iterator(); i.hasNext();) { 71 Resource p = (Resource) i.next(); 72 if (p instanceof OwnedResource && ((OwnedResource) p).getOwnerUsername() != null 74 && !((OwnedResource) p).getOwnerUsername().equals("")) { 75 if (((OwnedResource) p).getOwnerUsername().equals(user.getPrincipalName())) { 76 validResources.add(p); 77 } 78 } else { 79 if (PolicyDatabaseFactory.getInstance().isPrincipalAllowed(user, p, includeSuperUser)) { 80 validResources.add(p); 81 } 82 } 83 } 84 return validResources; 85 86 } 87 88 private static void addResources(User user, boolean includeSuperUser, List validResources, Resource p) throws Exception { 89 if (p instanceof OwnedResource && ((OwnedResource) p).getOwnerUsername() != null 91 && !((OwnedResource) p).getOwnerUsername().equals("")) { 92 if (((OwnedResource) p).getOwnerUsername().equals(user.getPrincipalName())) { 93 validResources.add(p); 94 } 95 } else { 96 if (PolicyDatabaseFactory.getInstance().isPrincipalAllowed(user, p, includeSuperUser)) { 97 validResources.add(p); 98 } 99 } 100 } 101 102 110 public static List setAvailableProfiles(SessionInfo session) throws Exception { 111 User user = LogonControllerFactory.getInstance().getUser(session.getHttpSession(), null); 112 List profiles = filterResources(user, ProfilesFactory.getInstance().getPropertyProfiles( 113 user.getPrincipalName(), true, session.getUser().getRealm().getResourceId()), false); 114 session.getHttpSession().setAttribute(Constants.PROFILES, profiles); 115 return profiles; 116 } 117 118 125 public static List resourceListAsLabelValueBeanList(List resourceList) { 126 List l = new ArrayList (); 127 Resource r; 128 for (Iterator i = resourceList.iterator(); i.hasNext();) { 129 r = (Resource) i.next(); 130 l.add(new LabelValueBean(r.getResourceName(), String.valueOf(r.getResourceId()))); 131 } 132 return l; 133 } 134 135 142 public static List filterOwned(List resources) { 143 List l = new ArrayList (); 144 for (Iterator i = resources.iterator(); i.hasNext();) { 145 Resource resource = (Resource) i.next(); 146 if (resource instanceof OwnedResource && ((OwnedResource) resource).getOwnerUsername() == null) { 147 l.add(resource); 148 } 149 } 150 return l; 151 } 152 153 169 public static boolean isManageableResource(Resource resource, User user, Permission permission) throws Exception { 170 boolean b = false; 171 if (LogonControllerFactory.getInstance().isAdministrator(user)){ 172 return true; 173 } 174 else{ 175 b = PolicyDatabaseFactory.getInstance().isPermitted(resource.getResourceType(), new Permission[] {permission}, user, false); 176 } 177 178 return b; 179 } 180 181 189 public static boolean resourceItemListContainsResource(List items, Resource resource) { 190 ResourceItem ri; 191 for (Iterator i = items.iterator(); i.hasNext();) { 192 ri = (ResourceItem) i.next(); 193 if (ri.getResource().equals(resource)) { 194 return true; 195 } 196 } 197 return false; 198 } 199 200 212 public static void checkResourceManagementRights(Resource resource, SessionInfo session, Permission[] permissions) 213 throws NoPermissionException { 214 for (int i = 0; i < permissions.length; i++) { 215 try { 216 ResourceType resourceType = resource.getResourceType(); 217 if (session.getNavigationContext() == SessionInfo.MANAGEMENT_CONSOLE_CONTEXT) { 220 try { 221 if (!ResourceUtil.isManageableResource(resource, session.getUser(), permissions[i])) { 222 throw new NoPermissionException("You do not have permission to manage this resource.", session 223 .getUser(), resourceType); 224 } 225 } catch (NoPermissionException npe) { 226 throw npe; 227 } catch (Exception e) { 228 throw new NoPermissionException("Failed to determine if resource is manangeable.", session.getUser(), 229 resourceType); 230 } 231 } 232 else if (session.getNavigationContext() == SessionInfo.USER_CONSOLE_CONTEXT) { 234 if (!(resource instanceof OwnedResource)) { 235 throw new NoPermissionException("You may not managed this resource here.", session.getUser(), resourceType); 236 } else { 237 if (!(session.getUser().getPrincipalName().equals(((OwnedResource) resource).getOwnerUsername()))) { 238 throw new NoPermissionException("You do not have permission to manage this resource.", session 239 .getUser(), resourceType); 240 } 241 } 242 } else { 243 throw new NoPermissionException("You may not manage this resource here.", session.getUser(), resourceType); 244 } 245 break; 246 } catch (NoPermissionException npe) { 247 if (i == (permissions.length - 1)) { 248 throw npe; 249 } 250 } 251 } 252 253 } 254 255 264 public static void checkResourceAccessRights(Resource resource, SessionInfo session) throws NoPermissionException { 265 ResourceType resourceType = resource.getResourceType(); 266 if (session.getNavigationContext() == SessionInfo.MANAGEMENT_CONSOLE_CONTEXT) { 268 try { 269 if (!ResourceUtil.isManageableResource(resource, session.getUser(), null)) { 270 throw new NoPermissionException("You do not have permission to access this resource.", session.getUser(), 271 resourceType); 272 } 273 } catch (NoPermissionException npe) { 274 throw npe; 275 } catch (Exception e) { 276 throw new NoPermissionException("Failed to determine if resource is accessable.", session.getUser(), resourceType); 277 } 278 } 279 else if (session.getNavigationContext() == SessionInfo.USER_CONSOLE_CONTEXT) { 281 if (!(resource instanceof OwnedResource) 282 || (resource instanceof OwnedResource && ((OwnedResource) resource).getOwnerUsername() == null)) { 283 try { 284 if (!PolicyDatabaseFactory.getInstance().isPrincipalAllowed(session.getUser(), resource, false)) { 286 throw new NoPermissionException("You may not access this resource here.", session.getUser(), resourceType); 287 } 288 } catch (NoPermissionException npe) { 289 throw npe; 290 } catch (Exception e) { 291 throw new NoPermissionException("Failed to determine if resource is accessable.", session.getUser(), 292 resourceType); 293 } 294 } else { 295 if (!(session.getUser().getPrincipalName().equals(((OwnedResource) resource).getOwnerUsername()))) { 297 throw new NoPermissionException("You do not have permission to access this resource.", session.getUser(), 298 resourceType); 299 } 300 } 301 } else { 302 throw new NoPermissionException("You may not access this resource here.", session.getUser(), resourceType); 303 } 304 } 305 306 320 public static void checkAccessRightsValid(AccessRights resource, SessionInfo session, String actionTarget) 321 throws NoPermissionException { 322 323 if (actionTarget.equals("edit") || actionTarget.equals("remove") || actionTarget.equals("confirmRemove")) { 324 ResourceUtil.checkResourceManagementRights(resource, session, new Permission[]{}); 325 } else if (actionTarget.equals("view")) { 326 try { 327 List l = LogonControllerFactory.getInstance().isAdministrator(session.getUser()) ? new ArrayList () 328 : PolicyDatabaseFactory.getInstance().getPermittingAccessRights(null, null, null, 329 session.getUser()); 330 if (!l.contains(resource)) { 331 throw new NoPermissionException("Permission denied.", session.getUser(), 332 PolicyConstants.ACCESS_RIGHTS_RESOURCE_TYPE); 333 } 334 } catch (NoPermissionException npe) { 335 throw npe; 336 } catch (Exception e) { 337 throw new NoPermissionException("Failed to determine management rights.", session.getUser(), 338 PolicyConstants.ACCESS_RIGHTS_RESOURCE_TYPE); 339 } 340 } else { 341 throw new Error ("checkValid() only supports edit, remove or view here, not '" + actionTarget + "'."); 342 } 343 344 } 345 346 351 public static List getSignonAuthenticationSchemeIDs(User user) throws Exception { 352 List <Integer > resourceIds = PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(user, 353 PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE); 354 List <Integer > filteredResourceIDs = new ArrayList <Integer >(); 355 for (Integer integer : resourceIds) { 356 AuthenticationScheme authenticationScheme = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequence(integer); 357 if (authenticationScheme != null && !authenticationScheme.isSystemScheme()) 359 filteredResourceIDs.add(authenticationScheme.getResourceId()); 360 } 361 return filteredResourceIDs; 362 } 363 364 370 public static List getGrantedResources(SessionInfo session) throws Exception { 371 372 List allResources = new ArrayList (); 373 List types = PolicyDatabaseFactory.getInstance().getResourceTypes(null); 374 375 for(Iterator it = types.iterator(); it.hasNext();) { 376 ResourceType type = (ResourceType) it.next(); 377 378 allResources.addAll(ResourceUtil.getGrantedResource(session, type)); 379 } 380 381 return allResources; 382 } 383 384 393 public static List getGrantedResource(SessionInfo session, ResourceType resourceType) throws Exception { 394 List l = new ArrayList (); 395 List granted = PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(session.getUser(), resourceType); 396 for (Iterator i = granted.iterator(); i.hasNext();) { 397 Integer r = (Integer ) i.next(); 398 Resource resource = resourceType.getResourceById(r.intValue()); 399 if(resource == null) { 400 log.warn("Could not locate resource with ID of " + r.intValue() + " for type " + resourceType.getResourceTypeId()); 401 } 402 else { 403 if (isPolicyResourceTypeEnforceable(resourceType) 404 && Property.getPropertyBoolean(new SystemConfigKey("security.enforce.policy.resource.access"))) { 405 for (Iterator iter = PolicyDatabaseFactory.getInstance().getPoliciesAttachedToResource(resource, 406 session.getUser().getRealm()).iterator(); iter.hasNext();) { 407 Policy element = (Policy) iter.next(); 408 List authSchemePolicies = (List ) session.getHttpSession().getAttribute("auth.scheme.policies"); 409 if (authSchemePolicies != null && (authSchemePolicies).contains(element)) { 410 l.add(resource); 411 } 412 } 413 } else { 414 l.add(resource); 415 } 416 } 417 } 418 return l; 419 } 420 421 public static boolean isPolicyResourceTypeEnforceable(ResourceType rt) { 422 if (rt.equals(PolicyConstants.PROFILE_RESOURCE_TYPE) || rt.equals(PolicyConstants.ACCESS_RIGHTS_RESOURCE_TYPE) 423 || rt.equals(PolicyConstants.POLICY_RESOURCE_TYPE) 424 || rt.equals(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE)) 425 return false; 426 else 427 return true; 428 } 429 430 431 440 public static List filterResourceIdsForGlobalFavorites(List resources, ResourceType resourceType) throws Exception { 441 List l = new ArrayList (); 442 for (Iterator i = resources.iterator(); i.hasNext();) { 443 Integer r = (Integer ) i.next(); 444 if (SystemDatabaseFactory.getInstance().getFavorite(resourceType.getResourceTypeId(), null, r.intValue()) != null) { 445 l.add(r); 446 } 447 } 448 return l; 449 } 450 451 459 public static void setResourceGlobalFavorite(Resource resource, boolean addToFavorites) throws Exception { 460 if(addToFavorites != isResourceGlobalFavorite(resource)) { 461 if(addToFavorites) { 462 SystemDatabaseFactory.getInstance().addFavorite(resource.getResourceType().getResourceTypeId(), resource.getResourceId(), null); 463 } 464 else { 465 SystemDatabaseFactory.getInstance().removeFavorite(resource.getResourceType().getResourceTypeId(), resource.getResourceId(), null); 466 } 467 } 468 } 469 470 478 public static boolean isResourceGlobalFavorite(Resource resource) throws Exception { 479 return SystemDatabaseFactory.getInstance().getFavorite(resource.getResourceType().getResourceTypeId(), null, resource.getResourceId()) != null; 480 } 481 482 } 483 | Popular Tags |