Skip to content

Commit

Permalink
fix: Validate transfer program/orgUnit combo [DHIS2-17880][2.40]
Browse files Browse the repository at this point in the history
  • Loading branch information
muilpp committed Aug 15, 2024
1 parent 924e1b4 commit 21e93e3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public List<ProgramDataElementDimensionItem> getGeneratedProgramDataElements(Str

@Override
public boolean hasOrgUnit(Program program, OrganisationUnit organisationUnit) {
return this.programStore.hasOrgUnit(program, organisationUnit);
return programStore.hasOrgUnit(program, organisationUnit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramOwnershipHistory;
import org.hisp.dhis.program.ProgramOwnershipHistoryService;
import org.hisp.dhis.program.ProgramService;
import org.hisp.dhis.program.ProgramTempOwner;
import org.hisp.dhis.program.ProgramTempOwnerService;
import org.hisp.dhis.program.ProgramTempOwnershipAudit;
Expand Down Expand Up @@ -81,6 +82,8 @@ public class DefaultTrackerOwnershipManager implements TrackerOwnershipManager {

private final OrganisationUnitService organisationUnitService;

private final ProgramService programService;

private final TrackedEntityInstanceService trackedEntityInstanceService;

private final DhisConfigurationProvider config;
Expand All @@ -94,6 +97,7 @@ public DefaultTrackerOwnershipManager(
ProgramOwnershipHistoryService programOwnershipHistoryService,
TrackedEntityInstanceService trackedEntityInstanceService,
OrganisationUnitService organisationUnitService,
ProgramService programService,
DhisConfigurationProvider config,
Environment env) {
checkNotNull(currentUserService);
Expand All @@ -113,6 +117,7 @@ public DefaultTrackerOwnershipManager(
this.programTempOwnerService = programTempOwnerService;
this.organisationUnitService = organisationUnitService;
this.trackedEntityInstanceService = trackedEntityInstanceService;
this.programService = programService;
this.config = config;
this.ownerCache = cacheProvider.createProgramOwnerCache();
this.tempOwnerCache = cacheProvider.createProgramTempOwnerCache();
Expand Down Expand Up @@ -140,8 +145,9 @@ public void transferOwnership(
return;
}

if (hasAccess(currentUserService.getCurrentUser(), entityInstance, program)
|| skipAccessValidation) {
if ((hasAccess(currentUserService.getCurrentUser(), entityInstance, program)
|| skipAccessValidation)
&& programService.hasOrgUnit(program, orgUnit)) {
TrackedEntityProgramOwner teProgramOwner =
trackedEntityProgramOwnerService.getTrackedEntityProgramOwner(
entityInstance.getId(), program.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.hisp.dhis.utils.Assertions.assertIsEmpty;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Instant;
Expand All @@ -61,6 +62,7 @@
import org.hisp.dhis.user.sharing.UserAccess;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;

/**
* @author Ameen Mohamed <ameen@dhis2.org>
Expand Down Expand Up @@ -141,6 +143,7 @@ protected void setUpTest() throws Exception {
programA = createProgram('A');
programA.setAccessLevel(AccessLevel.PROTECTED);
programA.setTrackedEntityType(trackedEntityType);
programA.setOrganisationUnits(Set.of(organisationUnitA, organisationUnitB));
programService.addProgram(programA);
UserAccess userAccess = new UserAccess(userA.getUid(), FULL);
programA.setSharing(new Sharing(FULL, userAccess));
Expand Down Expand Up @@ -293,6 +296,19 @@ void shouldFindTrackedEntityWhenTransferredToInaccessibleOrgUnitIfSuperUser() {
.collect(Collectors.toList()));
}

@Test
void shouldNotTransferOwnershipWhenOrgUnitNotAssociatedToProgram() {
OrganisationUnit notAssociatedOrgUnit = createOrganisationUnit('C');
organisationUnitService.addOrganisationUnit(notAssociatedOrgUnit);
Exception exception =
assertThrows(
AccessDeniedException.class,
() -> transferOwnership(entityInstanceA1, programA, notAssociatedOrgUnit));
assertEquals(
"User does not have access to change ownership for the entity-program combination",
exception.getMessage());
}

@Test
void shouldFindTrackedEntityWhenProgramSuppliedAndUserIsOwner() {
assignOwnership(entityInstanceA1, programA, organisationUnitA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ void setUp() {
HttpStatus.CREATED,
POST(
"/programs/",
"{'name':'P1', 'shortName':'P1', 'programType':'WITHOUT_REGISTRATION'}"));
"{'name':'P1', 'shortName':'P1', 'programType':'WITHOUT_REGISTRATION', 'organisationUnits': [{'id':'"
+ ouId
+ "'}]}"));
}

@Test
Expand Down

0 comments on commit 21e93e3

Please sign in to comment.