Search in sources :

Example 46 with UserGroup

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

the class GetUserGroupListAction method populateMemberShipMap.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private Map<UserGroup, Boolean> populateMemberShipMap(List<UserGroup> userGroups) {
    User currentUser = currentUserService.getCurrentUser();
    Map<UserGroup, Boolean> map = new HashMap<>();
    if (currentUser != null && currentUser.getGroups() != null) {
        Set<UserGroup> members = currentUser.getGroups();
        for (UserGroup ug : userGroups) {
            map.put(ug, members.contains(ug));
        }
    }
    return map;
}
Also used : User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) UserGroup(org.hisp.dhis.user.UserGroup)

Example 47 with UserGroup

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

the class UpdateUserGroupAction method execute.

// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    //TODO managed groups access control
    UserGroup userGroup = userGroupService.getUserGroup(userGroupId);
    Set<User> users = new HashSet<>();
    for (String uid : usersSelected) {
        users.add(userService.getUser(uid));
    }
    userGroup.setName(StringUtils.trimToNull(name));
    userGroup.updateUsers(users);
    if (jsonAttributeValues != null) {
        attributeService.updateAttributeValues(userGroup, jsonAttributeValues);
    }
    Set<UserGroup> managedGroups = new HashSet<>();
    for (String uid : userGroupsSelected) {
        managedGroups.add(userGroupService.getUserGroup(uid));
    }
    userGroup.updateManagedGroups(managedGroups);
    userGroupService.updateUserGroup(userGroup);
    hibernateCacheManager.clearCache();
    return SUCCESS;
}
Also used : User(org.hisp.dhis.user.User) UserGroup(org.hisp.dhis.user.UserGroup) HashSet(java.util.HashSet)

Example 48 with UserGroup

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

the class SendMessageAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
    String metaData = MessageService.META_USER_AGENT + ServletActionContext.getRequest().getHeader(ContextUtils.HEADER_USER_AGENT);
    Set<User> users = new HashSet<>();
    if (!ignoreTree) {
        for (OrganisationUnit unit : selectionTreeManager.getReloadedSelectedOrganisationUnits()) {
            users.addAll(unit.getUsers());
        }
    }
    String[] recipientsArray = recipients.split(",");
    for (String recipient : recipientsArray) {
        if (recipient.startsWith(PREFIX_USER)) {
            User user = userService.getUser(recipient.substring(2));
            if (user != null) {
                users.add(user);
            }
        } else if (recipient.startsWith(PREFIX_USERGROUP)) {
            UserGroup userGroup = userGroupService.getUserGroup(recipient.substring(3));
            if (userGroup != null) {
                users.addAll(userGroup.getMembers());
            }
        }
    }
    messageService.sendPrivateMessage(subject, text, metaData, users);
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) HashSet(java.util.HashSet) UserGroup(org.hisp.dhis.user.UserGroup)

Example 49 with UserGroup

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

the class ProcessingSendQuickSMSAction method execute.

// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
public String execute() throws Exception {
    gatewayId = gatewayAdminService.getDefaultGateway().getName();
    if (gatewayId == null || gatewayId.trim().length() == 0) {
        message = i18n.getString("please_select_a_gateway_type_to_send_sms");
        return ERROR;
    }
    if (text == null || text.trim().length() == 0) {
        message = i18n.getString("no_message");
        return ERROR;
    }
    User currentUser = currentUserService.getCurrentUser();
    List<User> recipientsList = new ArrayList<>();
    if ("phone".equals(sendTarget)) {
        ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        recipients = mapper.readValue(recipients.iterator().next(), Set.class);
        for (String each : recipients) {
            if (!each.startsWith("+")) {
                each = "+" + each;
            }
            User user = new User();
            user.setPhoneNumber(each);
            recipientsList.add(user);
        }
    // message = messageSender.sendMessage( smsSubject, smsMessage,
    // currentUser, true, recipients, gatewayId );
    } else if ("userGroup".equals(sendTarget)) {
        UserGroup group = userGroupService.getUserGroup(userGroup);
        if (group == null) {
            message = i18n.getString("selected_user_group_is_unavailable");
            return ERROR;
        }
        if (group.getMembers() == null || group.getMembers().isEmpty()) {
            message = i18n.getString("selected_user_group_has_no_member");
            return ERROR;
        }
        recipientsList = new ArrayList<>(group.getMembers());
    } else if (sendTarget.equals("user")) {
        Collection<OrganisationUnit> units = selectionTreeManager.getReloadedSelectedOrganisationUnits();
        if (units != null && !units.isEmpty()) {
            for (OrganisationUnit unit : units) {
                recipientsList.addAll(unit.getUsers());
            }
            if (recipientsList.isEmpty()) {
                message = i18n.getString("there_is_no_user_assigned_to_selected_units");
                return ERROR;
            }
        // message = messageSender.sendMessage( smsSubject, smsMessage,
        // currentUser, false, users, gatewayId );
        }
    } else if (sendTarget.equals("unit")) {
        for (OrganisationUnit unit : selectionTreeManager.getSelectedOrganisationUnits()) {
            if (unit.getPhoneNumber() != null && !unit.getPhoneNumber().isEmpty()) {
                User user = new User();
                user.setPhoneNumber(unit.getPhoneNumber());
                recipientsList.add(user);
            }
        }
        if (recipientsList.isEmpty()) {
            message = i18n.getString("selected_units_have_no_phone_number");
            return ERROR;
        }
    }
    TaskId taskId = new TaskId(TaskCategory.SENDING_SMS, currentUser);
    notifier.clear(taskId);
    sendSmsTask.setTaskId(taskId);
    sendSmsTask.setCurrentUser(currentUser);
    sendSmsTask.setRecipientsList(recipientsList);
    sendSmsTask.setSmsSubject(smsSubject);
    sendSmsTask.setText(text);
    scheduler.executeTask(sendSmsTask);
    if (message != null && !message.equals("success")) {
        message = i18n.getString(message);
        return ERROR;
    }
    if (message == null) {
        message = "An inter error occurs, please contact your administration";
        return ERROR;
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Set(java.util.Set) HashSet(java.util.HashSet) TaskId(org.hisp.dhis.scheduling.TaskId) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UserGroup(org.hisp.dhis.user.UserGroup)

Example 50 with UserGroup

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

the class UnregisteredSMSListener method postProcess.

@Override
protected void postProcess(IncomingSms sms, SMSCommand smsCommand, Map<String, String> parsedMessage) {
    UserGroup userGroup = smsCommand.getUserGroup();
    String userName = sms.getOriginator();
    if (userGroup != null) {
        User anonymousUser = userService.getUserByUsername(userName);
        if (anonymousUser == null) {
            User user = new User();
            user.setSurname(userName);
            user.setFirstName("");
            user.setAutoFields();
            userService.addUser(user);
            anonymousUser = userService.getUserByUsername(userName);
        }
        messageService.sendMessage(new MessageConversationParams.Builder(userGroup.getMembers(), anonymousUser, smsCommand.getName(), sms.getText(), MessageType.SYSTEM, null).build());
        sendFeedback(smsCommand.getReceivedMessage(), sms.getOriginator(), INFO);
        update(sms, SmsMessageStatus.PROCESSED, true);
    }
}
Also used : User(org.hisp.dhis.user.User) 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