📄 entitypermissionchecker.java
字号:
//List roleList = null; while (iter.hasNext()) { GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, iter.next(), entities); if (entity == null) continue; checkAncestors = false; passed = hasMatch(entity, permissionConditionGetter, relatedRoleGetter, null, partyId, checkAncestors); if (passed) { break; } } if (passed) { return true; } if (auxiliaryValueGetter != null) { //if (Debug.infoOn()) Debug.logInfo(auxiliaryValueGetter.dumpAsText(), module); // Check with just purposes next. iter = entityIdList.iterator(); while (iter.hasNext()) { GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, iter.next(), entities); if (entity == null) continue; checkAncestors = false; passed = hasMatch(entity, permissionConditionGetter, relatedRoleGetter, auxiliaryValueGetter, partyId, checkAncestors); if (passed){ break; } } } if (passed) return true; // TODO: need to return some information here about why it failed if (partyId == null) return false; // Check with roles. if (relatedRoleGetter != null) { iter = entityIdList.iterator(); while (iter.hasNext()) { GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, iter.next(), entities); if (entity == null) continue; checkAncestors = false; passed = hasMatch(entity, permissionConditionGetter, relatedRoleGetter, auxiliaryValueGetter, partyId, checkAncestors); if (passed){ break; } } } if (passed) return true; if (relatedRoleGetter != null) { iter = entityIdList.iterator(); while (iter.hasNext()) { GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, iter.next(), entities); if (entity == null) continue; String entityId = entity.getString(pkFieldName); checkAncestors = true; passed = hasMatch(entity, permissionConditionGetter, relatedRoleGetter, auxiliaryValueGetter, partyId, checkAncestors); if (passed){ break; } } } return passed; } public static GenericValue getNextEntity(GenericDelegator delegator, String entityName, String pkFieldName, Object obj, Map entities) throws GenericEntityException { GenericValue entity = null; if (obj instanceof String) { String entityId = (String)obj; if (entities != null) entity = (GenericValue)entities.get(entityId); if (entity == null) entity = delegator.findByPrimaryKeyCache(entityName,UtilMisc.toMap(pkFieldName, entityId)); } else if (obj instanceof GenericValue) { entity = (GenericValue)obj; } return entity; } public static boolean checkHasRoleOperations(String partyId, PermissionConditionGetter permissionConditionGetter , GenericDelegator delegator) { List targetOperations = permissionConditionGetter.getOperationList(); return checkHasRoleOperations(partyId, targetOperations, delegator); } public static boolean checkHasRoleOperations(String partyId, List targetOperations, GenericDelegator delegator) { //if (Debug.infoOn()) Debug.logInfo("targetOperations:" + targetOperations, module); //if (Debug.infoOn()) Debug.logInfo("userLoginId:" + userLoginId, module); if (targetOperations == null) return false; if (partyId != null && targetOperations.contains("HAS_USER_ROLE")) return true; boolean hasRoleOperation = false; Iterator targOpIter = targetOperations.iterator(); boolean hasNeed = false; List newHasRoleList = new ArrayList(); while (targOpIter.hasNext()) { String roleOp = (String)targOpIter.next(); int idx1 = roleOp.indexOf("HAS_"); if (idx1 == 0) { String roleOp1 = roleOp.substring(4); // lop off "HAS_" int idx2 = roleOp1.indexOf("_ROLE"); if (idx2 == (roleOp1.length() - 5)) { String roleOp2 = roleOp1.substring(0, roleOp1.indexOf("_ROLE") - 1); // lop off "_ROLE" //if (Debug.infoOn()) Debug.logInfo("roleOp2:" + roleOp2, module); newHasRoleList.add(roleOp2); hasNeed = true; } } } if (hasNeed) { GenericValue uLogin = null; try { if (UtilValidate.isNotEmpty(partyId)) { List partyRoleList = delegator.findByAndCache("PartyRole", UtilMisc.toMap("partyId", partyId)); Iterator partyRoleIter = partyRoleList.iterator(); while (partyRoleIter.hasNext()) { GenericValue partyRole = (GenericValue)partyRoleIter.next(); String roleTypeId = partyRole.getString("roleTypeId"); targOpIter = newHasRoleList.iterator(); while (targOpIter.hasNext()) { String thisRole = (String)targOpIter.next(); if (roleTypeId.indexOf(thisRole) >= 0) { hasRoleOperation = true; break; } } if (hasRoleOperation) break; } } } catch (GenericEntityException e) { Debug.logError(e, module); return hasRoleOperation; } } return hasRoleOperation; } public static boolean hasMatch(String entityName, List targetOperations, List roles, boolean hasPurposeOp, List purposes, boolean hasStatusOp, String targStatusId) { boolean isMatch = false; int targPrivilegeSeq = 0; // if (UtilValidate.isNotEmpty(targPrivilegeEnumId) && !targPrivilegeEnumId.equals("_NA_") && !targPrivilegeEnumId.equals("_00_") ) { // need to do a lookup here to find the seq value of targPrivilegeEnumId. // The lookup could be a static store or it could be done on Enumeration entity. // } String lcEntityName = entityName.toLowerCase(); Iterator targetOpsIter = targetOperations.iterator(); while (targetOpsIter.hasNext() ) { GenericValue targetOp = (GenericValue)targetOpsIter.next(); String testRoleTypeId = (String)targetOp.get("roleTypeId"); String testContentPurposeTypeId = null; if (hasPurposeOp) testContentPurposeTypeId = (String)targetOp.get(lcEntityName + "PurposeTypeId"); String testStatusId = null; if (hasStatusOp) testStatusId = (String)targetOp.get("statusId"); //String testPrivilegeEnumId = null; //if (hasPrivilegeOp) //testPrivilegeEnumId = (String)targetOp.get("privilegeEnumId"); //int testPrivilegeSeq = 0; boolean purposesCond = ( !hasPurposeOp || (purposes != null && purposes.contains(testContentPurposeTypeId) ) || testContentPurposeTypeId.equals("_NA_") ); boolean statusCond = ( !hasStatusOp || testStatusId.equals("_NA_") || (targStatusId != null && targStatusId.equals(testStatusId) ) ); //boolean privilegeCond = ( !hasPrivilegeOp || testPrivilegeEnumId.equals("_NA_") || testPrivilegeSeq <= targPrivilegeSeq || testPrivilegeEnumId.equals(targPrivilegeEnumId) ); boolean roleCond = ( testRoleTypeId.equals("_NA_") || (roles != null && roles.contains(testRoleTypeId) ) ); if (purposesCond && statusCond && roleCond) { isMatch = true; break; } } return isMatch; } public static boolean hasMatch(GenericValue entity, PermissionConditionGetter permissionConditionGetter, RelatedRoleGetter relatedRoleGetter, AuxiliaryValueGetter auxiliaryValueGetter, String partyId, boolean checkAncestors) throws GenericEntityException { String entityName = entity.getEntityName(); ModelEntity modelEntity = entity.getModelEntity(); GenericDelegator delegator = entity.getDelegator(); String pkFieldName = modelEntity.getFirstPkFieldName(); String entityId = entity.getString(pkFieldName); if (Debug.verboseOn()) Debug.logVerbose("\n\nIN hasMatch: entityId:" + entityId + " partyId:" + partyId + " checkAncestors:" + checkAncestors, module); boolean isMatch = false; permissionConditionGetter.restart(); List auxiliaryValueList = null; if (auxiliaryValueGetter != null) { auxiliaryValueGetter.init(delegator, entityId); auxiliaryValueList = auxiliaryValueGetter.getList(); if (Debug.verboseOn()) Debug.logVerbose(auxiliaryValueGetter.dumpAsText(), module); } else { if (Debug.verboseOn()) Debug.logVerbose("NO AUX GETTER", module); } List roleValueList = null; if (relatedRoleGetter != null) { if (checkAncestors) { relatedRoleGetter.initWithAncestors(delegator, entity, partyId); } else { relatedRoleGetter.init(delegator, entityId, partyId, entity); } roleValueList = relatedRoleGetter.getList(); if (Debug.verboseOn()) Debug.logVerbose(relatedRoleGetter.dumpAsText(), module); } else { if (Debug.verboseOn()) Debug.logVerbose("NO ROLE GETTER", module); } String targStatusId = null; if (modelEntity.getField("statusId") != null) { targStatusId = entity.getString("statusId"); } if (Debug.verboseOn()) Debug.logVerbose("STATUS:" + targStatusId, module); while (permissionConditionGetter.getNext() ) { String roleConditionId = permissionConditionGetter.getRoleValue(); String auxiliaryConditionId = permissionConditionGetter.getAuxiliaryValue(); String statusConditionId = permissionConditionGetter.getStatusValue(); boolean auxiliaryCond = ( auxiliaryConditionId == null || auxiliaryConditionId.equals("_NA_") || (auxiliaryValueList != null && auxiliaryValueList.contains(auxiliaryConditionId) ) ); boolean statusCond = ( statusConditionId == null || statusConditionId.equals("_NA_") || (targStatusId != null && targStatusId.equals(statusConditionId) ) ); boolean roleCond = ( roleConditionId == null || roleConditionId.equals("_NA_") || (roleValueList != null && roleValueList.contains(roleConditionId) ) ); if (auxiliaryCond && statusCond && roleCond) { if (Debug.verboseOn()) Debug.logVerbose("MATCHED: role:" + roleConditionId + " status:" + statusConditionId + " aux:" + auxiliaryConditionId, module); isMatch = true; break; } } return isMatch; } /** * getRelatedPurposes */ public static List getRelatedPurposes(GenericValue entity, List passedPurposes) { if(entity == null) return passedPurposes; List purposeIds = null; if (passedPurposes == null) { purposeIds = new ArrayList( ); } else { purposeIds = new ArrayList( passedPurposes ); } String entityName = entity.getEntityName(); String lcEntityName = entityName.toLowerCase(); List purposes = null; try { purposes = entity.getRelatedCache(entityName + "Purpose"); } catch (GenericEntityException e) { Debug.logError(e, "No associated purposes found. ", module); return purposeIds; } Iterator purposesIter = purposes.iterator(); while (purposesIter.hasNext() ) { GenericValue val = (GenericValue)purposesIter.next(); purposeIds.add(val.get(lcEntityName + "PurposeTypeId")); } return purposeIds; } /** * getUserRoles * Queries for the ContentRoles associated with a Content entity
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -