Search in sources :

Example 66 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityPermissionChecker method checkPermissionMethod.

public static boolean checkPermissionMethod(Delegator delegator, String partyId, String entityName, List<? extends Object> entityIdList, AuxiliaryValueGetter auxiliaryValueGetter, RelatedRoleGetter relatedRoleGetter, PermissionConditionGetter permissionConditionGetter) throws GenericEntityException {
    permissionConditionGetter.init(delegator);
    if (Debug.verboseOn())
        Debug.logVerbose(permissionConditionGetter.dumpAsText(), module);
    boolean passed = false;
    boolean checkAncestors = false;
    boolean hasRoleOperation = checkHasRoleOperations(partyId, permissionConditionGetter, delegator);
    if (hasRoleOperation) {
        return true;
    }
    ModelEntity modelEntity = delegator.getModelEntity(entityName);
    if (relatedRoleGetter != null) {
        if (UtilValidate.isNotEmpty(partyId)) {
            relatedRoleGetter.setList(UtilMisc.toList("LOGGEDIN"));
        }
    }
    // check permission for each id in passed list until success.
    // Note that "quickCheck" id come first in the list
    // Check with no roles or purposes on the chance that the permission fields contain _NA_ s.
    String pkFieldName = modelEntity.getFirstPkFieldName();
    if (Debug.infoOn()) {
        String entityIdString = "ENTITIES: ";
        for (Object obj : entityIdList) {
            if (obj instanceof GenericValue) {
                String s = ((GenericValue) obj).getString(pkFieldName);
                entityIdString += s + "  ";
            } else {
                entityIdString += obj + "  ";
            }
        }
    // if (Debug.infoOn()) Debug.logInfo(entityIdString, module);
    }
    Map<String, GenericValue> entities = new HashMap<String, GenericValue>();
    // List roleList = null;
    for (Object id : entityIdList) {
        GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, 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) {
        // Check with just purposes next.
        for (Object id : entityIdList) {
            GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, 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) {
        for (Object id : entityIdList) {
            GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, 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) {
        for (Object id : entityIdList) {
            GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, entities);
            if (entity == null)
                continue;
            checkAncestors = true;
            passed = hasMatch(entity, permissionConditionGetter, relatedRoleGetter, auxiliaryValueGetter, partyId, checkAncestors);
            if (passed) {
                break;
            }
        }
    }
    return passed;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 67 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityPermissionChecker method hasMatch.

public static boolean hasMatch(GenericValue entity, PermissionConditionGetter permissionConditionGetter, RelatedRoleGetter relatedRoleGetter, AuxiliaryValueGetter auxiliaryValueGetter, String partyId, boolean checkAncestors) throws GenericEntityException {
    ModelEntity modelEntity = entity.getModelEntity();
    Delegator 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<String> 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<String> 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;
}
Also used : Delegator(org.apache.ofbiz.entity.Delegator) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 68 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityPermissionChecker method checkPermissionMethod.

public static boolean checkPermissionMethod(Delegator delegator, GenericValue userLogin, List<String> targetOperationList, String entityName, List<? extends Object> entityIdList, List<String> purposeList, List<String> 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;
    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 LinkedList();
    // Iterator iterType = targetOperationList.iterator();
    // while (iterType.hasNext()) {
    // String op = (String)iterType.next();
    // condList.add(EntityCondition.makeCondition(lcEntityName + "OperationId", op));
    // }
    // EntityCondition opCond = EntityCondition.makeCondition(condList, EntityOperator.OR);
    List<GenericValue> targetOperationEntityList = EntityQuery.use(delegator).from(modelOperationEntity.getEntityName()).where(EntityCondition.makeCondition(lcEntityName + "OperationId", EntityOperator.IN, targetOperationList)).cache(true).queryList();
    Map<String, GenericValue> entities = new HashMap<String, GenericValue>();
    String pkFieldName = modelEntity.getFirstPkFieldName();
    // TODO: privilegeEnumId test
    /*
        if (hasPrivilegeOp && hasPrivilegeField) {
            int privilegeEnumSeq = -1;

            if (UtilValidate.isNotEmpty(privilegeEnumId)) {
                GenericValue privEnum = EntityQuery.use(delegator).from("Enumeration").where("enumId", privilegeEnumId).cache().queryOne();
                if (privEnum != null) {
                    String sequenceId = privEnum.getString("sequenceId");
                    try {
                        privilegeEnumSeq = Integer.parseInt(sequenceId);
                    } catch (NumberFormatException e) {
                        // just leave it at -1
                    }
                }
            }
            boolean thisPassed = true;
            Iterator iter = entityIdList.iterator();
            while (iter.hasNext()) {
                GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, iter.next(), entities);
                if (entity == null) continue;

                String entityId = entity.getString(pkFieldName);
                String targetPrivilegeEnumId = entity.getString("privilegeEnumId");
                if (UtilValidate.isNotEmpty(targetPrivilegeEnumId)) {
                    int targetPrivilegeEnumSeq = -1;
                    GenericValue privEnum = EntityQuery.use(delegator).from("Enumeration").where("enumId", privilegeEnumId).cache().queryOne();
                    if (privEnum != null) {
                        String sequenceId = privEnum.getString("sequenceId");
                        try {
                            targetPrivilegeEnumSeq = Integer.parseInt(sequenceId);
                        } catch (NumberFormatException e) {
                            // just leave it at -1
                        }
                        if (targetPrivilegeEnumSeq > privilegeEnumSeq) {
                            return false;
                        }
                    }
                }
                entities.put(entityId, entity);
            }
        }
        */
    // check permission for each id in passed list until success.
    // Note that "quickCheck" id come first in the list
    // Check with no roles or purposes on the chance that the permission fields contain _NA_ s.
    Map<String, List<String>> purposes = new HashMap<String, List<String>>();
    Map<String, List<String>> roles = new HashMap<String, List<String>>();
    // List roleList = null;
    for (Object id : entityIdList) {
        GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, entities);
        if (entity == null)
            continue;
        String statusId = null;
        if (hasStatusOp && hasStatusField) {
            statusId = entity.getString("statusId");
        }
        if (hasPrivilegeOp && hasPrivilegeField) {
            privilegeEnumId = entity.getString("privilegeEnumId");
            getPrivilegeEnumSeq(delegator, privilegeEnumId);
        }
        passed = hasMatch(entityName, targetOperationEntityList, roleList, hasPurposeOp, purposeList, hasStatusOp, statusId);
        if (passed) {
            break;
        }
    }
    if (passed) {
        return true;
    }
    if (hasPurposeOp) {
        // Check with just purposes next.
        for (Object id : entityIdList) {
            GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, entities);
            if (entity == null)
                continue;
            String entityId = entity.getString(pkFieldName);
            purposeList = getRelatedPurposes(entity, null);
            String statusId = null;
            if (hasStatusOp && hasStatusField) {
                statusId = entity.getString("statusId");
            }
            if (purposeList.size() > 0) {
                passed = hasMatch(entityName, targetOperationEntityList, roleList, hasPurposeOp, purposeList, hasStatusOp, statusId);
            }
            if (passed) {
                break;
            }
            purposes.put(entityId, purposeList);
        }
    }
    if (passed)
        return true;
    if (userLogin == null)
        return false;
    // Check with roles.
    for (Object id : entityIdList) {
        GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, entities);
        if (entity == null)
            continue;
        String entityId = entity.getString(pkFieldName);
        List<String> tmpPurposeList = purposes.get(entityId);
        if (purposeList != null) {
            if (tmpPurposeList != null) {
                purposeList.addAll(tmpPurposeList);
            }
        } else {
            purposeList = tmpPurposeList;
        }
        List<String> tmpRoleList = getUserRoles(entity, userLogin, delegator);
        if (roleList != null) {
            if (tmpRoleList != null) {
                roleList.addAll(tmpRoleList);
            }
        } else {
            roleList = tmpRoleList;
        }
        String statusId = null;
        if (hasStatusOp && hasStatusField) {
            statusId = entity.getString("statusId");
        }
        passed = hasMatch(entityName, targetOperationEntityList, roleList, hasPurposeOp, purposeList, hasStatusOp, statusId);
        if (passed) {
            break;
        }
        roles.put(entityId, roleList);
    }
    if (passed)
        return true;
    // Follow ownedEntityIds
    if (modelEntity.getField("owner" + entityName + "Id") != null) {
        for (Object id : entityIdList) {
            GenericValue entity = getNextEntity(delegator, entityName, pkFieldName, id, entities);
            if (entity == null)
                continue;
            String entityId = entity.getString(pkFieldName);
            List<String> ownedContentIdList = new LinkedList<String>();
            getEntityOwners(delegator, entity, ownedContentIdList, "Content", "ownerContentId");
            List<String> ownedContentRoleIds = getUserRolesFromList(delegator, ownedContentIdList, partyId, "contentId", "partyId", "roleTypeId", "ContentRole");
            String statusId = null;
            if (hasStatusOp && hasStatusField) {
                statusId = entity.getString("statusId");
            }
            purposeList = purposes.get(entityId);
            passed = hasMatch(entityName, targetOperationEntityList, ownedContentRoleIds, hasPurposeOp, purposeList, hasStatusOp, statusId);
            if (passed)
                break;
        /*
                   String ownedEntityId = entity.getString("owner" + entityName + "Id");
                   GenericValue ownedEntity = delegator.findOne(entityName,UtilMisc.toMap(pkFieldName, ownedEntityId), true);
                   while (ownedEntity != null) {
                       if (!alreadyCheckedIds.contains(ownedEntityId)) {
                        // Decided to let the original purposes only be used in permission checking
                        //
                        //purposeList = (List)purposes.get(entityId);
                        //purposeList = getRelatedPurposes(ownedEntity, purposeList);
                        roleList = getUserRoles(ownedEntity, userLogin, delegator);

                        String statusId = null;
                        if (hasStatusOp && hasStatusField) {
                            statusId = entity.getString("statusId");
                        }

                        passed = hasMatch(entityName, targetOperationEntityList, roleList, hasPurposeOp, purposeList, hasStatusOp, statusId);
                        if (passed)
                            break;
                        alreadyCheckedIds.add(ownedEntityId);
                       //purposes.put(ownedEntityId, purposeList);
                        //roles.put(ownedEntityId, roleList);
                           ownedEntityId = ownedEntity.getString("owner" + entityName + "Id");
                           ownedEntity = delegator.findOne(entityName,UtilMisc.toMap(pkFieldName, ownedEntityId), true);
                       } else {
                          ownedEntity = null;
                       }
                   }
                   if (passed)
                       break;
                       */
        }
    }
    return passed;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) UtilGenerics.checkList(org.apache.ofbiz.base.util.UtilGenerics.checkList) LinkedList(java.util.LinkedList) List(java.util.List) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 69 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityPermissionChecker method getEntityOwners.

public static void getEntityOwners(Delegator delegator, GenericValue entity, List<String> contentOwnerList, String entityName, String ownerIdFieldName) throws GenericEntityException {
    String ownerContentId = entity.getString(ownerIdFieldName);
    if (UtilValidate.isNotEmpty(ownerContentId)) {
        contentOwnerList.add(ownerContentId);
        ModelEntity modelEntity = delegator.getModelEntity(entityName);
        String pkFieldName = modelEntity.getFirstPkFieldName();
        GenericValue ownerContent = EntityQuery.use(delegator).from(entityName).where(pkFieldName, ownerContentId).cache(true).queryOne();
        if (ownerContent != null) {
            getEntityOwners(delegator, ownerContent, contentOwnerList, entityName, ownerIdFieldName);
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 70 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.

the class EntityDataServices method rebuildAllIndexesAndKeys.

public static Map<String, Object> rebuildAllIndexesAndKeys(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    Security security = dctx.getSecurity();
    Locale locale = (Locale) context.get("locale");
    // check permission
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    if (!security.hasPermission("ENTITY_MAINT", userLogin)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale));
    }
    String groupName = (String) context.get("groupName");
    Boolean fixSizes = (Boolean) context.get("fixColSizes");
    if (fixSizes == null)
        fixSizes = Boolean.FALSE;
    List<String> messages = new LinkedList<String>();
    GenericHelperInfo helperInfo = delegator.getGroupHelperInfo(groupName);
    DatabaseUtil dbUtil = new DatabaseUtil(helperInfo);
    Map<String, ModelEntity> modelEntities;
    try {
        modelEntities = delegator.getModelEntityMapByGroup(groupName);
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error getting list of entities in group: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorGettingListOfEntityInGroup", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    // step 1 - remove FK indices
    Debug.logImportant("Removing all foreign key indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deleteForeignKeyIndices(modelEntity, messages);
    }
    // step 2 - remove FKs
    Debug.logImportant("Removing all foreign keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deleteForeignKeys(modelEntity, modelEntities, messages);
    }
    // step 3 - remove PKs
    Debug.logImportant("Removing all primary keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deletePrimaryKey(modelEntity, messages);
    }
    // step 4 - remove declared indices
    Debug.logImportant("Removing all declared indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.deleteDeclaredIndices(modelEntity, messages);
    }
    // step 5 - repair field sizes
    if (fixSizes.booleanValue()) {
        Debug.logImportant("Updating column field size changes", module);
        List<String> fieldsWrongSize = new LinkedList<String>();
        dbUtil.checkDb(modelEntities, fieldsWrongSize, messages, true, true, true, true);
        if (fieldsWrongSize.size() > 0) {
            dbUtil.repairColumnSizeChanges(modelEntities, fieldsWrongSize, messages);
        } else {
            String thisMsg = "No field sizes to update";
            messages.add(thisMsg);
            Debug.logImportant(thisMsg, module);
        }
    }
    // step 6 - create PKs
    Debug.logImportant("Creating all primary keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createPrimaryKey(modelEntity, messages);
    }
    // step 7 - create FK indices
    Debug.logImportant("Creating all foreign key indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createForeignKeyIndices(modelEntity, messages);
    }
    // step 8 - create FKs
    Debug.logImportant("Creating all foreign keys", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createForeignKeys(modelEntity, modelEntities, messages);
    }
    // step 8 - create FKs
    Debug.logImportant("Creating all declared indices", module);
    for (ModelEntity modelEntity : modelEntities.values()) {
        dbUtil.createDeclaredIndices(modelEntity, messages);
    }
    // step 8 - checkdb
    Debug.logImportant("Running DB check with add missing enabled", module);
    dbUtil.checkDb(modelEntities, messages, true);
    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("messages", messages);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) GenericHelperInfo(org.apache.ofbiz.entity.datasource.GenericHelperInfo) Security(org.apache.ofbiz.security.Security) DatabaseUtil(org.apache.ofbiz.entity.jdbc.DatabaseUtil) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)102 GenericValue (org.apache.ofbiz.entity.GenericValue)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)29 ModelField (org.apache.ofbiz.entity.model.ModelField)28 HashMap (java.util.HashMap)22 Delegator (org.apache.ofbiz.entity.Delegator)17 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)16 LinkedList (java.util.LinkedList)14 Locale (java.util.Locale)12 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)11 ArrayList (java.util.ArrayList)10 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)10 IOException (java.io.IOException)8 TreeSet (java.util.TreeSet)8 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)8 Map (java.util.Map)7 GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)7 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)7 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)7 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)7