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: (2.41) fix search scope breaks trackedEntities endpoint[DHIS2-17933] #20236

Open
wants to merge 4 commits into
base: 2.41
Choose a base branch
from
Open
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 @@ -42,6 +42,7 @@
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.IllegalQueryException;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.QueryFilter;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
Expand Down Expand Up @@ -363,6 +364,11 @@ private int getMaxTeiLimit(TrackedEntityQueryParams params) {
}

private boolean isLocalSearch(TrackedEntityQueryParams params, User user) {
// If the organization unit selection mode is set to CAPTURE, then it's a local search.
if (OrganisationUnitSelectionMode.CAPTURE == params.getOrgUnitMode()) {
return true;
}

Set<OrganisationUnit> localOrgUnits = user.getOrganisationUnits();

Set<OrganisationUnit> searchOrgUnits = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.hisp.dhis.webapi.controller.tracker.export.trackedentity;

import static org.hisp.dhis.common.OrganisationUnitSelectionMode.ACCESSIBLE;
import static org.hisp.dhis.common.OrganisationUnitSelectionMode.CAPTURE;
import static org.hisp.dhis.utils.Assertions.assertStartsWith;
import static org.hisp.dhis.web.WebClient.Accept;
import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertContainsAll;
Expand Down Expand Up @@ -115,6 +116,7 @@ class TrackedEntitiesExportControllerTest extends DhisControllerConvenienceTest
private User owner;

private User user;
private User userWithDifferentScopes;

private TrackedEntity softDeletedTrackedEntity;

Expand All @@ -141,12 +143,12 @@ void setUp() {
program = createProgram('A');
program.addOrganisationUnit(orgUnit);
program.getSharing().setOwner(owner);
program.getSharing().addUserAccess(userAccess());
program.getSharing().addUserAccess(userAccess(user));
manager.save(program, false);

programStage = createProgramStage('A', program);
programStage.getSharing().setOwner(owner);
programStage.getSharing().addUserAccess(userAccess());
programStage.getSharing().addUserAccess(userAccess(user));
manager.save(programStage, false);

trackedEntityType = trackedEntityTypeAccessible();
Expand Down Expand Up @@ -194,6 +196,31 @@ void shouldReturnEmptyListWhenGettingTrackedEntitiesWithNoMatchingParams() {
assertEquals(0, instances.size());
}

@Test
void shouldGetTrackedEntitiesDisregardingSearchScope() {
userWithDifferentScopes =
createUserWithId("testerWithDiffSearchScope", CodeGenerator.generateUid());
userWithDifferentScopes.addOrganisationUnit(orgUnit);
userWithDifferentScopes.setTeiSearchOrganisationUnits(Set.of(anotherOrgUnit));

program.getSharing().addUserAccess(userAccess(userWithDifferentScopes));
manager.save(program, false);

trackedEntityType.getSharing().setUserAccesses(Set.of(userAccess(userWithDifferentScopes)));
manager.save(trackedEntityType, false);

this.userService.updateUser(userWithDifferentScopes);

this.switchContextToUser(userWithDifferentScopes);
HttpResponse response =
GET(
"/tracker/trackedEntities?program={programId}&orgUnitMode={orgUnitMode}",
program.getUid(),
CAPTURE);

assertEquals(HttpStatus.OK, response.status());
}

@Test
void getTrackedEntityById() {
TrackedEntity te = trackedEntity();
Expand Down Expand Up @@ -1019,7 +1046,7 @@ private JsonEvent assertDefaultEventResponse(JsonEnrollment enrollment, Event ev
private TrackedEntityType trackedEntityTypeAccessible() {
TrackedEntityType type = trackedEntityType('A');
type.getSharing().setOwner(owner);
type.getSharing().addUserAccess(userAccess());
type.getSharing().addUserAccess(userAccess(user));
type.getSharing().setPublicAccess(AccessStringHelper.DEFAULT);
manager.save(type, false);
return type;
Expand Down Expand Up @@ -1075,7 +1102,7 @@ private Enrollment enroll(
trackedEntity, program, new Date(), new Date(), orgUnit);
}

private UserAccess userAccess() {
private UserAccess userAccess(User user) {
UserAccess a = new UserAccess();
a.setUser(user);
a.setAccess(AccessStringHelper.FULL);
Expand All @@ -1085,7 +1112,7 @@ private UserAccess userAccess() {
private RelationshipType relationshipTypeAccessible(
RelationshipEntity from, RelationshipEntity to) {
RelationshipType type = relationshipType(from, to);
type.getSharing().addUserAccess(userAccess());
type.getSharing().addUserAccess(userAccess(user));
manager.save(type, false);
return type;
}
Expand Down