use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHookTest method verifyValidationFailsWhenLinkedTrackedEntityIsNotFound.
@Test
void verifyValidationFailsWhenLinkedTrackedEntityIsNotFound() {
RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, TRACKED_ENTITY_INSTANCE);
Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity("validTrackedEntity").build()).to(RelationshipItem.builder().trackedEntity("anotherValidTrackedEntity").build()).relationshipType(relType.getUid()).build();
hook.validateRelationship(reporter, relationship);
assertTrue(reporter.hasErrors());
assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E4012));
assertThat(reporter.getReportList().stream().map(TrackerErrorReport::getErrorMessage).collect(Collectors.toList()), hasItem("Could not find `trackedEntity`: `validTrackedEntity`, linked to Relationship."));
assertThat(reporter.getReportList().stream().map(TrackerErrorReport::getErrorMessage).collect(Collectors.toList()), hasItem("Could not find `trackedEntity`: `anotherValidTrackedEntity`, linked to Relationship."));
}
use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.
the class PreCheckSecurityOwnershipValidationHook method checkOrgUnitInCaptureScope.
private void checkOrgUnitInCaptureScope(ValidationErrorReporter reporter, TrackerDto dto, OrganisationUnit orgUnit) {
TrackerBundle bundle = reporter.getValidationContext().getBundle();
User user = bundle.getUser();
checkNotNull(user, USER_CANT_BE_NULL);
checkNotNull(orgUnit, ORGANISATION_UNIT_CANT_BE_NULL);
if (!organisationUnitService.isInUserHierarchyCached(user, orgUnit)) {
TrackerErrorReport error = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1000).addArg(user).addArg(orgUnit).build(bundle);
reporter.addError(error);
}
}
use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.
the class PreCheckSecurityOwnershipValidationHook method validateTrackedEntity.
@Override
public void validateTrackedEntity(ValidationErrorReporter reporter, TrackedEntity trackedEntity) {
TrackerImportValidationContext context = reporter.getValidationContext();
TrackerImportStrategy strategy = context.getStrategy(trackedEntity);
TrackerBundle bundle = context.getBundle();
User user = bundle.getUser();
checkNotNull(user, USER_CANT_BE_NULL);
checkNotNull(trackedEntity, TRACKED_ENTITY_CANT_BE_NULL);
checkNotNull(trackedEntity.getOrgUnit(), ORGANISATION_UNIT_CANT_BE_NULL);
// scope has to be checked
if (strategy.isCreate() || strategy.isDelete()) {
checkOrgUnitInCaptureScope(reporter, trackedEntity, context.getOrganisationUnit(trackedEntity.getOrgUnit()));
} else // if its to update trackedEntity, search scope has to be checked
{
checkOrgUnitInSearchScope(reporter, trackedEntity, context.getOrganisationUnit(trackedEntity.getOrgUnit()));
}
if (strategy.isDelete()) {
TrackedEntityInstance tei = context.getTrackedEntityInstance(trackedEntity.getTrackedEntity());
if (tei.getProgramInstances().stream().anyMatch(pi -> !pi.isDeleted()) && !user.isAuthorized(Authorities.F_TEI_CASCADE_DELETE.getAuthority())) {
TrackerErrorReport error = TrackerErrorReport.builder().uid(trackedEntity.getUid()).trackerType(TrackerType.TRACKED_ENTITY).errorCode(E1100).addArg(user).addArg(tei).build(bundle);
reporter.addError(error);
}
}
TrackedEntityType trackedEntityType = context.getTrackedEntityType(trackedEntity.getTrackedEntityType());
checkTeiTypeWriteAccess(reporter, trackedEntity.getUid(), trackedEntityType);
}
use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.
the class PreCheckSecurityOwnershipValidationHook method checkProgramWriteAccess.
private void checkProgramWriteAccess(ValidationErrorReporter reporter, TrackerDto dto, User user, Program program) {
checkNotNull(user, USER_CANT_BE_NULL);
checkNotNull(program, PROGRAM_CANT_BE_NULL);
if (!aclService.canDataWrite(user, program)) {
TrackerErrorReport error = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1091).addArg(user).addArg(program).build(reporter.getValidationContext().getBundle());
reporter.addError(error);
}
}
use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.
the class PreCheckSecurityOwnershipValidationHook method checkTeiTypeWriteAccess.
private void checkTeiTypeWriteAccess(ValidationErrorReporter reporter, String teUid, TrackedEntityType trackedEntityType) {
TrackerBundle bundle = reporter.getValidationContext().getBundle();
User user = bundle.getUser();
checkNotNull(user, USER_CANT_BE_NULL);
checkNotNull(trackedEntityType, TRACKED_ENTITY_TYPE_CANT_BE_NULL);
if (!aclService.canDataWrite(user, trackedEntityType)) {
TrackerErrorReport error = TrackerErrorReport.builder().uid(teUid).trackerType(TrackerType.TRACKED_ENTITY).errorCode(TrackerErrorCode.E1001).addArg(user).addArg(trackedEntityType).build(bundle);
reporter.addError(error);
}
}
Aggregations