1 24 package org.ofbiz.minilang.method.ifops; 25 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.ofbiz.base.util.UtilProperties; 32 import org.ofbiz.base.util.UtilValidate; 33 import org.ofbiz.base.util.UtilXml; 34 import org.ofbiz.entity.GenericValue; 35 import org.ofbiz.minilang.SimpleMethod; 36 import org.ofbiz.minilang.method.ContextAccessor; 37 import org.ofbiz.minilang.method.MethodContext; 38 import org.ofbiz.minilang.method.MethodOperation; 39 import org.ofbiz.security.Security; 40 import org.w3c.dom.Element ; 41 42 50 public class CheckPermission extends MethodOperation { 51 52 String message = null; 53 String propertyResource = null; 54 boolean isProperty = false; 55 56 57 ContextAccessor acceptUlPartyIdEnvNameAcsr = null; 58 59 PermissionInfo permissionInfo; 60 ContextAccessor errorListAcsr; 61 List altPermissions = null; 62 63 public CheckPermission(Element element, SimpleMethod simpleMethod) { 64 super(element, simpleMethod); 65 permissionInfo = new PermissionInfo(element); 66 this.errorListAcsr = new ContextAccessor(element.getAttribute("error-list-name"), "error_list"); 67 68 Element acceptUserloginPartyElement = UtilXml.firstChildElement(element, "accept-userlogin-party"); 69 if (acceptUserloginPartyElement != null) { 70 acceptUlPartyIdEnvNameAcsr = new ContextAccessor(acceptUserloginPartyElement.getAttribute("party-id-env-name"), "partyId"); 71 } 72 73 List altPermElements = UtilXml.childElementList(element, "alt-permission"); 74 Iterator apeIter = altPermElements.iterator(); 75 if (apeIter.hasNext()) { 76 altPermissions = new LinkedList (); 77 } 78 while (apeIter.hasNext()) { 79 Element altPermElement = (Element ) apeIter.next(); 80 altPermissions.add(new PermissionInfo(altPermElement)); 81 } 82 83 Element failMessage = UtilXml.firstChildElement(element, "fail-message"); 84 Element failProperty = UtilXml.firstChildElement(element, "fail-property"); 85 if (failMessage != null) { 86 this.message = failMessage.getAttribute("message"); 87 this.isProperty = false; 88 } else if (failProperty != null) { 89 this.propertyResource = failProperty.getAttribute("resource"); 90 this.message = failProperty.getAttribute("property"); 91 this.isProperty = true; 92 } 93 } 94 95 public boolean exec(MethodContext methodContext) { 96 boolean hasPermission = false; 97 98 List messages = (List ) errorListAcsr.get(methodContext); 99 if (messages == null) { 100 messages = new LinkedList (); 101 errorListAcsr.put(methodContext, messages); 102 } 103 104 GenericValue userLogin = methodContext.getUserLogin(); 106 if (userLogin != null) { 107 Security security = methodContext.getSecurity(); 108 if (this.permissionInfo.hasPermission(methodContext, userLogin, security)) { 109 hasPermission = true; 110 } 111 112 if (!hasPermission && altPermissions != null) { 114 Iterator altPermIter = altPermissions.iterator(); 115 while (altPermIter.hasNext()) { 116 PermissionInfo altPermInfo = (PermissionInfo) altPermIter.next(); 117 if (altPermInfo.hasPermission(methodContext, userLogin, security)) { 118 hasPermission = true; 119 break; 120 } 121 } 122 } 123 } 124 125 if (!hasPermission && acceptUlPartyIdEnvNameAcsr != null) { 126 String acceptPartyId = (String ) acceptUlPartyIdEnvNameAcsr.get(methodContext); 127 if (UtilValidate.isEmpty(acceptPartyId)) { 128 Map parameters = (Map ) methodContext.getEnv("parameters"); 130 if (parameters != null) { 131 acceptPartyId = (String ) acceptUlPartyIdEnvNameAcsr.get(parameters, methodContext); 132 } 133 } 134 if (UtilValidate.isNotEmpty(acceptPartyId) && UtilValidate.isNotEmpty(userLogin.getString("partyId")) && acceptPartyId.equals(userLogin.getString("partyId"))) { 135 hasPermission = true; 136 } 137 } 138 139 if (!hasPermission) { 140 this.addMessage(messages, methodContext); 141 } 142 143 return true; 144 } 145 146 public void addMessage(List messages, MethodContext methodContext) { 147 ClassLoader loader = methodContext.getLoader(); 148 149 String message = methodContext.expandString(this.message); 150 String propertyResource = methodContext.expandString(this.propertyResource); 151 152 if (!isProperty && message != null) { 153 messages.add(message); 154 } else if (isProperty && propertyResource != null && message != null) { 156 String propMsg = UtilProperties.getMessage(propertyResource, message, methodContext.getEnvMap(), methodContext.getLocale()); 158 if (propMsg == null || propMsg.length() == 0) { 159 messages.add("Simple Method Permission error occurred, but no message was found, sorry."); 160 } else { 161 messages.add(methodContext.expandString(propMsg)); 162 } 163 } else { 165 messages.add("Simple Method Permission error occurred, but no message was found, sorry."); 166 } 168 } 169 170 public static class PermissionInfo { 171 String permission; 172 String action; 173 174 public PermissionInfo(Element altPermissionElement) { 175 this.permission = altPermissionElement.getAttribute("permission"); 176 this.action = altPermissionElement.getAttribute("action"); 177 } 178 179 public boolean hasPermission(MethodContext methodContext, GenericValue userLogin, Security security) { 180 String permission = methodContext.expandString(this.permission); 181 String action = methodContext.expandString(this.action); 182 183 if (action != null && action.length() > 0) { 184 return security.hasEntityPermission(permission, action, userLogin); 186 } else { 187 return security.hasPermission(permission, userLogin); 189 } 190 } 191 } 192 193 public String rawString() { 194 return "<check-permission/>"; 196 } 197 public String expandedString(MethodContext methodContext) { 198 return this.rawString(); 200 } 201 } 202 | Popular Tags |