Search in sources :

Example 66 with UserGroup

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

the class MetadataImportServiceTest method testImportEmbeddedObjectWithSkipSharingIsFalse.

@Test
void testImportEmbeddedObjectWithSkipSharingIsFalse() throws IOException {
    User user = createUser('A');
    manager.save(user);
    User userA = manager.get(User.class, user.getUid());
    assertNotNull(userA);
    UserGroup userGroup = createUserGroup('A', Sets.newHashSet(userA));
    manager.save(userGroup);
    userGroup = manager.get(UserGroup.class, "ugabcdefghA");
    assertNotNull(userGroup);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/metadata_visualization_with_accesses.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setSkipSharing(false);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    dbmsManager.clearSession();
    Visualization visualization = manager.get(Visualization.class, "gyYXi0rXAIc");
    assertNotNull(visualization);
    assertEquals(1, visualization.getUserGroupAccesses().size());
    assertEquals(1, visualization.getUserAccesses().size());
    assertEquals(user.getUid(), visualization.getUserAccesses().iterator().next().getUser().getUid());
    assertEquals(userGroup.getUid(), visualization.getUserGroupAccesses().iterator().next().getUserGroup().getUid());
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/metadata_visualization_with_accesses_update.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setSkipSharing(false);
    dbmsManager.clearSession();
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    visualization = manager.get(Visualization.class, "gyYXi0rXAIc");
    assertNotNull(visualization);
    assertEquals(0, visualization.getUserGroupAccesses().size());
    assertEquals(0, visualization.getUserAccesses().size());
}
Also used : Visualization(org.hisp.dhis.visualization.Visualization) User(org.hisp.dhis.user.User) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) UserGroup(org.hisp.dhis.user.UserGroup) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 67 with UserGroup

use of org.hisp.dhis.user.UserGroup 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)

Example 68 with UserGroup

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

the class DefaultAclStoreTest method getAccessibleProgramsReturnsUserGroupOnes.

@Test
void getAccessibleProgramsReturnsUserGroupOnes() {
    // a private program
    Program programA = createProgram('A');
    programA.setPublicAccess("--------");
    programA.getSharing().setOwner(owner);
    manager.save(programA, false);
    // a private program readable by a user group of which the user IS part
    // of
    Program programB = createProgram('B');
    programB.setPublicAccess("--------");
    programB.getSharing().setOwner(owner);
    UserGroup g = createUserGroup('B', Set.of(owner, user));
    UserGroupAccess a = new UserGroupAccess();
    a.setUserGroup(g);
    a.setAccess("--r-----");
    programB.getSharing().addUserGroupAccess(a);
    manager.save(programB, false);
    List<Long> programIds = aclStore.getAccessiblePrograms(user.getUid(), Collections.singletonList(g.getUid()));
    assertContainsOnly(programIds, programB.getId());
}
Also used : Program(org.hisp.dhis.program.Program) UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) Test(org.junit.jupiter.api.Test)

Example 69 with UserGroup

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

the class DefaultMessageService method sendCompletenessMessage.

@Override
@Transactional
public long sendCompletenessMessage(CompleteDataSetRegistration registration) {
    DataSet dataSet = registration.getDataSet();
    if (dataSet == null) {
        return 0;
    }
    UserGroup userGroup = dataSet.getNotificationRecipients();
    User sender = currentUserService.getCurrentUser();
    // data set completed through sms
    if (sender == null) {
        return 0;
    }
    Set<User> recipients = new HashSet<>();
    if (userGroup != null) {
        recipients.addAll(new HashSet<>(userGroup.getMembers()));
    }
    if (dataSet.isNotifyCompletingUser()) {
        recipients.add(sender);
    }
    if (recipients.isEmpty()) {
        return 0;
    }
    String text = new VelocityManager().render(registration, COMPLETE_TEMPLATE);
    MessageConversation conversation = new MessageConversation(COMPLETE_SUBJECT, sender, MessageType.SYSTEM);
    conversation.addMessage(new Message(text, null, sender));
    for (User user : recipients) {
        conversation.addUserMessage(new UserMessage(user));
    }
    if (!conversation.getUserMessages().isEmpty()) {
        long id = saveMessageConversation(conversation);
        invokeMessageSenders(COMPLETE_SUBJECT, text, null, sender, new HashSet<>(conversation.getUsers()), false);
        return id;
    }
    return 0;
}
Also used : User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) VelocityManager(org.hisp.dhis.system.velocity.VelocityManager) UserGroup(org.hisp.dhis.user.UserGroup) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 70 with UserGroup

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

the class DefaultPushAnalysisService method runPushAnalysis.

@Override
public void runPushAnalysis(String uid, JobConfiguration jobId) {
    // ----------------------------------------------------------------------
    // Set up
    // ----------------------------------------------------------------------
    PushAnalysis pushAnalysis = pushAnalysisStore.getByUid(uid);
    Set<User> receivingUsers = new HashSet<>();
    notifier.clear(jobId);
    // ----------------------------------------------------------------------
    // Pre-check
    // ----------------------------------------------------------------------
    log(jobId, NotificationLevel.INFO, "Starting pre-check on PushAnalysis", false, null);
    if (pushAnalysis == null) {
        log(jobId, NotificationLevel.ERROR, "PushAnalysis with uid '" + uid + "' was not found. Terminating PushAnalysis", true, null);
        return;
    }
    if (pushAnalysis.getRecipientUserGroups().size() == 0) {
        log(jobId, NotificationLevel.ERROR, "PushAnalysis with uid '" + uid + "' has no userGroups assigned. Terminating PushAnalysis.", true, null);
        return;
    }
    if (pushAnalysis.getDashboard() == null) {
        log(jobId, NotificationLevel.ERROR, "PushAnalysis with uid '" + uid + "' has no dashboard assigned. Terminating PushAnalysis.", true, null);
        return;
    }
    if (dhisConfigurationProvider.getServerBaseUrl() == null) {
        log(jobId, NotificationLevel.ERROR, "Missing configuration '" + ConfigurationKey.SERVER_BASE_URL.getKey() + "'. Terminating PushAnalysis.", true, null);
        return;
    }
    log(jobId, NotificationLevel.INFO, "pre-check completed successfully", false, null);
    // ----------------------------------------------------------------------
    // Compose list of users that can receive PushAnalysis
    // ----------------------------------------------------------------------
    log(jobId, NotificationLevel.INFO, "Composing list of receiving users", false, null);
    for (UserGroup userGroup : pushAnalysis.getRecipientUserGroups()) {
        for (User user : userGroup.getMembers()) {
            if (!user.hasEmail()) {
                log(jobId, NotificationLevel.WARN, "Skipping user: User '" + user.getUsername() + "' is missing a valid email.", false, null);
                continue;
            }
            receivingUsers.add(user);
        }
    }
    log(jobId, NotificationLevel.INFO, "List composed. " + receivingUsers.size() + " eligible users found.", false, null);
    // ----------------------------------------------------------------------
    // Generating reports
    // ----------------------------------------------------------------------
    log(jobId, NotificationLevel.INFO, "Generating and sending reports", false, null);
    for (User user : receivingUsers) {
        try {
            String title = pushAnalysis.getTitle();
            String html = generateHtmlReport(pushAnalysis, user, jobId);
            // TODO: Better handling of messageStatus; Might require
            // refactoring of EmailMessageSender
            @SuppressWarnings("unused") Future<OutboundMessageResponse> status = messageSender.sendMessageAsync(title, html, "", null, Sets.newHashSet(user), true);
        } catch (Exception e) {
            log(jobId, NotificationLevel.ERROR, "Could not create or send report for PushAnalysis '" + pushAnalysis.getName() + "' and User '" + user.getUsername() + "': " + e.getMessage(), false, e);
        }
    }
}
Also used : User(org.hisp.dhis.user.User) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse) IOException(java.io.IOException) HashSet(java.util.HashSet) 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