0001: /*******************************************************************************
0002: * Licensed to the Apache Software Foundation (ASF) under one
0003: * or more contributor license agreements. See the NOTICE file
0004: * distributed with this work for additional information
0005: * regarding copyright ownership. The ASF licenses this file
0006: * to you under the Apache License, Version 2.0 (the
0007: * "License"); you may not use this file except in compliance
0008: * with the License. You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing,
0013: * software distributed under the License is distributed on an
0014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015: * KIND, either express or implied. See the License for the
0016: * specific language governing permissions and limitations
0017: * under the License.
0018: *******************************************************************************/package org.ofbiz.entityext.permission;
0019:
0020: import java.sql.Timestamp;
0021: import java.util.ArrayList;
0022: import java.util.Arrays;
0023: import java.util.HashMap;
0024: import java.util.HashSet;
0025: import java.util.Iterator;
0026: import java.util.List;
0027: import java.util.ListIterator;
0028: import java.util.Map;
0029:
0030: import javax.servlet.http.HttpServletRequest;
0031: import javax.servlet.http.HttpSession;
0032:
0033: import org.ofbiz.base.util.Debug;
0034: import org.ofbiz.base.util.StringUtil;
0035: import org.ofbiz.base.util.UtilDateTime;
0036: import org.ofbiz.base.util.UtilMisc;
0037: import org.ofbiz.base.util.UtilValidate;
0038: import org.ofbiz.base.util.UtilXml;
0039: import org.ofbiz.base.util.string.FlexibleStringExpander;
0040: import org.ofbiz.entity.GenericDelegator;
0041: import org.ofbiz.entity.GenericEntityException;
0042: import org.ofbiz.entity.GenericValue;
0043: import org.ofbiz.entity.condition.EntityCondition;
0044: import org.ofbiz.entity.condition.EntityConditionList;
0045: import org.ofbiz.entity.condition.EntityExpr;
0046: import org.ofbiz.entity.condition.EntityOperator;
0047: import org.ofbiz.entity.model.ModelEntity;
0048: import org.ofbiz.entity.util.EntityUtil;
0049: import org.ofbiz.security.Security;
0050: import org.ofbiz.service.ServiceUtil;
0051: import org.w3c.dom.Element;
0052:
0053: /**
0054: * EntityPermissionChecker Class
0055: * Services for granting operation permissions on Content entities in a data-driven manner.
0056: */
0057: public class EntityPermissionChecker {
0058:
0059: public static final String module = EntityPermissionChecker.class
0060: .getName();
0061:
0062: protected FlexibleStringExpander entityIdExdr;
0063: protected FlexibleStringExpander entityNameExdr;
0064: protected boolean displayFailCond;
0065: protected List targetOperationList;
0066: protected PermissionConditionGetter permissionConditionGetter;
0067: protected RelatedRoleGetter relatedRoleGetter;
0068: protected AuxiliaryValueGetter auxiliaryValueGetter;
0069:
0070: public EntityPermissionChecker(Element element) {
0071: this .entityNameExdr = new FlexibleStringExpander(element
0072: .getAttribute("entity-name"));
0073: this .entityIdExdr = new FlexibleStringExpander(element
0074: .getAttribute("entity-id"));
0075: this .displayFailCond = "true".equals(element
0076: .getAttribute("display-fail-cond"));
0077: Element permissionConditionElement = UtilXml.firstChildElement(
0078: element, "permission-condition-getter");
0079: if (permissionConditionElement == null) {
0080: permissionConditionGetter = new StdPermissionConditionGetter();
0081: } else {
0082: permissionConditionGetter = new StdPermissionConditionGetter(
0083: permissionConditionElement);
0084: }
0085: Element auxiliaryValueElement = UtilXml.firstChildElement(
0086: element, "auxiliary-value-getter");
0087: if (auxiliaryValueElement == null) {
0088: auxiliaryValueGetter = new StdAuxiliaryValueGetter();
0089: } else {
0090: auxiliaryValueGetter = new StdAuxiliaryValueGetter(
0091: auxiliaryValueElement);
0092: }
0093: Element relatedRoleElement = UtilXml.firstChildElement(element,
0094: "related-role-getter");
0095: if (relatedRoleElement == null) {
0096: relatedRoleGetter = new StdRelatedRoleGetter();
0097: } else {
0098: relatedRoleGetter = new StdRelatedRoleGetter(
0099: relatedRoleElement);
0100: }
0101: String targetOperationString = element
0102: .getAttribute("target-operation");
0103: if (UtilValidate.isNotEmpty(targetOperationString)) {
0104: List operationsFromString = StringUtil.split(
0105: targetOperationString, "|");
0106: if (targetOperationList == null) {
0107: targetOperationList = new ArrayList();
0108: }
0109: targetOperationList.addAll(operationsFromString);
0110: }
0111: permissionConditionGetter.setOperationList(targetOperationList);
0112:
0113: }
0114:
0115: public boolean runPermissionCheck(Map context) {
0116:
0117: boolean passed = false;
0118: String idString = entityIdExdr.expandString(context);
0119: List entityIdList = null;
0120: if (UtilValidate.isNotEmpty(idString)) {
0121: entityIdList = StringUtil.split(idString, "|");
0122: } else {
0123: entityIdList = new ArrayList();
0124: }
0125: String entityName = entityNameExdr.expandString(context);
0126: HttpServletRequest request = (HttpServletRequest) context
0127: .get("request");
0128: GenericValue userLogin = null;
0129: String partyId = null;
0130: GenericDelegator delegator = null;
0131: if (request != null) {
0132: HttpSession session = request.getSession();
0133: userLogin = (GenericValue) session
0134: .getAttribute("userLogin");
0135: if (userLogin != null) {
0136: partyId = userLogin.getString("partyId");
0137: }
0138: delegator = (GenericDelegator) request
0139: .getAttribute("delegator");
0140: }
0141:
0142: if (auxiliaryValueGetter != null)
0143: auxiliaryValueGetter.clearList();
0144: if (relatedRoleGetter != null)
0145: relatedRoleGetter.clearList();
0146: try {
0147: permissionConditionGetter.init(delegator);
0148: passed = checkPermissionMethod(delegator, partyId,
0149: entityName, entityIdList, auxiliaryValueGetter,
0150: relatedRoleGetter, permissionConditionGetter);
0151: if (!passed && displayFailCond) {
0152: String errMsg = "Permission is denied. \nThese are the conditions of which one must be met:\n"
0153: + permissionConditionGetter.dumpAsText();
0154: List errorMessageList = (List) context
0155: .get("errorMessageList");
0156: errorMessageList.add(errMsg);
0157: }
0158: } catch (GenericEntityException e) {
0159: throw new RuntimeException(e.getMessage());
0160: }
0161: return passed;
0162: }
0163:
0164: public static Map checkPermission(GenericValue content,
0165: String statusId, GenericValue userLogin,
0166: List passedPurposes, List targetOperations,
0167: List passedRoles, GenericDelegator delegator,
0168: Security security, String entityAction) {
0169: String privilegeEnumId = null;
0170: return checkPermission(content, statusId, userLogin,
0171: passedPurposes, targetOperations, passedRoles,
0172: delegator, security, entityAction, privilegeEnumId,
0173: null);
0174: }
0175:
0176: public static Map checkPermission(GenericValue content,
0177: String statusId, GenericValue userLogin,
0178: List passedPurposes, List targetOperations,
0179: List passedRoles, GenericDelegator delegator,
0180: Security security, String entityAction,
0181: String privilegeEnumId, String quickCheckContentId) {
0182: List statusList = null;
0183: if (statusId != null) {
0184: statusList = StringUtil.split(statusId, "|");
0185: }
0186: return checkPermission(content, statusList, userLogin,
0187: passedPurposes, targetOperations, passedRoles,
0188: delegator, security, entityAction, privilegeEnumId,
0189: quickCheckContentId);
0190: }
0191:
0192: public static Map checkPermission(GenericValue content,
0193: List statusList, GenericValue userLogin,
0194: List passedPurposes, List targetOperations,
0195: List passedRoles, GenericDelegator delegator,
0196: Security security, String entityAction,
0197: String privilegeEnumId) {
0198: return checkPermission(content, statusList, userLogin,
0199: passedPurposes, targetOperations, passedRoles,
0200: delegator, security, entityAction, privilegeEnumId,
0201: null);
0202: }
0203:
0204: public static Map checkPermission(GenericValue content,
0205: List statusList, GenericValue userLogin,
0206: List passedPurposes, List targetOperations,
0207: List passedRoles, GenericDelegator delegator,
0208: Security security, String entityAction,
0209: String privilegeEnumId, String quickCheckContentId) {
0210:
0211: List entityIds = new ArrayList();
0212: if (content != null)
0213: entityIds.add(content);
0214: if (UtilValidate.isNotEmpty(quickCheckContentId)) {
0215: List quickList = StringUtil.split(quickCheckContentId, "|");
0216: if (UtilValidate.isNotEmpty(quickList))
0217: entityIds.addAll(quickList);
0218: }
0219: Map results = new HashMap();
0220: boolean passed = false;
0221: if (userLogin != null && entityAction != null) {
0222: passed = security.hasEntityPermission("CONTENTMGR",
0223: entityAction, userLogin);
0224: }
0225: if (passed) {
0226: results.put("permissionStatus", "granted");
0227: return results;
0228: }
0229: try {
0230: boolean check = checkPermissionMethod(delegator, userLogin,
0231: targetOperations, "Content", entityIds,
0232: passedPurposes, null, privilegeEnumId);
0233: if (check) {
0234: results.put("permissionStatus", "granted");
0235: } else {
0236: results.put("permissionStatus", "rejected");
0237: }
0238: } catch (GenericEntityException e) {
0239: ServiceUtil.returnError(e.getMessage());
0240: }
0241: return results;
0242: }
0243:
0244: public static boolean checkPermissionMethod(
0245: GenericDelegator delegator, GenericValue userLogin,
0246: List targetOperationList, String entityName,
0247: List entityIdList, List purposeList, List roleList,
0248: String privilegeEnumId) throws GenericEntityException {
0249: boolean passed = false;
0250:
0251: String lcEntityName = entityName.toLowerCase();
0252: String userLoginId = null;
0253: String partyId = null;
0254: if (userLogin != null) {
0255: userLoginId = userLogin.getString("userLoginId");
0256: partyId = userLogin.getString("partyId");
0257: }
0258: boolean hasRoleOperation = false;
0259: if (!(targetOperationList == null) && userLoginId != null) {
0260: hasRoleOperation = checkHasRoleOperations(partyId,
0261: targetOperationList, delegator);
0262: }
0263: if (hasRoleOperation) {
0264: return true;
0265: }
0266: ModelEntity modelEntity = delegator.getModelEntity(entityName);
0267: boolean hasStatusField = false;
0268: if (modelEntity.getField("statusId") != null)
0269: hasStatusField = true;
0270:
0271: boolean hasPrivilegeField = false;
0272: if (modelEntity.getField("privilegeEnumId") != null)
0273: hasPrivilegeField = true;
0274:
0275: List operationEntities = null;
0276: ModelEntity modelOperationEntity = delegator
0277: .getModelEntity(entityName + "PurposeOperation");
0278: if (modelOperationEntity == null) {
0279: modelOperationEntity = delegator.getModelEntity(entityName
0280: + "Operation");
0281: }
0282:
0283: if (modelOperationEntity == null) {
0284: Debug.logError("No operation entity found for "
0285: + entityName, module);
0286: throw new RuntimeException("No operation entity found for "
0287: + entityName);
0288: }
0289:
0290: boolean hasPurposeOp = false;
0291: if (modelOperationEntity.getField(lcEntityName
0292: + "PurposeTypeId") != null)
0293: hasPurposeOp = true;
0294: boolean hasStatusOp = false;
0295: if (modelOperationEntity.getField("statusId") != null)
0296: hasStatusOp = true;
0297: boolean hasPrivilegeOp = false;
0298: if (modelOperationEntity.getField("privilegeEnumId") != null)
0299: hasPrivilegeOp = true;
0300:
0301: // Get all the condition operations that could apply, rather than having to go thru
0302: // entire table each time.
0303: //List condList = new ArrayList();
0304: //Iterator iterType = targetOperationList.iterator();
0305: //while (iterType.hasNext()) {
0306: // String op = (String)iterType.next();
0307: // condList.add(new EntityExpr(lcEntityName + "OperationId", EntityOperator.EQUALS, op));
0308: //}
0309: //EntityCondition opCond = new EntityConditionList(condList, EntityOperator.OR);
0310:
0311: EntityCondition opCond = new EntityExpr(lcEntityName
0312: + "OperationId", EntityOperator.IN, targetOperationList);
0313:
0314: List targetOperationEntityList = delegator
0315: .findByConditionCache(modelOperationEntity
0316: .getEntityName(), opCond, null, null);
0317: Map entities = new HashMap();
0318: String pkFieldName = modelEntity.getFirstPkFieldName();
0319:
0320: //TODO: privilegeEnumId test
0321: /*
0322: if (hasPrivilegeOp && hasPrivilegeField) {
0323: int privilegeEnumSeq = -1;
0324:
0325: if ( UtilValidate.isNotEmpty(privilegeEnumId)) {
0326: GenericValue privEnum = delegator.findByPrimaryKeyCache("Enumeration", UtilMisc.toMap("enumId", privilegeEnumId));
0327: if (privEnum != null) {
0328: String sequenceId = privEnum.getString("sequenceId");
0329: try {
0330: privilegeEnumSeq = Integer.parseInt(sequenceId);
0331: } catch(NumberFormatException e) {
0332: // just leave it at -1
0333: }
0334: }
0335: }
0336: boolean thisPassed = true;
0337: Iterator iter = entityIdList.iterator();
0338: while (iter.hasNext()) {
0339: GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, iter.next(), entities);
0340: if (entity == null) continue;
0341:
0342: String entityId = entity.getString(pkFieldName);
0343: String targetPrivilegeEnumId = entity.getString("privilegeEnumId");
0344: if (UtilValidate.isNotEmpty(targetPrivilegeEnumId)) {
0345: int targetPrivilegeEnumSeq = -1;
0346: GenericValue privEnum = delegator.findByPrimaryKeyCache("Enumeration", UtilMisc.toMap("enumId", privilegeEnumId));
0347: if (privEnum != null) {
0348: String sequenceId = privEnum.getString("sequenceId");
0349: try {
0350: targetPrivilegeEnumSeq = Integer.parseInt(sequenceId);
0351: } catch(NumberFormatException e) {
0352: // just leave it at -1
0353: }
0354: if (targetPrivilegeEnumSeq > privilegeEnumSeq) {
0355: return false;
0356: }
0357: }
0358: }
0359: entities.put(entityId, entity);
0360: }
0361: }
0362: */
0363:
0364: // check permission for each id in passed list until success.
0365: // Note that "quickCheck" id come first in the list
0366: // Check with no roles or purposes on the chance that the permission fields contain _NA_ s.
0367: List alreadyCheckedIds = new ArrayList();
0368: Map purposes = new HashMap();
0369: Map roles = new HashMap();
0370: Iterator iter = entityIdList.iterator();
0371: //List purposeList = null;
0372: //List roleList = null;
0373: while (iter.hasNext()) {
0374: GenericValue entity = getNextEntity(delegator, entityName,
0375: pkFieldName, iter.next(), entities);
0376: if (entity == null)
0377: continue;
0378:
0379: String statusId = null;
0380: if (hasStatusOp && hasStatusField) {
0381: statusId = entity.getString("statusId");
0382: }
0383:
0384: int privilegeEnumSeq = -1;
0385: if (hasPrivilegeOp && hasPrivilegeField) {
0386: privilegeEnumId = entity.getString("privilegeEnumId");
0387: privilegeEnumSeq = getPrivilegeEnumSeq(delegator,
0388: privilegeEnumId);
0389: }
0390:
0391: passed = hasMatch(entityName, targetOperationEntityList,
0392: roleList, hasPurposeOp, purposeList, hasStatusOp,
0393: statusId);
0394: if (passed) {
0395: break;
0396: }
0397: }
0398:
0399: if (passed) {
0400: return true;
0401: }
0402:
0403: if (hasPurposeOp) {
0404: // Check with just purposes next.
0405: iter = entityIdList.iterator();
0406: while (iter.hasNext()) {
0407: GenericValue entity = getNextEntity(delegator,
0408: entityName, pkFieldName, iter.next(), entities);
0409: if (entity == null)
0410: continue;
0411:
0412: String entityId = entity.getString(pkFieldName);
0413: purposeList = getRelatedPurposes(entity, null);
0414: String statusId = null;
0415: if (hasStatusOp && hasStatusField) {
0416: statusId = entity.getString("statusId");
0417: }
0418:
0419: if (purposeList.size() > 0) {
0420: passed = hasMatch(entityName,
0421: targetOperationEntityList, roleList,
0422: hasPurposeOp, purposeList, hasStatusOp,
0423: statusId);
0424: }
0425: if (passed) {
0426: break;
0427: }
0428: purposes.put(entityId, purposeList);
0429: }
0430: }
0431:
0432: if (passed)
0433: return true;
0434:
0435: if (userLogin == null)
0436: return false;
0437:
0438: // Check with roles.
0439: iter = entityIdList.iterator();
0440: while (iter.hasNext()) {
0441: GenericValue entity = getNextEntity(delegator, entityName,
0442: pkFieldName, iter.next(), entities);
0443: if (entity == null)
0444: continue;
0445: String entityId = entity.getString(pkFieldName);
0446: List tmpPurposeList = (List) purposes.get(entityId);
0447: if (purposeList != null) {
0448: if (tmpPurposeList != null) {
0449: purposeList.addAll(tmpPurposeList);
0450: }
0451: } else {
0452: purposeList = tmpPurposeList;
0453: }
0454:
0455: List tmpRoleList = getUserRoles(entity, userLogin,
0456: delegator);
0457: if (roleList != null) {
0458: if (tmpRoleList != null) {
0459: roleList.addAll(tmpRoleList);
0460: }
0461: } else {
0462: roleList = tmpRoleList;
0463: }
0464:
0465: String statusId = null;
0466: if (hasStatusOp && hasStatusField) {
0467: statusId = entity.getString("statusId");
0468: }
0469:
0470: passed = hasMatch(entityName, targetOperationEntityList,
0471: roleList, hasPurposeOp, purposeList, hasStatusOp,
0472: statusId);
0473: if (passed) {
0474: break;
0475: }
0476: roles.put(entityId, roleList);
0477: }
0478:
0479: if (passed)
0480: return true;
0481:
0482: // Follow ownedEntityIds
0483: if (modelEntity.getField("owner" + entityName + "Id") != null) {
0484: iter = entityIdList.iterator();
0485: while (iter.hasNext()) {
0486: GenericValue entity = getNextEntity(delegator,
0487: entityName, pkFieldName, iter.next(), entities);
0488: if (entity == null)
0489: continue;
0490:
0491: String entityId = entity.getString(pkFieldName);
0492: List ownedContentIdList = new ArrayList();
0493: getEntityOwners(delegator, entity, ownedContentIdList,
0494: "Content", "ownerContentId");
0495:
0496: List ownedContentRoleIds = getUserRolesFromList(
0497: delegator, ownedContentIdList, partyId,
0498: "contentId", "partyId", "roleTypeId",
0499: "ContentRole");
0500: String statusId = null;
0501: if (hasStatusOp && hasStatusField) {
0502: statusId = entity.getString("statusId");
0503: }
0504:
0505: purposeList = (List) purposes.get(entityId);
0506: passed = hasMatch(entityName,
0507: targetOperationEntityList, ownedContentRoleIds,
0508: hasPurposeOp, purposeList, hasStatusOp,
0509: statusId);
0510: if (passed)
0511: break;
0512:
0513: /*
0514: String ownedEntityId = entity.getString("owner" + entityName + "Id");
0515: GenericValue ownedEntity = delegator.findByPrimaryKeyCache(entityName,UtilMisc.toMap(pkFieldName, ownedEntityId));
0516: while (ownedEntity != null) {
0517: if (!alreadyCheckedIds.contains(ownedEntityId)) {
0518: // Decided to let the original purposes only be used in permission checking
0519: //
0520: //purposeList = (List)purposes.get(entityId);
0521: //purposeList = getRelatedPurposes(ownedEntity, purposeList);
0522: roleList = getUserRoles(ownedEntity, userLogin, delegator);
0523:
0524: String statusId = null;
0525: if (hasStatusOp && hasStatusField) {
0526: statusId = entity.getString("statusId");
0527: }
0528:
0529: passed = hasMatch(entityName, targetOperationEntityList, roleList, hasPurposeOp, purposeList, hasStatusOp, statusId);
0530: if (passed)
0531: break;
0532: alreadyCheckedIds.add(ownedEntityId);
0533: //purposes.put(ownedEntityId, purposeList);
0534: //roles.put(ownedEntityId, roleList);
0535: ownedEntityId = ownedEntity.getString("owner" + entityName + "Id");
0536: ownedEntity = delegator.findByPrimaryKeyCache(entityName,UtilMisc.toMap(pkFieldName, ownedEntityId));
0537: } else {
0538: ownedEntity = null;
0539: }
0540: }
0541: if (passed)
0542: break;
0543: */
0544: }
0545: }
0546:
0547: /* seems like repeat
0548: // Check parents
0549: iter = entityIdList.iterator();
0550: while (iter.hasNext()) {
0551: String entityId = (String)iter.next();
0552: GenericValue entity = (GenericValue)entities.get(entityId);
0553: purposeList = (List)purposes.get(entityId);
0554: roleList = getUserRoles(entity, userLogin, delegator);
0555:
0556: String statusId = null;
0557: if (hasStatusOp && hasStatusField) {
0558: statusId = entity.getString("statusId");
0559: }
0560: String targetPrivilegeEnumId = null;
0561: if (hasPrivilegeOp && hasPrivilegeField) {
0562: targetPrivilegeEnumId = entity.getString("privilegeEnumId");
0563: }
0564:
0565: passed = hasMatch(entityName, targetOperationEntityList, roleList, hasPurposeOp, purposeList, hasStatusOp, statusId);
0566: if (passed)
0567: break;
0568: alreadyCheckedIds.add(entityId);
0569: }
0570: */
0571:
0572: return passed;
0573: }
0574:
0575: public static boolean checkPermissionMethod(
0576: GenericDelegator delegator, String partyId,
0577: String entityName, List entityIdList,
0578: AuxiliaryValueGetter auxiliaryValueGetter,
0579: RelatedRoleGetter relatedRoleGetter,
0580: PermissionConditionGetter permissionConditionGetter)
0581: throws GenericEntityException {
0582:
0583: permissionConditionGetter.init(delegator);
0584: if (Debug.verboseOn())
0585: Debug.logVerbose(permissionConditionGetter.dumpAsText(),
0586: module);
0587: boolean passed = false;
0588:
0589: boolean checkAncestors = false;
0590: boolean hasRoleOperation = checkHasRoleOperations(partyId,
0591: permissionConditionGetter, delegator);
0592: if (hasRoleOperation) {
0593: return true;
0594: }
0595: ModelEntity modelEntity = delegator.getModelEntity(entityName);
0596:
0597: if (relatedRoleGetter != null) {
0598: if (UtilValidate.isNotEmpty(partyId)) {
0599: relatedRoleGetter.setList(UtilMisc.toList("LOGGEDIN"));
0600: }
0601: }
0602:
0603: // check permission for each id in passed list until success.
0604: // Note that "quickCheck" id come first in the list
0605: // Check with no roles or purposes on the chance that the permission fields contain _NA_ s.
0606: String pkFieldName = modelEntity.getFirstPkFieldName();
0607: if (Debug.infoOn()) {
0608: String entityIdString = "ENTITIES: ";
0609: for (int i = 0; i < entityIdList.size(); i++) {
0610: Object obj = entityIdList.get(i);
0611: if (obj instanceof GenericValue) {
0612: String s = ((GenericValue) obj)
0613: .getString(pkFieldName);
0614: entityIdString += s + " ";
0615: } else {
0616: entityIdString += obj + " ";
0617: }
0618: }
0619: //if (Debug.infoOn()) Debug.logInfo(entityIdString, module);
0620: }
0621:
0622: List alreadyCheckedIds = new ArrayList();
0623: Map entities = new HashMap();
0624: Iterator iter = entityIdList.iterator();
0625: //List purposeList = null;
0626: //List roleList = null;
0627: while (iter.hasNext()) {
0628: GenericValue entity = getNextEntity(delegator, entityName,
0629: pkFieldName, iter.next(), entities);
0630: if (entity == null)
0631: continue;
0632: checkAncestors = false;
0633: passed = hasMatch(entity, permissionConditionGetter,
0634: relatedRoleGetter, null, partyId, checkAncestors);
0635: if (passed) {
0636: break;
0637: }
0638: }
0639:
0640: if (passed) {
0641: return true;
0642: }
0643:
0644: if (auxiliaryValueGetter != null) {
0645: //if (Debug.infoOn()) Debug.logInfo(auxiliaryValueGetter.dumpAsText(), module);
0646: // Check with just purposes next.
0647: iter = entityIdList.iterator();
0648: while (iter.hasNext()) {
0649: GenericValue entity = getNextEntity(delegator,
0650: entityName, pkFieldName, iter.next(), entities);
0651: if (entity == null)
0652: continue;
0653: checkAncestors = false;
0654: passed = hasMatch(entity, permissionConditionGetter,
0655: relatedRoleGetter, auxiliaryValueGetter,
0656: partyId, checkAncestors);
0657:
0658: if (passed) {
0659: break;
0660: }
0661: }
0662: }
0663:
0664: if (passed)
0665: return true;
0666:
0667: // TODO: need to return some information here about why it failed
0668: if (partyId == null)
0669: return false;
0670:
0671: // Check with roles.
0672: if (relatedRoleGetter != null) {
0673: iter = entityIdList.iterator();
0674: while (iter.hasNext()) {
0675: GenericValue entity = getNextEntity(delegator,
0676: entityName, pkFieldName, iter.next(), entities);
0677: if (entity == null)
0678: continue;
0679: checkAncestors = false;
0680: passed = hasMatch(entity, permissionConditionGetter,
0681: relatedRoleGetter, auxiliaryValueGetter,
0682: partyId, checkAncestors);
0683:
0684: if (passed) {
0685: break;
0686: }
0687: }
0688: }
0689:
0690: if (passed)
0691: return true;
0692:
0693: if (relatedRoleGetter != null) {
0694: iter = entityIdList.iterator();
0695: while (iter.hasNext()) {
0696: GenericValue entity = getNextEntity(delegator,
0697: entityName, pkFieldName, iter.next(), entities);
0698: if (entity == null)
0699: continue;
0700: checkAncestors = true;
0701: passed = hasMatch(entity, permissionConditionGetter,
0702: relatedRoleGetter, auxiliaryValueGetter,
0703: partyId, checkAncestors);
0704:
0705: if (passed) {
0706: break;
0707: }
0708: }
0709: }
0710:
0711: return passed;
0712: }
0713:
0714: public static GenericValue getNextEntity(
0715: GenericDelegator delegator, String entityName,
0716: String pkFieldName, Object obj, Map entities)
0717: throws GenericEntityException {
0718: GenericValue entity = null;
0719: if (obj instanceof String) {
0720: String entityId = (String) obj;
0721: if (entities != null)
0722: entity = (GenericValue) entities.get(entityId);
0723:
0724: if (entity == null)
0725: entity = delegator.findByPrimaryKeyCache(entityName,
0726: UtilMisc.toMap(pkFieldName, entityId));
0727: } else if (obj instanceof GenericValue) {
0728: entity = (GenericValue) obj;
0729: }
0730: return entity;
0731: }
0732:
0733: public static boolean checkHasRoleOperations(String partyId,
0734: PermissionConditionGetter permissionConditionGetter,
0735: GenericDelegator delegator) {
0736: List targetOperations = permissionConditionGetter
0737: .getOperationList();
0738: return checkHasRoleOperations(partyId, targetOperations,
0739: delegator);
0740: }
0741:
0742: public static boolean checkHasRoleOperations(String partyId,
0743: List targetOperations, GenericDelegator delegator) {
0744: //if (Debug.infoOn()) Debug.logInfo("targetOperations:" + targetOperations, module);
0745: //if (Debug.infoOn()) Debug.logInfo("userLoginId:" + userLoginId, module);
0746: if (targetOperations == null)
0747: return false;
0748:
0749: if (partyId != null
0750: && targetOperations.contains("HAS_USER_ROLE"))
0751: return true;
0752:
0753: boolean hasRoleOperation = false;
0754: Iterator targOpIter = targetOperations.iterator();
0755: boolean hasNeed = false;
0756: List newHasRoleList = new ArrayList();
0757: while (targOpIter.hasNext()) {
0758: String roleOp = (String) targOpIter.next();
0759: int idx1 = roleOp.indexOf("HAS_");
0760: if (idx1 == 0) {
0761: String roleOp1 = roleOp.substring(4); // lop off "HAS_"
0762: int idx2 = roleOp1.indexOf("_ROLE");
0763: if (idx2 == (roleOp1.length() - 5)) {
0764: String roleOp2 = roleOp1.substring(0, roleOp1
0765: .indexOf("_ROLE") - 1); // lop off "_ROLE"
0766: //if (Debug.infoOn()) Debug.logInfo("roleOp2:" + roleOp2, module);
0767: newHasRoleList.add(roleOp2);
0768: hasNeed = true;
0769: }
0770: }
0771: }
0772:
0773: if (hasNeed) {
0774: try {
0775: if (UtilValidate.isNotEmpty(partyId)) {
0776: List partyRoleList = delegator.findByAndCache(
0777: "PartyRole", UtilMisc.toMap("partyId",
0778: partyId));
0779: Iterator partyRoleIter = partyRoleList.iterator();
0780: while (partyRoleIter.hasNext()) {
0781: GenericValue partyRole = (GenericValue) partyRoleIter
0782: .next();
0783: String roleTypeId = partyRole
0784: .getString("roleTypeId");
0785: targOpIter = newHasRoleList.iterator();
0786: while (targOpIter.hasNext()) {
0787: String this Role = (String) targOpIter
0788: .next();
0789: if (roleTypeId.indexOf(this Role) >= 0) {
0790: hasRoleOperation = true;
0791: break;
0792: }
0793: }
0794: if (hasRoleOperation)
0795: break;
0796: }
0797: }
0798: } catch (GenericEntityException e) {
0799: Debug.logError(e, module);
0800: return hasRoleOperation;
0801: }
0802: }
0803: return hasRoleOperation;
0804: }
0805:
0806: public static boolean hasMatch(String entityName,
0807: List targetOperations, List roles, boolean hasPurposeOp,
0808: List purposes, boolean hasStatusOp, String targStatusId) {
0809: boolean isMatch = false;
0810: int targPrivilegeSeq = 0;
0811: // if (UtilValidate.isNotEmpty(targPrivilegeEnumId) && !targPrivilegeEnumId.equals("_NA_") && !targPrivilegeEnumId.equals("_00_") ) {
0812: // need to do a lookup here to find the seq value of targPrivilegeEnumId.
0813: // The lookup could be a static store or it could be done on Enumeration entity.
0814: // }
0815: String lcEntityName = entityName.toLowerCase();
0816: Iterator targetOpsIter = targetOperations.iterator();
0817: while (targetOpsIter.hasNext()) {
0818: GenericValue targetOp = (GenericValue) targetOpsIter.next();
0819: String testRoleTypeId = (String) targetOp.get("roleTypeId");
0820: String testContentPurposeTypeId = null;
0821: if (hasPurposeOp)
0822: testContentPurposeTypeId = (String) targetOp
0823: .get(lcEntityName + "PurposeTypeId");
0824: String testStatusId = null;
0825: if (hasStatusOp)
0826: testStatusId = (String) targetOp.get("statusId");
0827: //String testPrivilegeEnumId = null;
0828: //if (hasPrivilegeOp)
0829: //testPrivilegeEnumId = (String)targetOp.get("privilegeEnumId");
0830: //int testPrivilegeSeq = 0;
0831:
0832: boolean purposesCond = (!hasPurposeOp
0833: || (purposes != null && purposes
0834: .contains(testContentPurposeTypeId)) || testContentPurposeTypeId
0835: .equals("_NA_"));
0836: boolean statusCond = (!hasStatusOp
0837: || testStatusId.equals("_NA_") || (targStatusId != null && targStatusId
0838: .equals(testStatusId)));
0839: //boolean privilegeCond = ( !hasPrivilegeOp || testPrivilegeEnumId.equals("_NA_") || testPrivilegeSeq <= targPrivilegeSeq || testPrivilegeEnumId.equals(targPrivilegeEnumId) );
0840: boolean roleCond = (testRoleTypeId.equals("_NA_") || (roles != null && roles
0841: .contains(testRoleTypeId)));
0842:
0843: if (purposesCond && statusCond && roleCond) {
0844:
0845: isMatch = true;
0846: break;
0847: }
0848: }
0849: return isMatch;
0850: }
0851:
0852: public static boolean hasMatch(GenericValue entity,
0853: PermissionConditionGetter permissionConditionGetter,
0854: RelatedRoleGetter relatedRoleGetter,
0855: AuxiliaryValueGetter auxiliaryValueGetter, String partyId,
0856: boolean checkAncestors) throws GenericEntityException {
0857:
0858: ModelEntity modelEntity = entity.getModelEntity();
0859: GenericDelegator delegator = entity.getDelegator();
0860: String pkFieldName = modelEntity.getFirstPkFieldName();
0861: String entityId = entity.getString(pkFieldName);
0862: if (Debug.verboseOn())
0863: Debug.logVerbose("\n\nIN hasMatch: entityId:" + entityId
0864: + " partyId:" + partyId + " checkAncestors:"
0865: + checkAncestors, module);
0866: boolean isMatch = false;
0867: permissionConditionGetter.restart();
0868: List auxiliaryValueList = null;
0869: if (auxiliaryValueGetter != null) {
0870: auxiliaryValueGetter.init(delegator, entityId);
0871: auxiliaryValueList = auxiliaryValueGetter.getList();
0872: if (Debug.verboseOn())
0873: Debug.logVerbose(auxiliaryValueGetter.dumpAsText(),
0874: module);
0875: } else {
0876: if (Debug.verboseOn())
0877: Debug.logVerbose("NO AUX GETTER", module);
0878: }
0879: List roleValueList = null;
0880: if (relatedRoleGetter != null) {
0881: if (checkAncestors) {
0882: relatedRoleGetter.initWithAncestors(delegator, entity,
0883: partyId);
0884: } else {
0885: relatedRoleGetter.init(delegator, entityId, partyId,
0886: entity);
0887: }
0888: roleValueList = relatedRoleGetter.getList();
0889: if (Debug.verboseOn())
0890: Debug
0891: .logVerbose(relatedRoleGetter.dumpAsText(),
0892: module);
0893: } else {
0894: if (Debug.verboseOn())
0895: Debug.logVerbose("NO ROLE GETTER", module);
0896: }
0897:
0898: String targStatusId = null;
0899: if (modelEntity.getField("statusId") != null) {
0900: targStatusId = entity.getString("statusId");
0901: }
0902: if (Debug.verboseOn())
0903: Debug.logVerbose("STATUS:" + targStatusId, module);
0904:
0905: while (permissionConditionGetter.getNext()) {
0906: String roleConditionId = permissionConditionGetter
0907: .getRoleValue();
0908: String auxiliaryConditionId = permissionConditionGetter
0909: .getAuxiliaryValue();
0910: String statusConditionId = permissionConditionGetter
0911: .getStatusValue();
0912:
0913: boolean auxiliaryCond = (auxiliaryConditionId == null
0914: || auxiliaryConditionId.equals("_NA_") || (auxiliaryValueList != null && auxiliaryValueList
0915: .contains(auxiliaryConditionId)));
0916: boolean statusCond = (statusConditionId == null
0917: || statusConditionId.equals("_NA_") || (targStatusId != null && targStatusId
0918: .equals(statusConditionId)));
0919: boolean roleCond = (roleConditionId == null
0920: || roleConditionId.equals("_NA_") || (roleValueList != null && roleValueList
0921: .contains(roleConditionId)));
0922:
0923: if (auxiliaryCond && statusCond && roleCond) {
0924: if (Debug.verboseOn())
0925: Debug.logVerbose("MATCHED: role:" + roleConditionId
0926: + " status:" + statusConditionId + " aux:"
0927: + auxiliaryConditionId, module);
0928: isMatch = true;
0929: break;
0930: }
0931: }
0932: return isMatch;
0933: }
0934:
0935: /**
0936: * getRelatedPurposes
0937: */
0938: public static List getRelatedPurposes(GenericValue entity,
0939: List passedPurposes) {
0940:
0941: if (entity == null)
0942: return passedPurposes;
0943:
0944: List purposeIds = null;
0945: if (passedPurposes == null) {
0946: purposeIds = new ArrayList();
0947: } else {
0948: purposeIds = new ArrayList(passedPurposes);
0949: }
0950:
0951: String entityName = entity.getEntityName();
0952: String lcEntityName = entityName.toLowerCase();
0953:
0954: List purposes = null;
0955: try {
0956: purposes = entity.getRelatedCache(entityName + "Purpose");
0957: } catch (GenericEntityException e) {
0958: Debug.logError(e, "No associated purposes found. ", module);
0959: return purposeIds;
0960: }
0961:
0962: Iterator purposesIter = purposes.iterator();
0963: while (purposesIter.hasNext()) {
0964: GenericValue val = (GenericValue) purposesIter.next();
0965: purposeIds.add(val.get(lcEntityName + "PurposeTypeId"));
0966: }
0967:
0968: return purposeIds;
0969: }
0970:
0971: /**
0972: * getUserRoles
0973: * Queries for the ContentRoles associated with a Content entity
0974: * and returns the ones that match the user.
0975: * Follows group parties to see if the user is a member.
0976: */
0977: public static List getUserRoles(GenericValue entity,
0978: GenericValue userLogin, GenericDelegator delegator)
0979: throws GenericEntityException {
0980:
0981: String entityName = entity.getEntityName();
0982: List roles = new ArrayList();
0983: if (entity == null)
0984: return roles;
0985: // TODO: Need to use ContentManagementWorker.getAuthorContent first
0986:
0987: roles.remove("OWNER"); // always test with the owner of the current content
0988: if (entity.get("createdByUserLogin") != null
0989: && userLogin != null) {
0990: String userLoginId = (String) userLogin.get("userLoginId");
0991: String userLoginIdCB = (String) entity
0992: .get("createdByUserLogin");
0993: //if (Debug.infoOn()) Debug.logInfo("userLoginId:" + userLoginId + ": userLoginIdCB:" + userLoginIdCB + ":", null);
0994: if (userLoginIdCB.equals(userLoginId)) {
0995: roles.add("OWNER");
0996: //if (Debug.infoOn()) Debug.logInfo("in getUserRoles, passedRoles(0):" + passedRoles, null);
0997: }
0998: }
0999:
1000: String partyId = (String) userLogin.get("partyId");
1001: List relatedRoles = null;
1002: List tmpRelatedRoles = entity.getRelatedCache(entityName
1003: + "Role");
1004: relatedRoles = EntityUtil.filterByDate(tmpRelatedRoles);
1005: if (relatedRoles != null) {
1006: Iterator rolesIter = relatedRoles.iterator();
1007: while (rolesIter.hasNext()) {
1008: GenericValue contentRole = (GenericValue) rolesIter
1009: .next();
1010: String roleTypeId = (String) contentRole
1011: .get("roleTypeId");
1012: String targPartyId = (String) contentRole
1013: .get("partyId");
1014: if (targPartyId.equals(partyId)) {
1015: if (!roles.contains(roleTypeId))
1016: roles.add(roleTypeId);
1017: if (roleTypeId.equals("AUTHOR")
1018: && !roles.contains("OWNER"))
1019: roles.add("OWNER");
1020: } else { // Party may be of "PARTY_GROUP" type, in which case the userLogin may still possess this role
1021: GenericValue party = null;
1022: String partyTypeId = null;
1023: try {
1024: party = contentRole.getRelatedOne("Party");
1025: partyTypeId = (String) party.get("partyTypeId");
1026: if (partyTypeId != null
1027: && partyTypeId.equals("PARTY_GROUP")) {
1028: HashMap map = new HashMap();
1029:
1030: // At some point from/thru date will need to be added
1031: map.put("partyIdFrom", partyId);
1032: map.put("partyIdTo", targPartyId);
1033: if (isGroupMember(map, delegator)) {
1034: if (!roles.contains(roleTypeId))
1035: roles.add(roleTypeId);
1036: }
1037: }
1038: } catch (GenericEntityException e) {
1039: Debug.logError(e,
1040: "Error in finding related party. "
1041: + e.getMessage(), module);
1042: }
1043: }
1044: }
1045: }
1046: return roles;
1047: }
1048:
1049: /**
1050: * Tests to see if the user belongs to a group
1051: */
1052: public static boolean isGroupMember(Map partyRelationshipValues,
1053: GenericDelegator delegator) {
1054: boolean isMember = false;
1055: String partyIdFrom = (String) partyRelationshipValues
1056: .get("partyIdFrom");
1057: String partyIdTo = (String) partyRelationshipValues
1058: .get("partyIdTo");
1059: String roleTypeIdFrom = "PERMISSION_GROUP_MBR";
1060: String roleTypeIdTo = "PERMISSION_GROUP";
1061: Timestamp fromDate = UtilDateTime.nowTimestamp();
1062: Timestamp thruDate = UtilDateTime.getDayStart(UtilDateTime
1063: .nowTimestamp(), 1);
1064:
1065: if (partyRelationshipValues.get("roleTypeIdFrom") != null) {
1066: roleTypeIdFrom = (String) partyRelationshipValues
1067: .get("roleTypeIdFrom");
1068: }
1069: if (partyRelationshipValues.get("roleTypeIdTo") != null) {
1070: roleTypeIdTo = (String) partyRelationshipValues
1071: .get("roleTypeIdTo");
1072: }
1073: if (partyRelationshipValues.get("fromDate") != null) {
1074: fromDate = (Timestamp) partyRelationshipValues
1075: .get("fromDate");
1076: }
1077: if (partyRelationshipValues.get("thruDate") != null) {
1078: thruDate = (Timestamp) partyRelationshipValues
1079: .get("thruDate");
1080: }
1081:
1082: EntityExpr partyFromExpr = new EntityExpr("partyIdFrom",
1083: EntityOperator.EQUALS, partyIdFrom);
1084: EntityExpr partyToExpr = new EntityExpr("partyIdTo",
1085: EntityOperator.EQUALS, partyIdTo);
1086:
1087: EntityExpr relationExpr = new EntityExpr(
1088: "partyRelationshipTypeId", EntityOperator.EQUALS,
1089: "CONTENT_PERMISSION");
1090: //EntityExpr roleTypeIdFromExpr = new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "CONTENT_PERMISSION_GROUP_MEMBER");
1091: //EntityExpr roleTypeIdToExpr = new EntityExpr("roleTypeIdTo", EntityOperator.EQUALS, "CONTENT_PERMISSION_GROUP");
1092: EntityExpr fromExpr = new EntityExpr("fromDate",
1093: EntityOperator.LESS_THAN_EQUAL_TO, fromDate);
1094: EntityCondition thruCond = new EntityConditionList(UtilMisc
1095: .toList(new EntityExpr("thruDate",
1096: EntityOperator.EQUALS, null), new EntityExpr(
1097: "thruDate", EntityOperator.GREATER_THAN,
1098: thruDate)), EntityOperator.OR);
1099:
1100: // This method is simplified to make it work, these conditions need to be added back in.
1101: //List joinList = UtilMisc.toList(fromExpr, thruCond, partyFromExpr, partyToExpr, relationExpr);
1102: List joinList = UtilMisc.toList(partyFromExpr, partyToExpr);
1103: EntityCondition condition = new EntityConditionList(joinList,
1104: EntityOperator.AND);
1105:
1106: List partyRelationships = null;
1107: try {
1108: partyRelationships = delegator.findByCondition(
1109: "PartyRelationship", condition, null, null);
1110: } catch (GenericEntityException e) {
1111: Debug.logError(e, "Problem finding PartyRelationships. ",
1112: module);
1113: return false;
1114: }
1115: if (partyRelationships.size() > 0) {
1116: isMember = true;
1117: }
1118:
1119: return isMember;
1120: }
1121:
1122: public interface PermissionConditionGetter {
1123:
1124: public boolean getNext();
1125:
1126: public String getRoleValue();
1127:
1128: public String getOperationValue();
1129:
1130: public String getStatusValue();
1131:
1132: public int getPrivilegeValue() throws GenericEntityException;
1133:
1134: public String getAuxiliaryValue();
1135:
1136: public void init(GenericDelegator delegator)
1137: throws GenericEntityException;
1138:
1139: public void restart();
1140:
1141: public void setOperationList(String operationIdString);
1142:
1143: public void setOperationList(List opList);
1144:
1145: public List getOperationList();
1146:
1147: public String dumpAsText();
1148:
1149: public void clearList();
1150: }
1151:
1152: public static class StdPermissionConditionGetter implements
1153: PermissionConditionGetter {
1154:
1155: protected List entityList;
1156: protected List operationList;
1157: protected ListIterator iter;
1158: protected GenericValue currentValue;
1159: protected String operationFieldName;
1160: protected String roleFieldName;
1161: protected String statusFieldName;
1162: protected String privilegeFieldName;
1163: protected String auxiliaryFieldName;
1164: protected String entityName;
1165:
1166: public StdPermissionConditionGetter() {
1167:
1168: this .operationFieldName = "contentOperationId";
1169: this .roleFieldName = "roleTypeId";
1170: this .statusFieldName = "statusId";
1171: this .privilegeFieldName = "privilegeEnumId";
1172: this .auxiliaryFieldName = "contentPurposeTypeId";
1173: this .entityName = "ContentPurposeOperation";
1174: }
1175:
1176: public StdPermissionConditionGetter(String entityName,
1177: String operationFieldName, String roleFieldName,
1178: String statusFieldName, String auxiliaryFieldName,
1179: String privilegeFieldName) {
1180:
1181: this .operationFieldName = operationFieldName;
1182: this .roleFieldName = roleFieldName;
1183: this .statusFieldName = statusFieldName;
1184: this .privilegeFieldName = privilegeFieldName;
1185: this .auxiliaryFieldName = auxiliaryFieldName;
1186: this .entityName = entityName;
1187: }
1188:
1189: public StdPermissionConditionGetter(Element getterElement) {
1190: this .operationFieldName = getterElement
1191: .getAttribute("operation-field-name");
1192: this .roleFieldName = getterElement
1193: .getAttribute("role-field-name");
1194: this .statusFieldName = getterElement
1195: .getAttribute("status-field-name");
1196: this .privilegeFieldName = getterElement
1197: .getAttribute("privilege-field-name");
1198: this .auxiliaryFieldName = getterElement
1199: .getAttribute("auxiliary-field-name");
1200: this .entityName = getterElement.getAttribute("entity-name");
1201: }
1202:
1203: public boolean getNext() {
1204: boolean hasNext = false;
1205: if (iter != null && iter.hasNext()) {
1206: currentValue = (GenericValue) iter.next();
1207: hasNext = true;
1208: }
1209: return hasNext;
1210: }
1211:
1212: public String getRoleValue() {
1213: return this .currentValue.getString(this .roleFieldName);
1214: }
1215:
1216: public String getOperationValue() {
1217: return this .currentValue.getString(this .operationFieldName);
1218: }
1219:
1220: public String getStatusValue() {
1221: return this .currentValue.getString(this .statusFieldName);
1222:
1223: }
1224:
1225: public int getPrivilegeValue() throws GenericEntityException {
1226: int privilegeEnumSeq = -1;
1227: String privilegeEnumId = null;
1228: GenericDelegator delegator = currentValue.getDelegator();
1229:
1230: if (UtilValidate.isNotEmpty(privilegeFieldName)) {
1231: privilegeEnumId = currentValue
1232: .getString(this .privilegeFieldName);
1233: }
1234: if (UtilValidate.isNotEmpty(privilegeEnumId)) {
1235: GenericValue privEnum = delegator
1236: .findByPrimaryKeyCache("Enumeration", UtilMisc
1237: .toMap("enumId", privilegeEnumId));
1238: if (privEnum != null) {
1239: String sequenceId = privEnum
1240: .getString("sequenceId");
1241: try {
1242: privilegeEnumSeq = Integer.parseInt(sequenceId);
1243: } catch (NumberFormatException e) {
1244: // just leave it at -1
1245: }
1246: }
1247: }
1248: return privilegeEnumSeq;
1249:
1250: }
1251:
1252: public String getAuxiliaryValue() {
1253: return this .currentValue.getString(this .auxiliaryFieldName);
1254: }
1255:
1256: public void setOperationList(String operationIdString) {
1257:
1258: this .operationList = null;
1259: if (UtilValidate.isNotEmpty(operationIdString)) {
1260: this .operationList = StringUtil.split(
1261: operationIdString, "|");
1262: }
1263: }
1264:
1265: public void setOperationList(List operationList) {
1266: this .operationList = operationList;
1267: }
1268:
1269: public List getOperationList() {
1270: return this .operationList;
1271: }
1272:
1273: public void clearList() {
1274: this .entityList = new ArrayList();
1275: }
1276:
1277: public void init(GenericDelegator delegator)
1278: throws GenericEntityException {
1279: EntityCondition opCond = new EntityExpr(operationFieldName,
1280: EntityOperator.IN, this .operationList);
1281: this .entityList = delegator.findByConditionCache(
1282: this .entityName, opCond, null, null);
1283: }
1284:
1285: public void restart() {
1286: this .iter = null;
1287: if (this .entityList != null) {
1288: this .iter = this .entityList.listIterator();
1289: }
1290: }
1291:
1292: public String dumpAsText() {
1293: List fieldNames = UtilMisc.toList("roleFieldName",
1294: "auxiliaryFieldName", "statusFieldName");
1295: Map widths = UtilMisc.toMap("roleFieldName",
1296: new Integer(24), "auxiliaryFieldName", new Integer(
1297: 24), "statusFieldName", new Integer(24));
1298: StringBuffer buf = new StringBuffer();
1299: Integer wid = null;
1300:
1301: buf.append("Dump for ");
1302: buf.append(this .entityName);
1303: buf.append(" ops:");
1304: buf.append(StringUtil.join(this .operationList, ","));
1305: buf.append("\n");
1306: Iterator itFields = fieldNames.iterator();
1307: while (itFields.hasNext()) {
1308: String fld = (String) itFields.next();
1309: wid = (Integer) widths.get(fld);
1310: buf.append(fld);
1311: for (int i = 0; i < (wid.intValue() - fld.length()); i++)
1312: buf.append("^");
1313: buf.append(" ");
1314: }
1315: buf.append("\n");
1316: itFields = fieldNames.iterator();
1317: while (itFields.hasNext()) {
1318: String fld = (String) itFields.next();
1319: wid = (Integer) widths.get(fld);
1320: for (int i = 0; i < wid.intValue(); i++)
1321: buf.append("-");
1322: buf.append(" ");
1323: }
1324: buf.append("\n");
1325: if (entityList != null) {
1326: Iterator it = this .entityList.iterator();
1327: while (it.hasNext()) {
1328: GenericValue contentPurposeOperation = (GenericValue) it
1329: .next();
1330: /*
1331: String contentOperationId = contentPurposeOperation.getString(this.operationFieldName);
1332: if (UtilValidate.isEmpty(contentOperationId)) {
1333: contentOperationId = "";
1334: }
1335: wid = (Integer)widths.get("operationFieldName");
1336: buf.append(contentOperationId);
1337: for (int i=0; i < (wid.intValue() - contentOperationId.length()); i++) buf.append("^");
1338: buf.append(" ");
1339: */
1340:
1341: String roleTypeId = contentPurposeOperation
1342: .getString(this .roleFieldName);
1343: if (UtilValidate.isEmpty(roleTypeId)) {
1344: roleTypeId = "";
1345: }
1346: wid = (Integer) widths.get("roleFieldName");
1347: buf.append(roleTypeId);
1348: for (int i = 0; i < (wid.intValue() - roleTypeId
1349: .length()); i++)
1350: buf.append("^");
1351: buf.append(" ");
1352:
1353: String auxiliaryFieldValue = contentPurposeOperation
1354: .getString(this .auxiliaryFieldName);
1355: if (UtilValidate.isEmpty(auxiliaryFieldValue)) {
1356: auxiliaryFieldValue = "";
1357: }
1358: wid = (Integer) widths.get("auxiliaryFieldName");
1359: buf.append(auxiliaryFieldValue);
1360: for (int i = 0; i < (wid.intValue() - auxiliaryFieldValue
1361: .length()); i++)
1362: buf.append("^");
1363: buf.append(" ");
1364:
1365: String statusId = contentPurposeOperation
1366: .getString(this .statusFieldName);
1367: if (UtilValidate.isEmpty(statusId)) {
1368: statusId = "";
1369: }
1370: buf.append(statusId);
1371: /*
1372: wid = (Integer)widths.get("statusFieldName");
1373: for (int i=0; i < (wid.intValue() - statusId.length()); i++) buf.append(" ");
1374: */
1375: buf.append(" ");
1376:
1377: buf.append("\n");
1378: }
1379: }
1380: return buf.toString();
1381: }
1382: }
1383:
1384: public interface AuxiliaryValueGetter {
1385: public void init(GenericDelegator delegator, String entityId)
1386: throws GenericEntityException;
1387:
1388: public List getList();
1389:
1390: public void clearList();
1391:
1392: public String dumpAsText();
1393: }
1394:
1395: public static class StdAuxiliaryValueGetter implements
1396: AuxiliaryValueGetter {
1397:
1398: protected List entityList = new ArrayList();
1399: protected String auxiliaryFieldName;
1400: protected String entityName;
1401: protected String entityIdName;
1402:
1403: public StdAuxiliaryValueGetter() {
1404:
1405: this .auxiliaryFieldName = "contentPurposeTypeId";
1406: this .entityName = "ContentPurpose";
1407: this .entityIdName = "contentId";
1408: }
1409:
1410: public StdAuxiliaryValueGetter(String entityName,
1411: String auxiliaryFieldName, String entityIdName) {
1412:
1413: this .auxiliaryFieldName = auxiliaryFieldName;
1414: this .entityName = entityName;
1415: this .entityIdName = entityIdName;
1416: }
1417:
1418: public StdAuxiliaryValueGetter(Element getterElement) {
1419:
1420: this .auxiliaryFieldName = getterElement
1421: .getAttribute("auxiliary-field-name");
1422: this .entityName = getterElement.getAttribute("entity-name");
1423: this .entityIdName = getterElement
1424: .getAttribute("entity-id-name");
1425: }
1426:
1427: public List getList() {
1428: return entityList;
1429: }
1430:
1431: public void clearList() {
1432: this .entityList = new ArrayList();
1433: }
1434:
1435: public void setList(List lst) {
1436: this .entityList = lst;
1437: }
1438:
1439: public void init(GenericDelegator delegator, String entityId)
1440: throws GenericEntityException {
1441:
1442: if (this .entityList == null) {
1443: this .entityList = new ArrayList();
1444: }
1445: if (UtilValidate.isEmpty(this .entityName)) {
1446: return;
1447: }
1448: List values = delegator.findByAndCache(this .entityName,
1449: UtilMisc.toMap(this .entityIdName, entityId));
1450: Iterator iter = values.iterator();
1451: while (iter.hasNext()) {
1452: GenericValue entity = (GenericValue) iter.next();
1453: this .entityList.add(entity
1454: .getString(this .auxiliaryFieldName));
1455: }
1456: }
1457:
1458: public String dumpAsText() {
1459: StringBuffer buf = new StringBuffer();
1460: buf.append("AUXILIARY: ");
1461: if (entityList != null) {
1462: for (int i = 0; i < entityList.size(); i++) {
1463: String val = (String) entityList.get(i);
1464: buf.append(val);
1465: buf.append(" ");
1466: }
1467: }
1468: return buf.toString();
1469: }
1470: }
1471:
1472: public interface RelatedRoleGetter {
1473: public void init(GenericDelegator delegator, String entityId,
1474: String partyId, GenericValue entity)
1475: throws GenericEntityException;
1476:
1477: public void initWithAncestors(GenericDelegator delegator,
1478: GenericValue entity, String partyId)
1479: throws GenericEntityException;
1480:
1481: public List getList();
1482:
1483: public void clearList();
1484:
1485: public void setList(List lst);
1486:
1487: public String dumpAsText();
1488:
1489: public boolean isOwner(GenericValue entity, String targetPartyId);
1490: }
1491:
1492: public static class StdRelatedRoleGetter implements
1493: RelatedRoleGetter {
1494:
1495: protected List roleIdList = new ArrayList();
1496: protected String roleTypeFieldName;
1497: protected String partyFieldName;
1498: protected String entityName;
1499: protected String roleEntityIdName;
1500: protected String roleEntityName;
1501: protected String ownerEntityFieldName;
1502:
1503: public StdRelatedRoleGetter() {
1504:
1505: this .roleTypeFieldName = "roleTypeId";
1506: this .partyFieldName = "partyId";
1507: this .ownerEntityFieldName = "ownerContentId";
1508: this .entityName = "Content";
1509: this .roleEntityName = "ContentRole";
1510: this .roleEntityIdName = "contentId";
1511: }
1512:
1513: public StdRelatedRoleGetter(String entityName,
1514: String roleTypeFieldName, String roleEntityIdName,
1515: String partyFieldName, String ownerEntityFieldName,
1516: String roleEntityName) {
1517:
1518: this .roleTypeFieldName = roleTypeFieldName;
1519: this .partyFieldName = partyFieldName;
1520: this .ownerEntityFieldName = ownerEntityFieldName;
1521: this .entityName = entityName;
1522: this .roleEntityName = roleEntityName;
1523: this .roleEntityIdName = roleEntityIdName;
1524: }
1525:
1526: public StdRelatedRoleGetter(Element getterElement) {
1527:
1528: this .roleTypeFieldName = getterElement
1529: .getAttribute("role-type-field-name");
1530: this .partyFieldName = getterElement
1531: .getAttribute("party-field-name");
1532: this .ownerEntityFieldName = getterElement
1533: .getAttribute("owner-entity-field-name");
1534: this .entityName = getterElement.getAttribute("entity-name");
1535: this .roleEntityName = getterElement
1536: .getAttribute("role-entity-name");
1537: this .roleEntityIdName = getterElement
1538: .getAttribute("entity-id-name");
1539: }
1540:
1541: public List getList() {
1542: return this .roleIdList;
1543: }
1544:
1545: public void clearList() {
1546: this .roleIdList = new ArrayList();
1547: }
1548:
1549: public void setList(List lst) {
1550: this .roleIdList = lst;
1551: }
1552:
1553: public void init(GenericDelegator delegator, String entityId,
1554: String partyId, GenericValue entity)
1555: throws GenericEntityException {
1556:
1557: List lst = getUserRolesFromList(delegator, UtilMisc
1558: .toList(entityId), partyId, this .roleEntityIdName,
1559: this .partyFieldName, this .roleTypeFieldName,
1560: this .roleEntityName);
1561: this .roleIdList.addAll(lst);
1562: if (isOwner(entity, partyId)) {
1563: this .roleIdList.add("OWNER");
1564: }
1565: }
1566:
1567: public void initWithAncestors(GenericDelegator delegator,
1568: GenericValue entity, String partyId)
1569: throws GenericEntityException {
1570:
1571: List ownedContentIdList = new ArrayList();
1572: getEntityOwners(delegator, entity, ownedContentIdList,
1573: this .entityName, this .ownerEntityFieldName);
1574: if (ownedContentIdList.size() > 0) {
1575: List lst = getUserRolesFromList(delegator,
1576: ownedContentIdList, partyId,
1577: this .roleEntityIdName, this .partyFieldName,
1578: this .roleTypeFieldName, this .roleEntityName);
1579: this .roleIdList.addAll(lst);
1580: }
1581: }
1582:
1583: public boolean isOwner(GenericValue entity, String targetPartyId) {
1584: boolean isOwner = false;
1585: if (entity == null || targetPartyId == null) {
1586: return false;
1587: }
1588: GenericDelegator delegator = entity.getDelegator();
1589: ModelEntity modelEntity = delegator
1590: .getModelEntity(entityName);
1591: if (modelEntity.getField("createdByUserLogin") == null) {
1592: return false;
1593: }
1594: if (entity.get("createdByUserLogin") != null) {
1595: String userLoginIdCB = (String) entity
1596: .get("createdByUserLogin");
1597: try {
1598: GenericValue userLogin = delegator
1599: .findByPrimaryKeyCache("UserLogin",
1600: UtilMisc.toMap("userLoginId",
1601: userLoginIdCB));
1602: if (userLogin != null) {
1603: String partyIdCB = userLogin
1604: .getString("partyId");
1605: if (partyIdCB != null) {
1606: if (partyIdCB.equals(targetPartyId)) {
1607: isOwner = true;
1608: }
1609: }
1610: }
1611: } catch (GenericEntityException e) {
1612: Debug
1613: .logInfo(
1614: e.getMessage()
1615: + " Returning false for 'isOwner'.",
1616: module);
1617:
1618: }
1619: }
1620: return isOwner;
1621: }
1622:
1623: public String dumpAsText() {
1624: StringBuffer buf = new StringBuffer();
1625: buf.append("ROLES: ");
1626: if (roleIdList != null) {
1627: for (int i = 0; i < roleIdList.size(); i++) {
1628: String val = (String) roleIdList.get(i);
1629: buf.append(val);
1630: buf.append(" ");
1631: }
1632: }
1633: return buf.toString();
1634: }
1635: }
1636:
1637: public static List getUserRolesFromList(GenericDelegator delegator,
1638: List idList, String partyId, String entityIdFieldName,
1639: String partyIdFieldName, String roleTypeIdFieldName,
1640: String entityName) throws GenericEntityException {
1641:
1642: EntityExpr expr = new EntityExpr(entityIdFieldName,
1643: EntityOperator.IN, idList);
1644: EntityExpr expr2 = new EntityExpr(partyIdFieldName,
1645: EntityOperator.EQUALS, partyId);
1646: EntityConditionList condList = new EntityConditionList(UtilMisc
1647: .toList(expr, expr2), EntityOperator.AND);
1648: List roleList = delegator.findByConditionCache(entityName,
1649: condList, null, null);
1650: List roleListFiltered = EntityUtil.filterByDate(roleList);
1651: HashSet distinctSet = new HashSet();
1652: Iterator iter = roleListFiltered.iterator();
1653: while (iter.hasNext()) {
1654: GenericValue contentRole = (GenericValue) iter.next();
1655: String roleTypeId = contentRole
1656: .getString(roleTypeIdFieldName);
1657: distinctSet.add(roleTypeId);
1658: }
1659: List distinctList = Arrays.asList(distinctSet.toArray());
1660: return distinctList;
1661: }
1662:
1663: public static void getEntityOwners(GenericDelegator delegator,
1664: GenericValue entity, List contentOwnerList,
1665: String entityName, String ownerIdFieldName)
1666: throws GenericEntityException {
1667:
1668: String ownerContentId = entity.getString(ownerIdFieldName);
1669: if (UtilValidate.isNotEmpty(ownerContentId)) {
1670: contentOwnerList.add(ownerContentId);
1671: ModelEntity modelEntity = delegator
1672: .getModelEntity(entityName);
1673: String pkFieldName = modelEntity.getFirstPkFieldName();
1674: GenericValue ownerContent = delegator
1675: .findByPrimaryKeyCache(entityName, UtilMisc.toMap(
1676: pkFieldName, ownerContentId));
1677: if (ownerContent != null) {
1678: getEntityOwners(delegator, ownerContent,
1679: contentOwnerList, entityName, ownerIdFieldName);
1680: }
1681: }
1682:
1683: }
1684:
1685: public static int getPrivilegeEnumSeq(GenericDelegator delegator,
1686: String privilegeEnumId) throws GenericEntityException {
1687: int privilegeEnumSeq = -1;
1688:
1689: if (UtilValidate.isNotEmpty(privilegeEnumId)) {
1690: GenericValue privEnum = delegator.findByPrimaryKeyCache(
1691: "Enumeration", UtilMisc.toMap("enumId",
1692: privilegeEnumId));
1693: if (privEnum != null) {
1694: String sequenceId = privEnum.getString("sequenceId");
1695: try {
1696: privilegeEnumSeq = Integer.parseInt(sequenceId);
1697: } catch (NumberFormatException e) {
1698: // just leave it at -1
1699: }
1700: }
1701: }
1702: return privilegeEnumSeq;
1703: }
1704: }
|