1 19 20 package com.sslexplorer.core; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import com.sslexplorer.boot.ContextHolder; 32 import com.sslexplorer.policyframework.Permission; 33 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 34 import com.sslexplorer.policyframework.ResourceType; 35 import com.sslexplorer.policyframework.ResourceUtil; 36 import com.sslexplorer.properties.Property; 37 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey; 38 import com.sslexplorer.security.SessionInfo; 39 40 65 public class MenuItem implements Comparable { 66 67 final static Log log = LogFactory.getLog(MenuItem.class); 68 69 71 protected String messageResourcesKey; 72 protected String path; 73 protected List children; 74 protected MenuItem parent; 75 protected boolean leaf; 76 protected int weight; 77 protected int navigationContext; 78 protected ResourceType resourceTypeOfPermissionsRequired; 79 protected ResourceType resourcesOfTypeRequired; 80 protected Permission[] permissionsRequired; 81 82 84 private String id; 85 private String target = "_self"; 86 87 103 public MenuItem(String id, String messageResourcesKey, String path, int weight, boolean leaf, int navigationContext) { 104 this(id, messageResourcesKey, path, weight, leaf, null, navigationContext, null, null, null); 105 } 106 107 126 public MenuItem(String id, String messageResourcesKey, String path, int weight, boolean leaf, String target, 127 int navigationContext) { 128 this(id, messageResourcesKey, path, weight, leaf, target, navigationContext, null, null, null); 129 } 130 131 155 public MenuItem(String id, String messageResourcesKey, String path, int weight, boolean leaf, 156 ResourceType resourceTypeOfPermissionsRequired, Permission[] permissionsRequired) { 157 this(id, messageResourcesKey, path, weight, leaf, null, SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, 158 resourceTypeOfPermissionsRequired, permissionsRequired, null); 159 } 160 161 186 public MenuItem(String id, String messageResourcesKey, String path, int weight, boolean leaf, String target, 187 int navigationContext, ResourceType resourceTypeOfPermissionsRequired, Permission[] permissionsRequired) { 188 this(id, messageResourcesKey, path, weight, leaf, target, navigationContext, resourceTypeOfPermissionsRequired, 189 permissionsRequired, null); 190 } 191 192 219 public MenuItem(String id, String messageResourcesKey, String path, int weight, boolean leaf, String target, 220 int navigationContext, ResourceType resourceTypeOfPermissionsRequired, Permission[] permissionsRequired, 221 ResourceType resourcesOfTypeRequired) { 222 super(); 223 this.navigationContext = navigationContext; 224 this.target = target == null ? "_self" : target; 225 this.id = id; 226 this.leaf = leaf; 227 this.weight = weight; 228 this.messageResourcesKey = messageResourcesKey; 229 this.path = path; 230 this.permissionsRequired = permissionsRequired; 231 this.resourceTypeOfPermissionsRequired = resourceTypeOfPermissionsRequired; 232 this.resourcesOfTypeRequired = resourcesOfTypeRequired; 233 } 234 235 241 public void addChild(MenuItem menuItem) throws IllegalArgumentException { 242 if (isLeaf()) { 243 throw new IllegalArgumentException ("Cannot add child menu items to leaf menu items."); 244 } 245 if (children == null) { 246 children = new ArrayList (); 247 } 248 children.add(menuItem); 249 } 250 251 257 public void removeChild(MenuItem menuItem) throws IllegalArgumentException { 258 if (isLeaf()) { 259 throw new IllegalArgumentException ("Cannot remove child menu items from leaf menu items."); 260 } 261 if (children != null) { 262 children.remove(menuItem); 263 } 264 } 265 266 272 public MenuItem getChild(String id) { 273 if (children != null) { 274 for (Iterator i = children.iterator(); i.hasNext();) { 275 MenuItem it = (MenuItem) i.next(); 276 if (it.getId().equals(id)) { 277 return it; 278 } 279 } 280 } 281 return null; 282 } 283 284 290 public boolean isLeaf() { 291 return leaf; 292 } 293 294 299 public void setParent(MenuItem parent) { 300 this.parent = parent; 301 } 302 303 308 public String getId() { 309 return id; 310 } 311 312 318 public String getMessageResourcesKey() { 319 return messageResourcesKey; 320 } 321 322 329 public String getTarget() { 330 return target; 331 } 332 333 339 public String getPath() { 340 return path; 341 } 342 343 358 public boolean isAvailable(int checkNavigationContext, SessionInfo info, HttpServletRequest request) { 359 if ((ContextHolder.getContext().isSetupMode() && ((navigationContext & SessionInfo.SETUP_CONSOLE_CONTEXT) != 0)) 360 || (navigationContext & checkNavigationContext) != 0 || navigationContext == 0 ) { 361 362 try { 364 if (Property.getPropertyBoolean(new SystemConfigKey("security.enforce.policy.resource.access"))) { 365 if (resourcesOfTypeRequired != null) { 366 if (ResourceUtil.getGrantedResource(info, resourcesOfTypeRequired).size() == 0) { 367 return false; 368 } 369 } 370 } 371 } catch (Exception e) { 372 log.error("Failed to check auth schemes restrictions.", e); 373 return false; 374 } 375 if (resourceTypeOfPermissionsRequired != null) { 376 try { 377 boolean allowed = info != null && PolicyDatabaseFactory.getInstance().isPermitted( 378 resourceTypeOfPermissionsRequired, permissionsRequired, info.getUser(), false); 379 if (!allowed) { 380 if (resourcesOfTypeRequired != null) { 381 return PolicyDatabaseFactory.getInstance().isPrincipalGrantedResourcesOfType(info.getUser(), 382 resourcesOfTypeRequired, null); 383 } 384 return false; 385 } 386 return true; 387 } catch (Exception e) { 388 log.error("Failed to check delegation rights.", e); 389 return false; 390 } 391 } else { 392 try { 393 if (resourcesOfTypeRequired != null) { 394 return info != null && PolicyDatabaseFactory.getInstance().isPrincipalGrantedResourcesOfType(info.getUser(), 395 resourcesOfTypeRequired, null); 396 } 397 } catch (Exception e) { 398 log.error("Failed to check delegation rights.", e); 399 return false; 400 } 401 } 402 return true; 403 } 404 return false; 405 } 406 407 417 public List availableChildren(int checkNavigationContext, SessionInfo info, HttpServletRequest request) { 418 List l = new ArrayList (); 419 if (children != null) { 420 for (Iterator i = children.iterator(); i.hasNext();) { 421 MenuItem it = (MenuItem) i.next(); 422 if (it.isAvailable(navigationContext, info, request)) { 423 l.add(it); 424 } 425 } 426 } 427 return l; 428 } 429 430 435 public boolean isEmpty() { 436 return children == null || children.size() == 0; 437 } 438 439 444 public int compareTo(Object arg0) { 445 return new Integer (weight).compareTo(new Integer (((MenuItem) arg0).weight)); 446 } 447 448 456 public int getNavigationContext() { 457 return navigationContext; 458 } 459 } | Popular Tags |