Search in sources :

Example 1 with PreheatIdentifier

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

the class DefaultObjectBundleValidationService method validateSecurity.

private TypeReport validateSecurity(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle, ImportStrategy importMode) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects == null || objects.isEmpty()) {
        return typeReport;
    }
    Iterator<IdentifiableObject> iterator = objects.iterator();
    PreheatIdentifier identifier = bundle.getPreheatIdentifier();
    int idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        if (importMode.isCreate()) {
            if (!aclService.canCreate(bundle.getUser(), klass)) {
                ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
                objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
                objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3000, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
                typeReport.addObjectReport(objectReport);
                typeReport.getStats().incIgnored();
                iterator.remove();
                continue;
            }
        } else {
            IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
            if (importMode.isUpdate()) {
                if (!aclService.canUpdate(bundle.getUser(), persistedObject)) {
                    ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
                    objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
                    objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3001, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
                    typeReport.addObjectReport(objectReport);
                    typeReport.getStats().incIgnored();
                    iterator.remove();
                    continue;
                }
            } else if (importMode.isDelete()) {
                if (!aclService.canDelete(bundle.getUser(), persistedObject)) {
                    ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
                    objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
                    objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3002, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
                    typeReport.addObjectReport(objectReport);
                    typeReport.getStats().incIgnored();
                    iterator.remove();
                    continue;
                }
            }
        }
        if (User.class.isInstance(object)) {
            User user = (User) object;
            List<ErrorReport> errorReports = userService.validateUser(user, bundle.getUser());
            if (!errorReports.isEmpty()) {
                ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
                objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
                objectReport.addErrorReports(errorReports);
                typeReport.addObjectReport(objectReport);
                typeReport.getStats().incIgnored();
                iterator.remove();
                continue;
            }
        }
        List<ErrorReport> sharingErrorReports = aclService.verifySharing(object, bundle.getUser());
        if (!sharingErrorReports.isEmpty()) {
            ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReports(sharingErrorReports);
            typeReport.addObjectReport(objectReport);
            typeReport.getStats().incIgnored();
            iterator.remove();
            continue;
        }
        idx++;
    }
    return typeReport;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) User(org.hisp.dhis.user.User) TypeReport(org.hisp.dhis.feedback.TypeReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 2 with PreheatIdentifier

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

the class DefaultObjectBundleValidationService method checkReferences.

private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
    List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
    if (object == null) {
        return preheatErrorReports;
    }
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    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()) {
            IdentifiableObject refObject = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
            IdentifiableObject ref = preheat.get(identifier, refObject);
            if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
                if (!("user".equals(p.getName()) && User.class.isAssignableFrom(p.getKlass()) && skipSharing)) {
                    preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getName()));
                }
            }
        } else {
            Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance(p.getKlass());
            Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
            for (IdentifiableObject refObject : refObjects) {
                if (Preheat.isDefault(refObject))
                    continue;
                IdentifiableObject ref = preheat.get(identifier, refObject);
                if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
                    preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getCollectionName()));
                } else {
                    objects.add(refObject);
                }
            }
            ReflectionUtils.invokeMethod(object, p.getSetterMethod(), objects);
        }
    });
    if (schema.havePersistedProperty("attributeValues")) {
        object.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute() != null && preheat.get(identifier, attributeValue.getAttribute()) == null).forEach(attributeValue -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(attributeValue.getAttribute()), identifier.getIdentifiersWithName(object), "attributeValues")));
    }
    if (schema.havePersistedProperty("userGroupAccesses")) {
        object.getUserGroupAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUserGroup() != null && preheat.get(identifier, userGroupAccess.getUserGroup()) == null).forEach(userGroupAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userGroupAccesses.getUserGroup()), identifier.getIdentifiersWithName(object), "userGroupAccesses")));
    }
    if (schema.havePersistedProperty("userAccesses")) {
        object.getUserAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUser() != null && preheat.get(identifier, userGroupAccess.getUser()) == null).forEach(userAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userAccesses.getUser()), identifier.getIdentifiersWithName(object), "userAccesses")));
    }
    return preheatErrorReports;
}
Also used : 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) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 3 with PreheatIdentifier

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

the class UniquenessCheck method checkUniqueness.

private List<ErrorReport> checkUniqueness(IdentifiableObject object, PreheatIdentifier identifier, Map<String, Map<Object, String>> uniquenessMap, List<Property> uniqueProperties) {
    List<ErrorReport> errorReports = new ArrayList<>();
    uniqueProperties.forEach(property -> {
        Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (value != null) {
            String objectIdentifier = uniquenessMap.computeIfAbsent(property.getName(), key -> new HashMap<>()).get(value);
            if (objectIdentifier != null) {
                if (!identifier.getIdentifier(object).equals(objectIdentifier)) {
                    String identifiersWithName = identifier.getIdentifiersWithName(object);
                    ErrorReport errorReport = new ErrorReport(HibernateProxyUtils.getRealClass(object), ErrorCode.E5003, property.getName(), value, identifiersWithName, objectIdentifier);
                    errorReports.add(errorReport.setMainId(objectIdentifier).setErrorProperty(property.getName()));
                }
            } else {
                uniquenessMap.get(property.getName()).put(value, identifier.getIdentifier(object));
            }
        }
    });
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Collections.emptyList(java.util.Collections.emptyList) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) ValidationUtils.createObjectReport(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.createObjectReport) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 4 with PreheatIdentifier

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

the class UniquenessCheck method checkUniqueness.

private List<ErrorReport> checkUniqueness(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, ValidationContext ctx) {
    if (object == null || preheat.isDefault(object)) {
        return emptyList();
    }
    @SuppressWarnings("unchecked") Class<? extends IdentifiableObject> objType = HibernateProxyUtils.getRealClass(object);
    Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().computeIfAbsent(objType, key -> new HashMap<>());
    Schema schema = ctx.getSchemaService().getDynamicSchema(objType);
    List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
    if (uniqueProperties.isEmpty()) {
        return emptyList();
    }
    return checkUniqueness(object, identifier, uniquenessMap, uniqueProperties);
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Collections.emptyList(java.util.Collections.emptyList) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) ValidationUtils.createObjectReport(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.createObjectReport) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Schema(org.hisp.dhis.schema.Schema) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.hisp.dhis.schema.Property)

Example 5 with PreheatIdentifier

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

the class NotOwnerReferencesCheck method checkReferences.

private List<PreheatErrorReport> checkReferences(IdentifiableObject object, PreheatIdentifier identifier, 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.isOwner() && p.isWritable() && (PropertyType.REFERENCE == p.getPropertyType() && schema.getKlass() != p.getKlass() || PropertyType.REFERENCE == p.getItemPropertyType() && schema.getKlass() != p.getItemKlass())).forEach(p -> {
        if (!p.isCollection()) {
            checkReference(object, identifier, preheatErrorReports, p);
        } else {
            checkCollection(object, identifier, preheatErrorReports, p);
        }
    });
    return preheatErrorReports;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Collections.emptyList(java.util.Collections.emptyList) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) TypeReport(org.hisp.dhis.feedback.TypeReport) List(java.util.List) Component(org.springframework.stereotype.Component) ValidationUtils.joinObjects(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.joinObjects) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList)

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