Search in sources :

Example 6 with UserAccess

use of org.hisp.dhis.user.UserAccess in project dhis2-core by dhis2.

the class ProgramSupplierAclIntegrationTest method verifyUserHasWriteAccessToProgramStageForUserAccess.

@Test
void verifyUserHasWriteAccessToProgramStageForUserAccess() {
    // Given
    final User user = createUser("user2");
    final ProgramStage programStage = createProgramStage('B', 1);
    UserAccess userAccess = new UserAccess(user, AccessStringHelper.DATA_READ_WRITE);
    programStage.setUserAccesses(singleton(userAccess));
    manager.save(programStage, false);
    final Program program = createProgram('A');
    program.setProgramStages(singleton(programStage));
    program.setPublicAccess(AccessStringHelper.DEFAULT);
    manager.save(program, false);
    manager.flush();
    // When
    final Map<String, Program> programs = programSupplier.get(getDefaultImportOptions(), singletonList(event));
    // Then
    assertThat(programs.keySet(), hasSize(1));
    assertTrue(aclService.canDataWrite(user, getProgramStage(programs.get(program.getUid()))));
}
Also used : User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) UserAccess(org.hisp.dhis.user.UserAccess) ProgramStage(org.hisp.dhis.program.ProgramStage) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 7 with UserAccess

use of org.hisp.dhis.user.UserAccess in project dhis2-core by dhis2.

the class TrackedEntityInstanceAttributesAggregateTest method populatePrerequisites.

private void populatePrerequisites(boolean removeOwnership) {
    doInTransaction(() -> {
        ProgramStage programStageA = createProgramStage(programB, true);
        ProgramStage programStageB = createProgramStage(programB, true);
        ProgramStage programStageA1 = createProgramStage(programA, true);
        ProgramStage programStageA2 = createProgramStage(programA, true);
        // Create 5 Tracked Entity Attributes (named A .. E)
        IntStream.range(A, F).mapToObj(i -> Character.toString((char) i)).forEach(c -> attributeService.addTrackedEntityAttribute(createTrackedEntityAttribute(c.charAt(0), ValueType.TEXT)));
        // Transform the Tracked Entity Attributes into a List of
        // TrackedEntityTypeAttribute
        List<TrackedEntityTypeAttribute> teatList = IntStream.range(A, C).mapToObj(i -> Character.toString((char) i)).map(s -> new TrackedEntityTypeAttribute(trackedEntityTypeA, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
        // Assign 2 (A, B) TrackedEntityTypeAttribute to Tracked Entity Type
        // A
        trackedEntityTypeA.getTrackedEntityTypeAttributes().addAll(teatList);
        // Make TET public
        trackedEntityTypeA.setPublicAccess(AccessStringHelper.FULL);
        manager.update(trackedEntityTypeA);
        programB = createProgram('B', new HashSet<>(), organisationUnitA);
        programB.setProgramType(ProgramType.WITH_REGISTRATION);
        programB.setCategoryCombo(categoryComboA);
        programB.setAccessLevel(AccessLevel.PROTECTED);
        programB.setUid(CodeGenerator.generateUid());
        programB.setCode(RandomStringUtils.randomAlphanumeric(10));
        Set<UserAccess> programBUserAccess = new HashSet<>();
        programBUserAccess.add(new UserAccess(currentUserService.getCurrentUser(), AccessStringHelper.FULL));
        programB.setUserAccesses(programBUserAccess);
        programB.setProgramStages(Stream.of(programStageA, programStageB).collect(Collectors.toCollection(HashSet::new)));
        programService.addProgram(programB);
        programA = createProgram('A', new HashSet<>(), organisationUnitA);
        programA.setProgramType(ProgramType.WITH_REGISTRATION);
        programA.setCategoryCombo(categoryComboA);
        programA.setUid(CodeGenerator.generateUid());
        programA.setCode(RandomStringUtils.randomAlphanumeric(10));
        programA.setProgramStages(Stream.of(programStageA1, programStageA2).collect(Collectors.toCollection(HashSet::new)));
        programService.addProgram(programA);
        // Because access strings isnt getting persisted with programService
        // methods for some reason
        programB.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programB);
        programA.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programA);
        programStageA.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageA);
        programStageB.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageB);
        programStageA1.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageA1);
        programStageA2.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageA2);
        // Assign ProgramTrackedEntityAttribute C to program A
        List<ProgramTrackedEntityAttribute> pteaListA = IntStream.range(C, D).mapToObj(i -> Character.toString((char) i)).map(s -> new ProgramTrackedEntityAttribute(programA, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
        // Assign ProgramTrackedEntityAttribute D, E to program B
        List<ProgramTrackedEntityAttribute> pteaListB = IntStream.range(D, F).mapToObj(i -> Character.toString((char) i)).map(s -> new ProgramTrackedEntityAttribute(programB, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
        programA.getProgramAttributes().addAll(pteaListA);
        programB.getProgramAttributes().addAll(pteaListB);
        manager.update(programA);
        manager.update(programB);
        // Create a TEI associated to program B
        final org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = persistTrackedEntityInstance(ImmutableMap.of("program", programB));
        ProgramInstance piB = new ProgramInstance(programB, trackedEntityInstance, organisationUnitA);
        piB.setEnrollmentDate(new Date());
        manager.save(piB);
        ProgramInstance piA = new ProgramInstance(programA, trackedEntityInstance, organisationUnitA);
        piA.setEnrollmentDate(new Date());
        manager.save(piA);
        if (removeOwnership) {
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programB, organisationUnitB, true, true);
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programA, organisationUnitA, true, true);
        } else {
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programB, organisationUnitA, true, true);
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programA, organisationUnitA, true, true);
        }
        // Assign Attribute A,B,E to Tracked Entity Instance
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeA"), trackedEntityInstance, "A"));
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeB"), trackedEntityInstance, "B"));
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeC"), trackedEntityInstance, "C"));
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeE"), trackedEntityInstance, "E"));
    });
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Program(org.hisp.dhis.program.Program) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) HashSet(java.util.HashSet) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ImmutableMap(org.testcontainers.shaded.com.google.common.collect.ImmutableMap) Matchers.hasSize(org.hamcrest.Matchers.hasSize) User(org.hisp.dhis.user.User) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ProgramStage(org.hisp.dhis.program.ProgramStage) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) List(java.util.List) Stream(java.util.stream.Stream) UserAccess(org.hisp.dhis.user.UserAccess) ProgramType(org.hisp.dhis.program.ProgramType) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Matchers.is(org.hamcrest.Matchers.is) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) CodeGenerator(org.hisp.dhis.common.CodeGenerator) TrackerTest(org.hisp.dhis.dxf2.TrackerTest) ProgramService(org.hisp.dhis.program.ProgramService) AccessLevel(org.hisp.dhis.common.AccessLevel) UserAccess(org.hisp.dhis.user.UserAccess) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Date(java.util.Date) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ProgramStage(org.hisp.dhis.program.ProgramStage) HashSet(java.util.HashSet)

Example 8 with UserAccess

use of org.hisp.dhis.user.UserAccess in project dhis2-core by dhis2.

the class ProgramSupplierAclIntegrationTest method verifyUserHasWriteAccessToProgramForUserAccess.

@Test
void verifyUserHasWriteAccessToProgramForUserAccess() {
    // Given
    final User user = createUser("A");
    final Program program = createProgram('A');
    UserAccess userAccess = new UserAccess(user, AccessStringHelper.DATA_READ_WRITE);
    Set<UserAccess> userAccesses = new HashSet<>();
    userAccesses.add(userAccess);
    program.setUserAccesses(userAccesses);
    manager.save(program, false);
    manager.flush();
    // When
    final Map<String, Program> programs = programSupplier.get(getDefaultImportOptions(), singletonList(event));
    // Then
    assertThat(programs.keySet(), hasSize(1));
    assertTrue(aclService.canDataWrite(user, programs.get(program.getUid())));
}
Also used : User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) UserAccess(org.hisp.dhis.user.UserAccess) HashSet(java.util.HashSet) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 9 with UserAccess

use of org.hisp.dhis.user.UserAccess in project dhis2-core by dhis2.

the class MetadataExportServiceTest method testSkipSharing.

// @Test
// TODO Fix this
public void testSkipSharing() {
    MetadataExportParams params = new MetadataExportParams();
    params.setSkipSharing(true);
    params.setClasses(Sets.newHashSet(DataElement.class));
    User user = createUser('A');
    UserGroup group = createUserGroup('A', Sets.newHashSet(user));
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    DataElement de4 = createDataElement('D');
    DataElement de5 = createDataElement('E');
    de1.setUserAccesses(Sets.newHashSet(new UserAccess(user, "rwrwrwrw")));
    de2.setPublicAccess("rwrwrwrw");
    de3.setCreatedBy(user);
    de4.setUserGroupAccesses(Sets.newHashSet(new UserGroupAccess(group, "rwrwrwrw")));
    de5.setExternalAccess(true);
    manager.save(user);
    manager.save(group);
    manager.save(de1);
    manager.save(de2);
    manager.save(de3);
    manager.save(de4);
    manager.save(de5);
    Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = metadataExportService.getMetadata(params);
    assertEquals(5, metadata.get(DataElement.class).size());
    metadata.get(DataElement.class).stream().forEach(element -> checkSharingFields(element));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) UserAccess(org.hisp.dhis.user.UserAccess) List(java.util.List) UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.UserGroupAccess) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

UserAccess (org.hisp.dhis.user.UserAccess)9 User (org.hisp.dhis.user.User)8 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 Program (org.hisp.dhis.program.Program)4 Test (org.junit.jupiter.api.Test)4 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)3 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)3 UserGroupAccess (org.hisp.dhis.user.UserGroupAccess)3 HashSet (java.util.HashSet)2 List (java.util.List)2 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)2 ProgramStage (org.hisp.dhis.program.ProgramStage)2 Sharing (org.hisp.dhis.webapi.webdomain.sharing.Sharing)2 SharingUserAccess (org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess)2 SharingUserGroupAccess (org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Sets (com.google.common.collect.Sets)1 Collection (java.util.Collection)1 Date (java.util.Date)1