Search in sources :

Example 6 with UserCredentials

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

the class DeleteCurrentUserAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    message = "";
    User user = currentUserService.getCurrentUser();
    UserCredentials userCredentials = user.getUserCredentials();
    username = userCredentials.getUsername();
    String oldPasswordFromDB = userCredentials.getPassword();
    if (oldPassword == null) {
        return INPUT;
    }
    oldPassword = oldPassword.trim();
    if (oldPassword.length() == 0) {
        return INPUT;
    }
    if (!passwordManager.matches(oldPassword, oldPasswordFromDB)) {
        message = i18n.getString("wrong_password");
        return INPUT;
    } else {
        Collection<UserSetting> userSettings = userSettingService.getAllUserSettings();
        for (UserSetting userSetting : userSettings) {
            userSettingService.deleteUserSetting(userSetting);
        }
        if (userService.isLastSuperUser(userCredentials)) {
            message = i18n.getString("can_not_remove_last_super_user");
            return INPUT;
        } else {
            userService.deleteUser(user);
        }
        return "logout";
    }
}
Also used : User(org.hisp.dhis.user.User) UserCredentials(org.hisp.dhis.user.UserCredentials) UserSetting(org.hisp.dhis.user.UserSetting)

Example 7 with UserCredentials

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

the class ValidateUserAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    if (username != null) {
        UserCredentials match = userService.getUserCredentialsByUsername(username);
        if (match != null && (id == null || match.getId() != id)) {
            message = i18n.getString("username_in_use");
            return ERROR;
        }
    }
    if (openId != null) {
        UserCredentials match = userService.getUserCredentialsByOpenId(openId);
        if (match != null && (id == null || match.getId() != id)) {
            message = i18n.getString("openid_in_use");
            return ERROR;
        }
    }
    if (ldapId != null) {
        UserCredentials match = userService.getUserCredentialsByLdapId(ldapId);
        if (match != null && (id == null || match.getId() != id)) {
            message = i18n.getString("ldap_in_use");
            return ERROR;
        }
    }
    if (inviteUsername != null) {
        UserCredentials match = userService.getUserCredentialsByUsername(inviteUsername);
        if (match != null && (id == null || match.getId() != id)) {
            message = i18n.getString("username_in_use");
            return ERROR;
        }
    }
    if (rawPassword != null && !rawPassword.isEmpty()) {
        PasswordValidationResult result;
        CredentialsInfo credentialsInfo = new CredentialsInfo(username, rawPassword, email, true);
        if (id != null) {
            User user = userService.getUser(id);
            if (user != null) {
                credentialsInfo = new CredentialsInfo(user.getUsername(), rawPassword, user.getEmail(), false);
            }
        }
        result = passwordValidationService.validate(credentialsInfo);
        if (!result.isValid()) {
            message = i18n.getString(result.getI18ErrorMessage());
            return ERROR;
        }
    }
    message = i18n.getString("everything_is_ok");
    return SUCCESS;
}
Also used : User(org.hisp.dhis.user.User) PasswordValidationResult(org.hisp.dhis.user.PasswordValidationResult) CredentialsInfo(org.hisp.dhis.user.CredentialsInfo) UserCredentials(org.hisp.dhis.user.UserCredentials)

Example 8 with UserCredentials

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

the class RemoveUserAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    //TODO: Allow user with F_USER_DELETE_WITHIN_MANAGED_GROUP to delete a user within managed groups.
    User user = userService.getUser(id);
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null || user == null) {
        return ERROR;
    }
    if (!currentUser.getUserCredentials().canModifyUser(user.getUserCredentials())) {
        return ERROR;
    }
    boolean isCurrentUser = currentUser.equals(user);
    UserCredentials userCredentials = user.getUserCredentials();
    if (userService.isLastSuperUser(userCredentials)) {
        message = i18n.getString("can_not_remove_last_super_user");
        return ERROR;
    } else {
        userService.deleteUser(user);
    }
    if (isCurrentUser) {
        return "logout";
    }
    return SUCCESS;
}
Also used : User(org.hisp.dhis.user.User) UserCredentials(org.hisp.dhis.user.UserCredentials)

Example 9 with UserCredentials

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

the class UnregisteredSMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.UNREGISTERED_PARSER);
    UserGroup userGroup = smsCommand.getUserGroup();
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    if (userGroup != null) {
        Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
        if (users != null && users.size() >= 1) {
            String messageError = "This number is already registered for user: ";
            for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
                User user = iterator.next();
                messageError += user.getName();
                if (iterator.hasNext()) {
                    messageError += ", ";
                }
            }
            throw new SMSParserException(messageError);
        } else {
            Set<User> receivers = new HashSet<>(userGroup.getMembers());
            UserCredentials anonymousUser = userService.getUserCredentialsByUsername("anonymous");
            if (anonymousUser == null) {
                User user = new User();
                UserCredentials usercredential = new UserCredentials();
                usercredential.setUsername(USER_NAME);
                usercredential.setPassword(USER_NAME);
                usercredential.setUserInfo(user);
                user.setSurname(USER_NAME);
                user.setFirstName(USER_NAME);
                user.setUserCredentials(usercredential);
                userService.addUserCredentials(usercredential);
                userService.addUser(user);
                anonymousUser = userService.getUserCredentialsByUsername("anonymous");
            }
            // forward to user group by SMS, E-mail, DHIS conversation
            messageService.sendMessage(smsCommand.getName(), message, null, receivers, anonymousUser.getUserInfo(), MessageType.SYSTEM, false);
            // confirm SMS was received and forwarded completely
            Set<User> feedbackList = new HashSet<>();
            User sender = new User();
            sender.setPhoneNumber(senderPhoneNumber);
            feedbackList.add(sender);
            smsSender.sendMessage(smsCommand.getName(), smsCommand.getReceivedMessage(), null, null, feedbackList, true);
            sms.setStatus(SmsMessageStatus.PROCESSED);
            sms.setParsed(true);
            incomingSmsService.update(sms);
        }
    }
}
Also used : User(org.hisp.dhis.user.User) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) UserCredentials(org.hisp.dhis.user.UserCredentials) UserGroup(org.hisp.dhis.user.UserGroup) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with UserCredentials

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

the class AbstractEventService method convertProgramStageInstance.

private Event convertProgramStageInstance(ProgramStageInstance programStageInstance) {
    if (programStageInstance == null) {
        return null;
    }
    Event event = new Event();
    event.setEvent(programStageInstance.getUid());
    if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
        event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
    }
    event.setFollowup(programStageInstance.getProgramInstance().getFollowup());
    event.setEnrollmentStatus(EnrollmentStatus.fromProgramStatus(programStageInstance.getProgramInstance().getStatus()));
    event.setStatus(programStageInstance.getStatus());
    event.setEventDate(DateUtils.getIso8601NoTz(programStageInstance.getExecutionDate()));
    event.setDueDate(DateUtils.getIso8601NoTz(programStageInstance.getDueDate()));
    event.setStoredBy(programStageInstance.getStoredBy());
    event.setCompletedBy(programStageInstance.getCompletedBy());
    event.setCompletedDate(DateUtils.getIso8601NoTz(programStageInstance.getCompletedDate()));
    event.setCreated(DateUtils.getIso8601NoTz(programStageInstance.getCreated()));
    event.setCreatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getCreatedAtClient()));
    event.setLastUpdated(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdated()));
    event.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdatedAtClient()));
    UserCredentials userCredentials = currentUserService.getCurrentUser().getUserCredentials();
    OrganisationUnit ou = programStageInstance.getOrganisationUnit();
    if (ou != null) {
        if (!organisationUnitService.isInUserHierarchy(ou)) {
            if (!userCredentials.isSuper() && !userCredentials.isAuthorized("F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS")) {
                throw new IllegalQueryException("User has no access to organisation unit: " + ou.getUid());
            }
        }
        event.setOrgUnit(ou.getUid());
        event.setOrgUnitName(ou.getName());
    }
    Program program = programStageInstance.getProgramInstance().getProgram();
    if (!userCredentials.isSuper() && !userCredentials.getAllPrograms().contains(program)) {
        throw new IllegalQueryException("User has no access to program: " + program.getUid());
    }
    event.setProgram(program.getUid());
    event.setEnrollment(programStageInstance.getProgramInstance().getUid());
    event.setProgramStage(programStageInstance.getProgramStage().getUid());
    event.setAttributeOptionCombo(programStageInstance.getAttributeOptionCombo().getUid());
    event.setAttributeCategoryOptions(String.join(";", programStageInstance.getAttributeOptionCombo().getCategoryOptions().stream().map(DataElementCategoryOption::getUid).collect(Collectors.toList())));
    if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
        event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
    }
    if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
        Coordinate coordinate = null;
        if (programStageInstance.getLongitude() != null && programStageInstance.getLatitude() != null) {
            coordinate = new Coordinate(programStageInstance.getLongitude(), programStageInstance.getLatitude());
            try {
                List<Double> list = OBJECT_MAPPER.readValue(coordinate.getCoordinateString(), new TypeReference<List<Double>>() {
                });
                coordinate.setLongitude(list.get(0));
                coordinate.setLatitude(list.get(1));
            } catch (IOException ignored) {
            }
        }
        if (coordinate != null && coordinate.isValid()) {
            event.setCoordinate(coordinate);
        }
    }
    Collection<TrackedEntityDataValue> dataValues = dataValueService.getTrackedEntityDataValues(programStageInstance);
    for (TrackedEntityDataValue dataValue : dataValues) {
        DataValue value = new DataValue();
        value.setCreated(DateUtils.getIso8601NoTz(dataValue.getCreated()));
        value.setLastUpdated(DateUtils.getIso8601NoTz(dataValue.getLastUpdated()));
        value.setDataElement(dataValue.getDataElement().getUid());
        value.setValue(dataValue.getValue());
        value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
        value.setStoredBy(dataValue.getStoredBy());
        event.getDataValues().add(value);
    }
    List<TrackedEntityComment> comments = programStageInstance.getComments();
    for (TrackedEntityComment comment : comments) {
        Note note = new Note();
        note.setValue(comment.getCommentText());
        note.setStoredBy(comment.getCreator());
        if (comment.getCreatedDate() != null) {
            note.setStoredDate(DateUtils.getIso8601NoTz(comment.getCreatedDate()));
        }
        event.getNotes().add(note);
    }
    return event;
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) IOException(java.io.IOException) UserCredentials(org.hisp.dhis.user.UserCredentials) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

UserCredentials (org.hisp.dhis.user.UserCredentials)29 User (org.hisp.dhis.user.User)15 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)4 Query (org.hibernate.Query)4 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)4 CategoryOptionGroupSet (org.hisp.dhis.dataelement.CategoryOptionGroupSet)3 DataSet (org.hisp.dhis.dataset.DataSet)3 UserGroup (org.hisp.dhis.user.UserGroup)3 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)2 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)2 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)2 Program (org.hisp.dhis.program.Program)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 List (java.util.List)1