📄 entitypermissionchecker.java
字号:
* and returns the ones that match the user. * Follows group parties to see if the user is a member. */ public static List getUserRoles(GenericValue entity, GenericValue userLogin, GenericDelegator delegator) throws GenericEntityException { String entityName = entity.getEntityName(); List roles = new ArrayList(); if(entity == null) return roles; // TODO: Need to use ContentManagementWorker.getAuthorContent first roles.remove("OWNER"); // always test with the owner of the current content if ( entity.get("createdByUserLogin") != null && userLogin != null) { String userLoginId = (String)userLogin.get("userLoginId"); String userLoginIdCB = (String)entity.get("createdByUserLogin"); //if (Debug.infoOn()) Debug.logInfo("userLoginId:" + userLoginId + ": userLoginIdCB:" + userLoginIdCB + ":", null); if (userLoginIdCB.equals(userLoginId)) { roles.add("OWNER"); //if (Debug.infoOn()) Debug.logInfo("in getUserRoles, passedRoles(0):" + passedRoles, null); } } String partyId = (String)userLogin.get("partyId"); List relatedRoles = null; List tmpRelatedRoles = entity.getRelatedCache(entityName + "Role"); relatedRoles = EntityUtil.filterByDate(tmpRelatedRoles); if(relatedRoles != null ) { Iterator rolesIter = relatedRoles.iterator(); while (rolesIter.hasNext() ) { GenericValue contentRole = (GenericValue)rolesIter.next(); String roleTypeId = (String)contentRole.get("roleTypeId"); String targPartyId = (String)contentRole.get("partyId"); if (targPartyId.equals(partyId)) { if (!roles.contains(roleTypeId)) roles.add(roleTypeId); if (roleTypeId.equals("AUTHOR") && !roles.contains("OWNER")) roles.add("OWNER"); } else { // Party may be of "PARTY_GROUP" type, in which case the userLogin may still possess this role GenericValue party = null; String partyTypeId = null; try { party = contentRole.getRelatedOne("Party"); partyTypeId = (String)party.get("partyTypeId"); if ( partyTypeId != null && partyTypeId.equals("PARTY_GROUP") ) { HashMap map = new HashMap(); // At some point from/thru date will need to be added map.put("partyIdFrom", partyId); map.put("partyIdTo", targPartyId); if ( isGroupMember( map, delegator ) ) { if (!roles.contains(roleTypeId)) roles.add(roleTypeId); } } } catch (GenericEntityException e) { Debug.logError(e, "Error in finding related party. " + e.getMessage(), module); } } } } return roles; } /** * Tests to see if the user belongs to a group */ public static boolean isGroupMember( Map partyRelationshipValues, GenericDelegator delegator ) { boolean isMember = false; String partyIdFrom = (String)partyRelationshipValues.get("partyIdFrom") ; String partyIdTo = (String)partyRelationshipValues.get("partyIdTo") ; String roleTypeIdFrom = "PERMISSION_GROUP_MBR"; String roleTypeIdTo = "PERMISSION_GROUP"; Timestamp fromDate = UtilDateTime.nowTimestamp(); Timestamp thruDate = UtilDateTime.getDayStart(UtilDateTime.nowTimestamp(), 1); if (partyRelationshipValues.get("roleTypeIdFrom") != null ) { roleTypeIdFrom = (String)partyRelationshipValues.get("roleTypeIdFrom") ; } if (partyRelationshipValues.get("roleTypeIdTo") != null ) { roleTypeIdTo = (String)partyRelationshipValues.get("roleTypeIdTo") ; } if (partyRelationshipValues.get("fromDate") != null ) { fromDate = (Timestamp)partyRelationshipValues.get("fromDate") ; } if (partyRelationshipValues.get("thruDate") != null ) { thruDate = (Timestamp)partyRelationshipValues.get("thruDate") ; } EntityExpr partyFromExpr = new EntityExpr("partyIdFrom", EntityOperator.EQUALS, partyIdFrom); EntityExpr partyToExpr = new EntityExpr("partyIdTo", EntityOperator.EQUALS, partyIdTo); EntityExpr relationExpr = new EntityExpr("partyRelationshipTypeId", EntityOperator.EQUALS, "CONTENT_PERMISSION"); //EntityExpr roleTypeIdFromExpr = new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "CONTENT_PERMISSION_GROUP_MEMBER"); //EntityExpr roleTypeIdToExpr = new EntityExpr("roleTypeIdTo", EntityOperator.EQUALS, "CONTENT_PERMISSION_GROUP"); EntityExpr fromExpr = new EntityExpr("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, fromDate); EntityCondition thruCond = new EntityConditionList( UtilMisc.toList( new EntityExpr("thruDate", EntityOperator.EQUALS, null), new EntityExpr("thruDate", EntityOperator.GREATER_THAN, thruDate) ), EntityOperator.OR); // This method is simplified to make it work, these conditions need to be added back in. //List joinList = UtilMisc.toList(fromExpr, thruCond, partyFromExpr, partyToExpr, relationExpr); List joinList = UtilMisc.toList( partyFromExpr, partyToExpr); EntityCondition condition = new EntityConditionList(joinList, EntityOperator.AND); List partyRelationships = null; try { partyRelationships = delegator.findByCondition("PartyRelationship", condition, null, null); } catch (GenericEntityException e) { Debug.logError(e, "Problem finding PartyRelationships. ", module); return false; } if (partyRelationships.size() > 0) { isMember = true; } return isMember; } public interface PermissionConditionGetter { public boolean getNext(); public String getRoleValue(); public String getOperationValue(); public String getStatusValue(); public int getPrivilegeValue() throws GenericEntityException; public String getAuxiliaryValue(); public void init(GenericDelegator delegator) throws GenericEntityException; public void restart(); public void setOperationList(String operationIdString); public void setOperationList(List opList); public List getOperationList(); public String dumpAsText(); public void clearList(); } public static class StdPermissionConditionGetter implements PermissionConditionGetter { protected List entityList; protected List operationList; protected ListIterator iter; protected GenericValue currentValue; protected String operationFieldName; protected String roleFieldName; protected String statusFieldName; protected String privilegeFieldName; protected String auxiliaryFieldName; protected String entityName; public StdPermissionConditionGetter () { this.operationFieldName = "contentOperationId"; this.roleFieldName = "roleTypeId"; this.statusFieldName = "statusId"; this.privilegeFieldName = "privilegeEnumId"; this.auxiliaryFieldName = "contentPurposeTypeId"; this.entityName = "ContentPurposeOperation"; } public StdPermissionConditionGetter ( String entityName, String operationFieldName, String roleFieldName, String statusFieldName, String auxiliaryFieldName, String privilegeFieldName) { this.operationFieldName = operationFieldName; this.roleFieldName = roleFieldName ; this.statusFieldName = statusFieldName ; this.privilegeFieldName = privilegeFieldName ; this.auxiliaryFieldName = auxiliaryFieldName ; this.entityName = entityName; } public StdPermissionConditionGetter ( Element getterElement) { this.operationFieldName = getterElement.getAttribute("operation-field-name"); this.roleFieldName = getterElement.getAttribute("role-field-name"); this.statusFieldName = getterElement.getAttribute("status-field-name"); this.privilegeFieldName = getterElement.getAttribute("privilege-field-name"); this.auxiliaryFieldName = getterElement.getAttribute("auxiliary-field-name"); this.entityName = getterElement.getAttribute("entity-name"); } public boolean getNext() { boolean hasNext = false; if (iter != null && iter.hasNext()) { currentValue = (GenericValue)iter.next(); hasNext = true; } return hasNext; } public String getRoleValue() { return this.currentValue.getString(this.roleFieldName); } public String getOperationValue() { return this.currentValue.getString(this.operationFieldName); } public String getStatusValue() { return this.currentValue.getString(this.statusFieldName); } public int getPrivilegeValue() throws GenericEntityException { int privilegeEnumSeq = -1; String privilegeEnumId = null; GenericDelegator delegator = currentValue.getDelegator(); if (UtilValidate.isNotEmpty(privilegeFieldName)) { privilegeEnumId = currentValue.getString(this.privilegeFieldName); } if ( UtilValidate.isNotEmpty(privilegeEnumId)) { GenericValue privEnum = delegator.findByPrimaryKeyCache("Enumeration", UtilMisc.toMap("enumId", privilegeEnumId)); if (privEnum != null) { String sequenceId = privEnum.getString("sequenceId"); try { privilegeEnumSeq = Integer.parseInt(sequenceId); } catch(NumberFormatException e) { // just leave it at -1 } } } return privilegeEnumSeq; } public String getAuxiliaryValue() { return this.currentValue.getString(this.auxiliaryFieldName); } public void setOperationList(String operationIdString) { this.operationList = null; if (UtilValidate.isNotEmpty(operationIdString)) { this.operationList = StringUtil.split(operationIdString, "|"); } } public void setOperationList(List operationList) { this.operationList = operationList; } public List getOperationList() { return this.operationList; } public void clearList() { this.entityList = new ArrayList(); } public void init( GenericDelegator delegator) throws GenericEntityException { EntityCondition opCond = new EntityExpr(operationFieldName, EntityOperator.IN, this.operationList); this.entityList = delegator.findByConditionCache(this.entityName, opCond, null, null); } public void restart() { this.iter = null; if (this.entityList != null) { this.iter = this.entityList.listIterator(); } } public String dumpAsText() { List fieldNames = UtilMisc.toList("roleFieldName", "auxiliaryFieldName", "statusFieldName"); Map widths = UtilMisc.toMap("roleFieldName", new Integer(24), "auxiliaryFieldName", new Integer(24), "statusFieldName", new Integer(24)); StringBuffer buf = new StringBuffer(); Integer wid = null; buf.append("Dump for "); buf.append(this.entityName); buf.append(" ops:"); buf.append(StringUtil.join(this.operationList, ",")); buf.append("\n"); Iterator itFields = fieldNames.iterator(); while (itFields.hasNext()) { String fld = (String)itFields.next(); wid = (Integer)widths.get(fld); buf.append(fld); for (int i=0; i < (wid.intValue() - fld.length()); i++) buf.append("^"); buf.append(" "); } buf.append("\n"); itFields = fieldNames.iterator(); while (itFields.hasNext()) { String fld = (String)itFields.next(); wid = (Integer)widths.get(fld);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -