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);
}
}
}
}
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;
}
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)));
}
});
}
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;
}
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())));
}
});
}
Aggregations