Search in sources :

Example 6 with PreheatIdentifier

use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.

the class SecurityCheck method runValidationCheck.

private <T extends IdentifiableObject> void runValidationCheck(ObjectBundle bundle, Class<T> klass, List<T> objects, ImportStrategy importMode, ValidationContext ctx, Consumer<ObjectReport> addReports) {
    if (objects == null || objects.isEmpty()) {
        return;
    }
    PreheatIdentifier identifier = bundle.getPreheatIdentifier();
    for (T object : objects) {
        if (importMode.isCreate()) {
            if (!ctx.getAclService().canCreate(bundle.getUser(), klass)) {
                ErrorReport errorReport = new ErrorReport(klass, ErrorCode.E3000, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object));
                addReports.accept(createObjectReport(errorReport, object, bundle));
                ctx.markForRemoval(object);
                continue;
            }
        } else {
            T persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
            if (importMode.isUpdate()) {
                if (!ctx.getAclService().canUpdate(bundle.getUser(), persistedObject)) {
                    ErrorReport errorReport = new ErrorReport(klass, ErrorCode.E3001, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object));
                    addReports.accept(createObjectReport(errorReport, object, bundle));
                    ctx.markForRemoval(object);
                    continue;
                }
            } else if (importMode.isDelete() && !ctx.getAclService().canDelete(bundle.getUser(), persistedObject)) {
                ErrorReport errorReport = new ErrorReport(klass, ErrorCode.E3002, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object));
                addReports.accept(createObjectReport(errorReport, object, bundle));
                ctx.markForRemoval(object);
                continue;
            }
        }
        if (object instanceof User) {
            User user = (User) object;
            List<ErrorReport> errorReports = ctx.getUserService().validateUser(user, bundle.getUser());
            if (!errorReports.isEmpty()) {
                addReports.accept(createObjectReport(errorReports, object, bundle));
                ctx.markForRemoval(object);
            }
        }
        if (!bundle.isSkipSharing()) {
            List<ErrorReport> sharingErrorReports = ctx.getAclService().verifySharing(object, bundle.getUser());
            if (!sharingErrorReports.isEmpty()) {
                addReports.accept(createObjectReport(sharingErrorReports, object, bundle));
                ctx.markForRemoval(object);
            }
        }
    }
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) User(org.hisp.dhis.user.User) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier)

Example 7 with PreheatIdentifier

use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.

the class DefaultObjectBundleValidationService method checkUniqueness.

private List<ErrorReport> checkUniqueness(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (object == null || Preheat.isDefault(object))
        return errorReports;
    if (!preheat.getUniquenessMap().containsKey(object.getClass())) {
        preheat.getUniquenessMap().put(object.getClass(), new HashMap<>());
    }
    Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().get(object.getClass());
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
    uniqueProperties.forEach(property -> {
        if (!uniquenessMap.containsKey(property.getName())) {
            uniquenessMap.put(property.getName(), new HashMap<>());
        }
        Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (value != null) {
            String persistedUid = uniquenessMap.get(property.getName()).get(value);
            if (persistedUid != null) {
                if (!object.getUid().equals(persistedUid)) {
                    errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E5003, property.getName(), value, identifier.getIdentifiersWithName(object), persistedUid).setMainId(persistedUid).setErrorProperty(property.getName()));
                }
            } else {
                uniquenessMap.get(property.getName()).put(value, object.getUid());
            }
        }
    });
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.hisp.dhis.attribute.Attribute) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) TypeReport(org.hisp.dhis.feedback.TypeReport) UserCredentials(org.hisp.dhis.user.UserCredentials) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Service(org.springframework.stereotype.Service) Map(java.util.Map) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) UserService(org.hisp.dhis.user.UserService) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) Timer(org.hisp.dhis.commons.timer.Timer) SchemaService(org.hisp.dhis.schema.SchemaService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) SchemaValidator(org.hisp.dhis.schema.validation.SchemaValidator) List(java.util.List) AclService(org.hisp.dhis.security.acl.AclService) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) Transactional(org.springframework.transaction.annotation.Transactional) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.hisp.dhis.schema.Property)

Example 8 with PreheatIdentifier

use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.

the class ProgramStageObjectBundleHook method validateProgramStageDataElementsAcl.

private void validateProgramStageDataElementsAcl(ProgramStage programStage, ObjectBundle bundle, Consumer<ErrorReport> addReports) {
    if (programStage.getDataElements().isEmpty()) {
        return;
    }
    PreheatIdentifier identifier = bundle.getPreheatIdentifier();
    programStage.getDataElements().forEach(de -> {
        DataElement dataElement = bundle.getPreheat().get(identifier, de);
        if (dataElement == null || !aclService.canRead(bundle.getUser(), de)) {
            addReports.accept(new ErrorReport(DataElement.class, ErrorCode.E3012, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(de)));
        }
    });
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) DataElement(org.hisp.dhis.dataelement.DataElement) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier)

Example 9 with PreheatIdentifier

use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.

the class ReferencesCheck method checkReferences.

private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing, ValidationContext ctx) {
    if (object == null) {
        return emptyList();
    }
    List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
    Schema schema = ctx.getSchemaService().getDynamicSchema(HibernateProxyUtils.getRealClass(object));
    schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).forEach(p -> {
        if (skipCheck(p.getKlass()) || skipCheck(p.getItemKlass())) {
            return;
        }
        if (!p.isCollection()) {
            checkReference(object, preheat, identifier, skipSharing, preheatErrorReports, p);
        } else {
            checkCollection(object, preheat, identifier, preheatErrorReports, p);
        }
    });
    if (schema.havePersistedProperty("attributeValues")) {
        checkAttributeValues(object, preheat, identifier, preheatErrorReports);
    }
    if (schema.havePersistedProperty("sharing") && !skipSharing && object.getSharing() != null) {
        checkSharing(object, preheat, preheatErrorReports);
    }
    return preheatErrorReports;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) ArrayList(java.util.ArrayList) TypeReport(org.hisp.dhis.feedback.TypeReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Sharing(org.hisp.dhis.user.sharing.Sharing) Property(org.hisp.dhis.schema.Property) List(java.util.List) Component(org.springframework.stereotype.Component) ValidationUtils.joinObjects(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.joinObjects) PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList)

Example 10 with PreheatIdentifier

use of org.hisp.dhis.preheat.PreheatIdentifier in project dhis2-core by dhis2.

the class ProgramObjectBundleHook method validateAttributeSecurity.

private void validateAttributeSecurity(Program program, ObjectBundle bundle, Consumer<ErrorReport> addReports) {
    if (program.getProgramAttributes().isEmpty()) {
        return;
    }
    PreheatIdentifier identifier = bundle.getPreheatIdentifier();
    program.getProgramAttributes().forEach(programAttr -> {
        TrackedEntityAttribute attribute = bundle.getPreheat().get(identifier, programAttr.getAttribute());
        if (attribute == null || !aclService.canRead(bundle.getUser(), attribute)) {
            addReports.accept(new ErrorReport(TrackedEntityAttribute.class, ErrorCode.E3012, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(programAttr.getAttribute())));
        }
    });
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier)

Aggregations

PreheatIdentifier (org.hisp.dhis.preheat.PreheatIdentifier)10 ErrorReport (org.hisp.dhis.feedback.ErrorReport)8 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)7 ObjectReport (org.hisp.dhis.feedback.ObjectReport)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 ErrorCode (org.hisp.dhis.feedback.ErrorCode)6 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)6 Property (org.hisp.dhis.schema.Property)6 Schema (org.hisp.dhis.schema.Schema)6 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)6 TypeReport (org.hisp.dhis.feedback.TypeReport)5 Preheat (org.hisp.dhis.preheat.Preheat)5 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)5 User (org.hisp.dhis.user.User)5 Collection (java.util.Collection)4 Collections.emptyList (java.util.Collections.emptyList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4