Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate data sharing on program stage [DHIS2-17594][2.41] #19425

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ private Set<Enrollment> getEnrollments(
e -> {
Set<Event> filteredEvents =
e.getEvents().stream()
.filter(event -> includeDeleted || !event.isDeleted())
.filter(
event ->
(includeDeleted || !event.isDeleted())
&& trackerAccessManager.canRead(user, event, false).isEmpty())
.collect(Collectors.toSet());
e.setEvents(filteredEvents);
return e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserDetails;
import org.hisp.dhis.user.UserService;
import org.hisp.dhis.user.sharing.Sharing;
import org.junit.jupiter.api.Disabled;
Expand Down Expand Up @@ -164,14 +165,12 @@ class TrackedEntityServiceTest extends IntegrationTestBase {

private Enrollment enrollmentB;

private Enrollment enrollmentC;
private ProgramStage programStageA1;

private Event eventA;

private Event eventB;

private Event eventC;

private TrackedEntity trackedEntityA;

private TrackedEntity trackedEntityB;
Expand All @@ -180,8 +179,6 @@ class TrackedEntityServiceTest extends IntegrationTestBase {

private TrackedEntity trackedEntityGrandchildA;

private Note note;

private CategoryOptionCombo defaultCategoryOptionCombo;

private Relationship relationshipA;
Expand All @@ -194,6 +191,8 @@ class TrackedEntityServiceTest extends IntegrationTestBase {

private Relationship relationshipE;

private Note note;

private static List<String> uids(
Collection<? extends BaseIdentifiableObject> identifiableObjects) {
return identifiableObjects.stream()
Expand Down Expand Up @@ -282,7 +281,7 @@ protected void setUpTest() throws Exception {
programA.setCategoryCombo(defaultCategoryCombo);
programA.setMinAttributesRequiredToSearch(0);
manager.save(programA, false);
ProgramStage programStageA1 = createProgramStage(programA);
programStageA1 = createProgramStage(programA);
programStageA1.setPublicAccess(AccessStringHelper.FULL);
manager.save(programStageA1, false);
ProgramStage programStageA2 = createProgramStage(programA);
Expand Down Expand Up @@ -399,10 +398,10 @@ protected void setUpTest() throws Exception {
trackedEntityB.setTrackedEntityType(trackedEntityTypeA);
manager.save(trackedEntityB, false);

enrollmentC =
Enrollment enrollmentC =
enrollmentService.enrollTrackedEntity(
trackedEntityB, programB, new Date(), new Date(), orgUnitB);
eventC = new Event();
Event eventC = new Event();
eventC.setEnrollment(enrollmentC);
eventC.setProgramStage(programStageB1);
eventC.setOrganisationUnit(orgUnitB);
Expand Down Expand Up @@ -2071,6 +2070,51 @@ void shouldReturnTrackedEntityTypeAttributesWhenSingleTERequestedAndNoProgramSpe
trackedEntity.getTrackedEntityAttributeValues());
}

@Test
void shouldFindTrackedEntityWithEventsWhenEventRequestedAndAccessible()
throws ForbiddenException, NotFoundException, BadRequestException {
injectSecurityContextUser(getAdminUser());
User testUser = createAndAddUser(false, "testUser", emptySet(), emptySet(), "F_EXPORT_DATA");
testUser.setOrganisationUnits(Set.of(orgUnitA));
manager.update(testUser);
injectSecurityContext(UserDetails.fromUser(testUser));

TrackedEntity trackedEntity =
trackedEntityService.getTrackedEntity(
trackedEntityA.getUid(), programA.getUid(), TrackedEntityParams.TRUE, false);

assertEquals(trackedEntityA.getUid(), trackedEntity.getUid());
assertContainsOnly(Set.of(enrollmentA.getUid()), uids(trackedEntity.getEnrollments()));
List<Enrollment> enrollments = new ArrayList<>(trackedEntity.getEnrollments());
Optional<Enrollment> enrollment =
enrollments.stream().filter(e -> e.getUid().equals(this.enrollmentA.getUid())).findFirst();
Set<Event> events = enrollment.get().getEvents();
assertContainsOnly(Set.of(eventA.getUid()), uids(events));
}

@Test
void shouldFindTrackedEntityWithoutEventsWhenEventRequestedButNotAccessible()
throws ForbiddenException, NotFoundException, BadRequestException {
injectSecurityContextUser(getAdminUser());
programStageA1.setSharing(Sharing.builder().publicAccess("--------").build());
manager.update(programStageA1);
User testUser = createAndAddUser(false, "testUser", emptySet(), emptySet(), "F_EXPORT_DATA");
testUser.setOrganisationUnits(Set.of(orgUnitA));
manager.update(testUser);
injectSecurityContext(UserDetails.fromUser(testUser));

TrackedEntity trackedEntity =
trackedEntityService.getTrackedEntity(
trackedEntityA.getUid(), programA.getUid(), TrackedEntityParams.TRUE, false);

assertEquals(trackedEntityA.getUid(), trackedEntity.getUid());
assertContainsOnly(Set.of(enrollmentA.getUid()), uids(trackedEntity.getEnrollments()));
List<Enrollment> enrollments = new ArrayList<>(trackedEntity.getEnrollments());
Optional<Enrollment> enrollment =
enrollments.stream().filter(e -> e.getUid().equals(this.enrollmentA.getUid())).findFirst();
assertIsEmpty(enrollment.get().getEvents());
}

private Set<String> attributeNames(final Collection<TrackedEntityAttributeValue> attributes) {
// depends on createTrackedEntityAttribute() prefixing with "Attribute"
return attributes.stream()
Expand Down
Loading