Search in sources :

Example 36 with UserGroup

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

the class ObjectBundleServiceTest method testUpdateDataElementsUID.

@Test
void testUpdateDataElementsUID() throws IOException {
    defaultSetup();
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_update1.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setPreheatMode(PreheatMode.REFERENCE);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setObjects(metadata);
    Map<String, DataElement> dataElementMap = manager.getIdMap(DataElement.class, IdScheme.UID);
    UserGroup userGroup = manager.get(UserGroup.class, "ugabcdefghA");
    assertEquals(4, dataElementMap.size());
    assertNotNull(userGroup);
    ObjectBundle bundle = objectBundleService.create(params);
    objectBundleValidationService.validate(bundle);
    objectBundleService.commit(bundle);
    DataElement dataElementA = dataElementMap.get("deabcdefghA");
    DataElement dataElementB = dataElementMap.get("deabcdefghB");
    DataElement dataElementC = dataElementMap.get("deabcdefghC");
    DataElement dataElementD = dataElementMap.get("deabcdefghD");
    assertNotNull(dataElementA);
    assertNotNull(dataElementB);
    assertNotNull(dataElementC);
    assertNotNull(dataElementD);
    assertEquals("DEA", dataElementA.getName());
    assertEquals("DEB", dataElementB.getName());
    assertEquals("DEC", dataElementC.getName());
    assertEquals("DED", dataElementD.getName());
    assertEquals("DECA", dataElementA.getCode());
    assertEquals("DECB", dataElementB.getCode());
    assertEquals("DECC", dataElementC.getCode());
    assertEquals("DECD", dataElementD.getCode());
    assertEquals("DESA", dataElementA.getShortName());
    assertEquals("DESB", dataElementB.getShortName());
    assertEquals("DESC", dataElementC.getShortName());
    assertEquals("DESD", dataElementD.getShortName());
    assertEquals("DEDA", dataElementA.getDescription());
    assertEquals("DEDB", dataElementB.getDescription());
    assertEquals("DEDC", dataElementC.getDescription());
    assertEquals("DEDD", dataElementD.getDescription());
    assertEquals(1, dataElementA.getUserGroupAccesses().size());
    assertEquals(0, dataElementB.getUserGroupAccesses().size());
    assertEquals(1, dataElementC.getUserGroupAccesses().size());
    assertEquals(0, dataElementD.getUserGroupAccesses().size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 37 with UserGroup

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

the class MessageConversationController method getUsersToMessageConversation.

private Set<User> getUsersToMessageConversation(MessageConversation messageConversation, Set<User> users) throws WebMessageException {
    Set<User> usersToMessageConversation = Sets.newHashSet();
    for (OrganisationUnit ou : messageConversation.getOrganisationUnits()) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou.getUid());
        if (organisationUnit == null) {
            throw new WebMessageException(conflict("Organisation Unit does not exist: " + ou.getUid()));
        }
        usersToMessageConversation.addAll(organisationUnit.getUsers());
    }
    for (User u : users) {
        User user = userService.getUser(u.getUid());
        if (user == null) {
            throw new WebMessageException(conflict("User does not exist: " + u.getUid()));
        }
        usersToMessageConversation.add(user);
    }
    for (UserGroup ug : messageConversation.getUserGroups()) {
        UserGroup userGroup = userGroupService.getUserGroup(ug.getUid());
        if (userGroup == null) {
            throw new WebMessageException(notFound("User Group does not exist: " + ug.getUid()));
        }
        usersToMessageConversation.addAll(userGroup.getMembers());
    }
    return usersToMessageConversation;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UserGroup(org.hisp.dhis.user.UserGroup)

Example 38 with UserGroup

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

the class UserControllerTest method setUp.

@BeforeEach
public void setUp() {
    userGroup1 = new UserGroup();
    userGroup1.setUid("abc1");
    userGroup2 = new UserGroup();
    userGroup2.setUid("abc2");
    currentUser = new User();
    currentUser.setId(1000);
    currentUser.setUid("def1");
    user = new User();
    user.setId(1001);
    user.setUid("def2");
    parsedUser = new User();
    parsedUser.setUid("def2");
    parsedUser.setGroups(new HashSet<>(Arrays.asList(userGroup1, userGroup2)));
}
Also used : User(org.hisp.dhis.user.User) UserGroup(org.hisp.dhis.user.UserGroup) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 39 with UserGroup

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

the class MetadataImportServiceTest method testUpdateImmutableCreatedByField.

@Test
void testUpdateImmutableCreatedByField() throws IOException {
    User userA = createUser('A', Lists.newArrayList("ALL"));
    userService.addUser(userA);
    injectSecurityContext(userA);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setUser(userA);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    UserGroup userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals(userA.getUid(), userGroup.getCreatedBy().getUid());
    User userB = createUser("B", "ALL");
    userB.setUid("userabcdefB");
    userService.addUser(userB);
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups_update.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setUser(userA);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals("TA user group updated", userGroup.getName());
    assertEquals(userA.getUid(), userGroup.getCreatedBy().getUid());
}
Also used : User(org.hisp.dhis.user.User) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 40 with UserGroup

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

the class MetadataImportServiceTest method testUpdateUserGroupWithoutCreatedUserProperty.

@Test
void testUpdateUserGroupWithoutCreatedUserProperty() throws IOException {
    User userA = createUser('A', Lists.newArrayList("ALL"));
    userService.addUser(userA);
    injectSecurityContext(userA);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setUser(userA);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    UserGroup userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals(userA.getUid(), userGroup.getSharing().getOwner());
    User userB = createUser("B", "ALL");
    userService.addUser(userB);
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups_update.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setUser(userB);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals("TA user group updated", userGroup.getName());
    assertEquals(userA.getUid(), userGroup.getSharing().getOwner());
}
Also used : User(org.hisp.dhis.user.User) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

UserGroup (org.hisp.dhis.user.UserGroup)76 User (org.hisp.dhis.user.User)50 Test (org.junit.jupiter.api.Test)32 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)23 UserGroupAccess (org.hisp.dhis.user.sharing.UserGroupAccess)22 DataElement (org.hisp.dhis.dataelement.DataElement)17 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)12 HashSet (java.util.HashSet)11 List (java.util.List)11 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)10 UserAccess (org.hisp.dhis.user.sharing.UserAccess)10 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Program (org.hisp.dhis.program.Program)6 ArrayList (java.util.ArrayList)5 CategoryOption (org.hisp.dhis.category.CategoryOption)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)4 UserGroupAccess (org.hisp.dhis.user.UserGroupAccess)4