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());
}
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));
}
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());
}
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;
}
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);
}
}
}
Aggregations