1 24 package org.ofbiz.minilang.method.ifops; 25 26 import java.util.LinkedList ; 27 import java.util.List ; 28 29 import org.ofbiz.base.util.UtilXml; 30 import org.ofbiz.base.util.string.FlexibleStringExpander; 31 import org.ofbiz.entity.GenericValue; 32 import org.ofbiz.minilang.SimpleMethod; 33 import org.ofbiz.minilang.method.MethodContext; 34 import org.ofbiz.minilang.method.MethodOperation; 35 import org.ofbiz.security.Security; 36 import org.w3c.dom.Element ; 37 38 46 public class IfHasPermission extends MethodOperation { 47 48 protected List subOps = new LinkedList (); 49 protected List elseSubOps = null; 50 51 protected FlexibleStringExpander permissionExdr; 52 protected FlexibleStringExpander actionExdr; 53 54 public IfHasPermission(Element element, SimpleMethod simpleMethod) { 55 super(element, simpleMethod); 56 this.permissionExdr = new FlexibleStringExpander(element.getAttribute("permission")); 57 this.actionExdr = new FlexibleStringExpander(element.getAttribute("action")); 58 59 SimpleMethod.readOperations(element, subOps, simpleMethod); 60 61 Element elseElement = UtilXml.firstChildElement(element, "else"); 62 if (elseElement != null) { 63 elseSubOps = new LinkedList (); 64 SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod); 65 } 66 } 67 68 public boolean exec(MethodContext methodContext) { 69 73 boolean runSubOps = false; 75 76 GenericValue userLogin = methodContext.getUserLogin(); 78 if (userLogin != null) { 79 String permission = methodContext.expandString(permissionExdr); 80 String action = methodContext.expandString(actionExdr); 81 82 Security security = methodContext.getSecurity(); 83 if (action != null && action.length() > 0) { 84 if (security.hasEntityPermission(permission, action, userLogin)) { 86 runSubOps = true; 87 } 88 } else { 89 if (security.hasPermission(permission, userLogin)) { 91 runSubOps = true; 92 } 93 } 94 } 95 96 if (runSubOps) { 97 return SimpleMethod.runSubOps(subOps, methodContext); 98 } else { 99 if (elseSubOps != null) { 100 return SimpleMethod.runSubOps(elseSubOps, methodContext); 101 } else { 102 return true; 103 } 104 } 105 } 106 107 public String rawString() { 108 return "<if-has-permission permission=\"" + this.permissionExdr + "\" action=\"" + this.actionExdr + "\"/>"; 109 } 110 public String expandedString(MethodContext methodContext) { 111 return this.rawString(); 113 } 114 } 115 | Popular Tags |