📄 contentpermissionservices.java
字号:
/**
* getUserRoles
* Queries for the ContentRoles associated with a Content entity
* and returns the ones that match the user.
* Follows group parties to see if the user is a member.
*/
public static List getUserRoles(GenericValue content, GenericValue userLogin,
List passedRoles, GenericDelegator delegator) {
if(content == null) return passedRoles;
ArrayList roles = null;
if (passedRoles == null) {
roles = new ArrayList( );
} else {
roles = new ArrayList( passedRoles );
}
String partyId = (String)userLogin.get("partyId");
List relatedRoles = null;
try {
relatedRoles = content.getRelatedCache("ContentRole");
} catch (GenericEntityException e) {
Debug.logError(e, "No related roles found. ", module);
}
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)) {
roles.add(roleTypeId);
} else {
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 ) ) {
roles.add(roleTypeId);
}
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error in finding related party. " + e.getMessage(), module);
}
}
}
}
return roles;
}
/**
* publicMatches
* Takes all the criteria and performs a check to see if there is a match.
*/
public static boolean publicMatches(List purposeOperations, List targetOperations,
List purposes, List roles, String targStatusId) {
boolean isMatch = false;
Iterator purposeOpsIter = purposeOperations.iterator();
while (purposeOpsIter.hasNext() ) {
GenericValue purposeOp = (GenericValue)purposeOpsIter.next();
String roleTypeId = (String)purposeOp.get("roleTypeId");
String contentPurposeTypeId = (String)purposeOp.get("contentPurposeTypeId");
String contentOperationId = (String)purposeOp.get("contentOperationId");
String testStatusId = (String)purposeOp.get("statusId");
if (Debug.verboseOn()) Debug.logVerbose("purposeOp:" + purposeOp, null);
if (Debug.verboseOn()) Debug.logVerbose("purposes:" + purposes, null);
if (Debug.verboseOn()) Debug.logVerbose("roles:" + roles, null);
if (Debug.verboseOn()) Debug.logVerbose("targetOperations:" + targetOperations, null);
if ( targetOperations != null && targetOperations.contains(contentOperationId)
&& ( (purposes != null && purposes.contains(contentPurposeTypeId) )
|| contentPurposeTypeId.equals("_NA_") )
&& (testStatusId == null || testStatusId.equals("_NA_")
|| testStatusId.equals(targStatusId) )
) {
if ( roleTypeId == null
|| roleTypeId.equals("_NA_")
|| (roles != null && roles.contains(roleTypeId) ) ){
isMatch = true;
break;
}
}
}
return isMatch;
}
/**
* isGroupMember
* 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_MEMBER";
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;
}
/**
* getRelatedPurposes
*/
public static List getRelatedPurposes(GenericValue content, List passedPurposes) {
if(content == null) return passedPurposes;
List purposeIds = null;
if (passedPurposes == null) {
purposeIds = new ArrayList( );
} else {
purposeIds = new ArrayList( passedPurposes );
}
if (content == null || content.get("contentId") == null ) {
return purposeIds;
}
List purposes = null;
try {
purposes = content.getRelatedCache("ContentPurpose");
} catch (GenericEntityException e) {
Debug.logError(e, "No associated purposes found. ", module);
}
Iterator purposesIter = purposes.iterator();
while (purposesIter.hasNext() ) {
GenericValue val = (GenericValue)purposesIter.next();
purposeIds.add(val.get("contentPurposeTypeId"));
}
return purposeIds;
}
public static Map checkAssocPermission(DispatchContext dctx, Map context) {
if (Debug.verboseOn()) Debug.logVerbose("checkAssoc", null);
Map results = new HashMap();
Security security = dctx.getSecurity();
GenericDelegator delegator = dctx.getDelegator();
if (Debug.verboseOn()) Debug.logVerbose("checkAssoc, delegator:" + delegator, null);
String contentIdFrom = (String) context.get("contentIdFrom");
String contentIdTo = (String) context.get("contentIdTo");
String statusId = (String) context.get("statusId");
GenericValue content = (GenericValue) context.get("currentContent");
GenericValue userLogin = (GenericValue) context.get("userLogin");
List purposeList = (List) context.get("contentPurposeList");
List targetOperations = (List) context.get("targetOperationList");
List roleList = (List) context.get("roleTypeList");
if (roleList == null) roleList = new ArrayList();
String entityAction = (String) context.get("entityOperation");
if (entityAction == null) entityAction = "_ADMIN";
List roleIds = null;
if (Debug.verboseOn()) Debug.logVerbose("in checkAssocPerm, contentIdTo:" + contentIdTo, null);
if (Debug.verboseOn()) Debug.logVerbose("in checkAssocPerm, contentIdFrom:" + contentIdFrom, null);
GenericValue contentTo = null;
GenericValue contentFrom = null;
try {
contentTo = delegator.findByPrimaryKey("Content",
UtilMisc.toMap("contentId", contentIdTo) );
contentFrom = delegator.findByPrimaryKey("Content",
UtilMisc.toMap("contentId", contentIdFrom) );
if (Debug.verboseOn()) Debug.logVerbose("in checkAssocPerm, contentTo:" + contentTo, null);
if (Debug.verboseOn()) Debug.logVerbose("in checkAssocPerm, contentFrom:" + contentFrom, null);
} catch (GenericEntityException e) {
return ServiceUtil.returnError("Error in retrieving content To or From. " + e.getMessage());
}
if (contentTo == null || contentFrom == null) {
return ServiceUtil.returnError("contentTo[" + contentTo + "]/From[" + contentFrom + "] is null. ");
}
String creatorLoginTo = (String)contentTo.get("createdByUserLogin");
String creatorLoginFrom = (String)contentFrom.get("createdByUserLogin");
if(creatorLoginTo != null && creatorLoginFrom != null
&& creatorLoginTo.equals(creatorLoginFrom) ) {
roleList.add("_OWNER_");
}
Map resultsMap = checkPermission( null, statusId,
userLogin, purposeList,
targetOperations, roleList,
delegator, security, entityAction);
boolean isMatch = false;
if(resultsMap.get("permissionStatus").equals("granted") ) isMatch = true;
boolean isMatchTo = false;
boolean isMatchFrom = false;
if(!isMatch){
roleList = (List)resultsMap.get("roleTypeList");
resultsMap = checkPermission( contentTo, statusId,
userLogin, purposeList,
targetOperations, roleList,
delegator, security, entityAction);
if(resultsMap.get("permissionStatus").equals("granted") ) isMatchTo = true;
resultsMap = checkPermission( contentFrom, statusId,
userLogin, purposeList,
targetOperations, roleList,
delegator, security, entityAction);
if(resultsMap.get("permissionStatus").equals("granted") ) isMatchFrom = true;
results.put("roleTypeList", resultsMap.get("roleTypeList"));
if(isMatchTo && isMatchFrom) isMatch = true;
}
String permissionStatus = null;
if( isMatch ) permissionStatus = "granted";
results.put("permissionStatus", permissionStatus);
return results;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -