Search in sources :

Example 71 with UserGroup

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

the class ValidationNotificationServiceTest method testNotifyUsersInHierarchyLimitsRecipients.

@Test
void testNotifyUsersInHierarchyLimitsRecipients() {
    // Complicated fixtures. Sorry to whomever has to read this...
    // Org units
    OrganisationUnit root = createOrganisationUnit('R'), lvlOneLeft = createOrganisationUnit('1'), lvlOneRight = createOrganisationUnit('2'), lvlTwoLeftLeft = createOrganisationUnit('3'), lvlTwoLeftRight = createOrganisationUnit('4');
    configureHierarchy(root, lvlOneLeft, lvlOneRight, lvlTwoLeftLeft, lvlTwoLeftRight);
    // Users
    User uA = createUser('A'), uB = createUser('B'), uC = createUser('C'), uD = createUser('D'), uE = createUser('E'), uF = createUser('F'), uG = createUser('G');
    root.addUser(uA);
    lvlOneLeft.addUser(uB);
    lvlOneLeft.addUser(uC);
    lvlOneRight.addUser(uD);
    lvlOneRight.addUser(uE);
    lvlTwoLeftLeft.addUser(uF);
    lvlTwoLeftRight.addUser(uG);
    // User groups
    UserGroup ugA = createUserGroup('A', Sets.newHashSet());
    ugA.addUser(uB);
    ugA.addUser(uC);
    ugA.addUser(uD);
    ugA.addUser(uE);
    ugA.addUser(uF);
    ugA.addUser(uG);
    UserGroup ugB = createUserGroup('B', Sets.newHashSet());
    ugB.addUser(uA);
    // Validation rule and template
    ValidationRule rule = createValidationRule('V', Operator.equal_to, createExpression2('A', "X"), createExpression2('B', "Y"), PeriodType.getPeriodTypeByName(QuarterlyPeriodType.NAME));
    ValidationNotificationTemplate template = createValidationNotificationTemplate("My fancy template");
    template.setNotifyUsersInHierarchyOnly(true);
    template.addValidationRule(rule);
    template.setRecipientUserGroups(Sets.newHashSet(ugA));
    // Create a validationResult that emanates from the middle of the left
    // branch
    final ValidationResult resultFromMiddleLeft = createValidationResult(lvlOneLeft, rule);
    // Perform tests
    // One
    subject.sendNotifications(Sets.newHashSet(resultFromMiddleLeft));
    assertEquals(1, sentMessages.size());
    Collection<User> rcpt = sentMessages.iterator().next().recipients;
    assertEquals(2, rcpt.size());
    assertTrue(rcpt.containsAll(Sets.newHashSet(uB, uC)));
    // Two
    sentMessages = new ArrayList<>();
    // Add the second group (with user F) to the recipients
    template.getRecipientUserGroups().add(ugB);
    subject.sendNotifications(Sets.newHashSet(resultFromMiddleLeft));
    assertEquals(1, sentMessages.size());
    rcpt = sentMessages.iterator().next().recipients;
    // We now expect user A, which is on the root org unit and in group B to
    // also be among the recipients
    assertEquals(3, rcpt.size());
    assertTrue(rcpt.containsAll(Sets.newHashSet(uA, uB, uC)));
    // Three
    sentMessages = new ArrayList<>();
    // Keep the hierarchy as is, but spread out the validation result from
    // the bottom left of the tree
    final ValidationResult resultFromBottomLeft = createValidationResult(lvlTwoLeftLeft, rule);
    subject.sendNotifications(Sets.newHashSet(resultFromBottomLeft));
    assertEquals(1, sentMessages.size());
    rcpt = sentMessages.iterator().next().recipients;
    assertEquals(4, rcpt.size());
    assertTrue(rcpt.containsAll(Sets.newHashSet(uA, uB, uC, uF)));
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) ValidationNotificationTemplate(org.hisp.dhis.validation.notification.ValidationNotificationTemplate) UserGroup(org.hisp.dhis.user.UserGroup) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 72 with UserGroup

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

the class ValidationNotificationServiceTest method testNotifyParentOfUserInGroup.

@Test
void testNotifyParentOfUserInGroup() {
    OrganisationUnit root = createOrganisationUnit('R'), lvlOneLeft = createOrganisationUnit('1'), lvlOneRight = createOrganisationUnit('2'), lvlTwoLeftLeft = createOrganisationUnit('3'), lvlTwoLeftRight = createOrganisationUnit('4');
    configureHierarchy(root, lvlOneLeft, lvlOneRight, lvlTwoLeftLeft, lvlTwoLeftRight);
    // Users
    User uB = createUser('B'), uC = createUser('C'), uD = createUser('D'), uE = createUser('E');
    UserGroup groupA = createUserGroup('A', Sets.newHashSet());
    groupA.addUser(uD);
    groupA.addUser(uE);
    lvlOneLeft.addUser(uB);
    lvlOneRight.addUser(uC);
    lvlTwoLeftLeft.addUser(uD);
    lvlTwoLeftRight.addUser(uE);
    ValidationRule rule = createValidationRule('V', Operator.equal_to, createExpression2('A', "X"), createExpression2('B', "Y"), PeriodType.getPeriodTypeByName(QuarterlyPeriodType.NAME));
    ValidationNotificationTemplate template = createValidationNotificationTemplate("My fancy template");
    template.setNotifyParentOrganisationUnitOnly(true);
    template.addValidationRule(rule);
    template.setRecipientUserGroups(Sets.newHashSet(groupA));
    final ValidationResult validationResult = createValidationResult(lvlOneLeft, rule);
    subject.sendNotifications(Sets.newHashSet(validationResult));
    assertEquals(1, sentMessages.size());
    Collection<User> rcpt = sentMessages.iterator().next().recipients;
    assertEquals(1, rcpt.size());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) ValidationNotificationTemplate(org.hisp.dhis.validation.notification.ValidationNotificationTemplate) UserGroup(org.hisp.dhis.user.UserGroup) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 73 with UserGroup

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

the class DhisConvenienceTest method createUserGroup.

public static UserGroup createUserGroup(char uniqueCharacter, Set<User> users) {
    UserGroup userGroup = new UserGroup();
    userGroup.setAutoFields();
    userGroup.setUid(BASE_USER_GROUP_UID + uniqueCharacter);
    userGroup.setCode("UserGroupCode" + uniqueCharacter);
    userGroup.setName("UserGroup" + uniqueCharacter);
    userGroup.setMembers(users);
    return userGroup;
}
Also used : UserGroup(org.hisp.dhis.user.UserGroup)

Example 74 with UserGroup

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

the class ConfigurationController method setSystemUpdateNotificationRecipients.

@PreAuthorize("hasRole('ALL') or hasRole('F_SYSTEM_SETTING')")
@PostMapping("/systemUpdateNotificationRecipients")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void setSystemUpdateNotificationRecipients(@RequestBody String uid) throws NotFoundException {
    uid = trim(uid);
    UserGroup group = identifiableObjectManager.get(UserGroup.class, uid);
    if (group == null) {
        throw new NotFoundException("User group", uid);
    }
    Configuration configuration = configurationService.getConfiguration();
    configuration.setSystemUpdateNotificationRecipients(group);
    configurationService.setConfiguration(configuration);
}
Also used : Configuration(org.hisp.dhis.configuration.Configuration) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) UserGroup(org.hisp.dhis.user.UserGroup) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 75 with UserGroup

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

the class SharingController method getSharingUserGroups.

private List<SharingUserGroupAccess> getSharingUserGroups(@RequestParam String key, int max) {
    List<SharingUserGroupAccess> sharingUserGroupAccesses = new ArrayList<>();
    List<UserGroup> userGroups = userGroupService.getUserGroupsBetweenByName(key, 0, max);
    for (UserGroup userGroup : userGroups) {
        SharingUserGroupAccess sharingUserGroupAccess = new SharingUserGroupAccess();
        sharingUserGroupAccess.setId(userGroup.getUid());
        sharingUserGroupAccess.setName(userGroup.getDisplayName());
        sharingUserGroupAccess.setDisplayName(userGroup.getDisplayName());
        sharingUserGroupAccesses.add(sharingUserGroupAccess);
    }
    return sharingUserGroupAccesses;
}
Also used : SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) ArrayList(java.util.ArrayList) UserGroup(org.hisp.dhis.user.UserGroup)

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