📄 entitypermissionchecker.java
字号:
/* * $Id: EntityPermissionChecker.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.entityext.permission;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.StringUtil;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.string.FlexibleStringExpander;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.security.Security;import org.ofbiz.service.ServiceUtil;import org.w3c.dom.Element;/** * EntityPermissionChecker Class * * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a> * @version $Rev: 5462 $ * @since 3.1 * * Services for granting operation permissions on Content entities in a data-driven manner. */public class EntityPermissionChecker { public static final String module = EntityPermissionChecker.class.getName(); protected FlexibleStringExpander entityIdExdr; protected FlexibleStringExpander entityNameExdr; protected boolean displayFailCond; protected List targetOperationList; protected PermissionConditionGetter permissionConditionGetter; protected RelatedRoleGetter relatedRoleGetter; protected AuxiliaryValueGetter auxiliaryValueGetter; public EntityPermissionChecker(Element element) { this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name")); this.entityIdExdr = new FlexibleStringExpander(element.getAttribute("entity-id")); this.displayFailCond = "true".equals(element.getAttribute("display-fail-cond")); Element permissionConditionElement = UtilXml.firstChildElement(element, "permission-condition-getter"); if (permissionConditionElement == null) { permissionConditionGetter = new StdPermissionConditionGetter(); } else { permissionConditionGetter = new StdPermissionConditionGetter(permissionConditionElement); } Element auxiliaryValueElement = UtilXml.firstChildElement(element, "auxiliary-value-getter"); if (auxiliaryValueElement == null) { auxiliaryValueGetter = new StdAuxiliaryValueGetter(); } else { auxiliaryValueGetter = new StdAuxiliaryValueGetter(auxiliaryValueElement); } Element relatedRoleElement = UtilXml.firstChildElement(element, "related-role-getter"); if (relatedRoleElement == null) { relatedRoleGetter = new StdRelatedRoleGetter(); } else { relatedRoleGetter = new StdRelatedRoleGetter(relatedRoleElement); } String targetOperationString = new String(element.getAttribute("target-operation")); if (UtilValidate.isNotEmpty(targetOperationString)) { List operationsFromString = StringUtil.split(targetOperationString, "|"); if (targetOperationList == null) { targetOperationList = new ArrayList(); } targetOperationList.addAll(operationsFromString); } permissionConditionGetter.setOperationList(targetOperationList); return; } public boolean runPermissionCheck(Map context) { boolean passed = false; String idString = entityIdExdr.expandString(context); List entityIdList = null; if (UtilValidate.isNotEmpty(idString)) { entityIdList = StringUtil.split(idString, "|"); } else { entityIdList = new ArrayList(); } String entityName = entityNameExdr.expandString(context); HttpServletRequest request = (HttpServletRequest)context.get("request"); GenericValue userLogin = null; String userLoginId = null; String partyId = null; GenericDelegator delegator = null; if (request != null) { HttpSession session = request.getSession(); userLogin = (GenericValue)session.getAttribute("userLogin"); if (userLogin != null) { userLoginId = userLogin.getString("userLoginId"); partyId = userLogin.getString("partyId"); } delegator = (GenericDelegator)request.getAttribute("delegator"); } if (auxiliaryValueGetter != null) auxiliaryValueGetter.clearList(); if (relatedRoleGetter != null) relatedRoleGetter.clearList(); try { permissionConditionGetter.init(delegator); passed = checkPermissionMethod(delegator, partyId, entityName, entityIdList, auxiliaryValueGetter, relatedRoleGetter, permissionConditionGetter); if (!passed && displayFailCond) { String errMsg = "Permission is denied. \nThese are the conditions of which one must be met:\n" + permissionConditionGetter.dumpAsText(); List errorMessageList = (List)context.get("errorMessageList"); errorMessageList.add(errMsg); } } catch(GenericEntityException e) { throw new RuntimeException(e.getMessage()); } return passed; } public static Map checkPermission(GenericValue content, String statusId, GenericValue userLogin, List passedPurposes, List targetOperations, List passedRoles, GenericDelegator delegator , Security security, String entityAction) { String privilegeEnumId = null; return checkPermission( content, statusId, userLogin, passedPurposes, targetOperations, passedRoles, delegator, security, entityAction, privilegeEnumId, null); } public static Map checkPermission(GenericValue content, String statusId, GenericValue userLogin, List passedPurposes, List targetOperations, List passedRoles, GenericDelegator delegator , Security security, String entityAction, String privilegeEnumId, String quickCheckContentId) { List statusList = null; if (statusId != null) { statusList = StringUtil.split(statusId, "|"); } return checkPermission( content, statusList, userLogin, passedPurposes, targetOperations, passedRoles, delegator, security, entityAction, privilegeEnumId, quickCheckContentId); } public static Map checkPermission(GenericValue content, List statusList, GenericValue userLogin, List passedPurposes, List targetOperations, List passedRoles, GenericDelegator delegator , Security security, String entityAction, String privilegeEnumId) { return checkPermission( content, statusList, userLogin, passedPurposes, targetOperations, passedRoles, delegator, security, entityAction, privilegeEnumId, null); } public static Map checkPermission(GenericValue content, List statusList, GenericValue userLogin, List passedPurposes, List targetOperations, List passedRoles, GenericDelegator delegator , Security security, String entityAction, String privilegeEnumId, String quickCheckContentId) { String contentId = null; if (content != null) contentId = content.getString("contentId"); List entityIds = new ArrayList(); if (content != null) entityIds.add(content); if (UtilValidate.isNotEmpty(quickCheckContentId)) { List quickList = StringUtil.split(quickCheckContentId, "|"); if (UtilValidate.isNotEmpty(quickList)) entityIds.addAll(quickList); } Map results = new HashMap(); boolean passed = false; if (userLogin != null && entityAction != null) { passed = security.hasEntityPermission("CONTENTMGR", entityAction, userLogin); } if (passed) { results.put("permissionStatus", "granted"); return results; } try { boolean check = checkPermissionMethod( delegator, userLogin, targetOperations, "Content", entityIds, passedPurposes, null, privilegeEnumId); if (check) results.put("permissionStatus", "granted"); else results.put("permissionStatus", "rejected"); } catch (GenericEntityException e) { ServiceUtil.returnError(e.getMessage()); } return results; } public static boolean checkPermissionMethod(GenericDelegator delegator, GenericValue userLogin, List targetOperationList, String entityName, List entityIdList, List purposeList, List roleList, String privilegeEnumId) throws GenericEntityException { boolean passed = false; String lcEntityName = entityName.toLowerCase(); String userLoginId = null; String partyId = null; if (userLogin != null) { userLoginId = userLogin.getString("userLoginId"); partyId = userLogin.getString("partyId"); } boolean hasRoleOperation = false; if (!(targetOperationList == null) && userLoginId != null) { hasRoleOperation = checkHasRoleOperations(partyId, targetOperationList, delegator); } if( hasRoleOperation ) { return true; } ModelEntity modelEntity = delegator.getModelEntity(entityName); boolean hasStatusField = false; if (modelEntity.getField("statusId") != null) hasStatusField = true; boolean hasPrivilegeField = false; if (modelEntity.getField("privilegeEnumId") != null) hasPrivilegeField = true; List operationEntities = null; ModelEntity modelOperationEntity = delegator.getModelEntity(entityName + "PurposeOperation"); if (modelOperationEntity == null) { modelOperationEntity = delegator.getModelEntity(entityName + "Operation"); } if (modelOperationEntity == null) { Debug.logError("No operation entity found for " + entityName, module); throw new RuntimeException("No operation entity found for " + entityName); } boolean hasPurposeOp = false; if (modelOperationEntity.getField(lcEntityName + "PurposeTypeId") != null) hasPurposeOp = true; boolean hasStatusOp = false; if (modelOperationEntity.getField("statusId") != null) hasStatusOp = true; boolean hasPrivilegeOp = false; if (modelOperationEntity.getField("privilegeEnumId") != null) hasPrivilegeOp = true; // Get all the condition operations that could apply, rather than having to go thru // entire table each time. //List condList = new ArrayList();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -