Search in sources :

Example 1 with UserSetting

use of org.hisp.dhis.user.UserSetting 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 2 with UserSetting

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

the class UserController method replicateUser.

@SuppressWarnings("unchecked")
@PreAuthorize("hasRole('ALL') or hasRole('F_REPLICATE_USER')")
@PostMapping("/{uid}/replica")
@ResponseBody
public WebMessage replicateUser(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
    User existingUser = userService.getUser(uid);
    if (existingUser == null) {
        return conflict("User not found: " + uid);
    }
    User currentUser = currentUserService.getCurrentUser();
    validateCreateUser(existingUser, currentUser);
    Map<String, String> auth = renderService.fromJson(request.getInputStream(), Map.class);
    String username = StringUtils.trimToNull(auth != null ? auth.get(KEY_USERNAME) : null);
    String password = StringUtils.trimToNull(auth != null ? auth.get(KEY_PASSWORD) : null);
    if (auth == null || username == null) {
        return conflict("Username must be specified");
    }
    if (userService.getUserByUsername(username) != null) {
        return conflict("Username already taken: " + username);
    }
    if (password == null) {
        return conflict("Password must be specified");
    }
    if (!ValidationUtils.passwordIsValid(password)) {
        return conflict("Password must have at least 8 characters, one digit, one uppercase");
    }
    User userReplica = new User();
    mergeService.merge(new MergeParams<>(existingUser, userReplica).setMergeMode(MergeMode.MERGE));
    copyAttributeValues(userReplica);
    userReplica.setId(0);
    userReplica.setUuid(UUID.randomUUID());
    userReplica.setUid(CodeGenerator.generateUid());
    userReplica.setCode(null);
    userReplica.setCreated(new Date());
    userReplica.setLdapId(null);
    userReplica.setOpenId(null);
    userReplica.setUsername(username);
    userService.encodeAndSetPassword(userReplica, password);
    userService.addUser(userReplica);
    userGroupService.addUserToGroups(userReplica, getUids(existingUser.getGroups()), currentUser);
    // ---------------------------------------------------------------------
    // Replicate user settings
    // ---------------------------------------------------------------------
    List<UserSetting> settings = userSettingService.getUserSettings(existingUser);
    for (UserSetting setting : settings) {
        Optional<UserSettingKey> key = UserSettingKey.getByName(setting.getName());
        key.ifPresent(userSettingKey -> userSettingService.saveUserSetting(userSettingKey, setting.getValue(), userReplica));
    }
    return created("User replica created").setLocation(UserSchemaDescriptor.API_ENDPOINT + "/" + userReplica.getUid());
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) MergeParams(org.hisp.dhis.schema.MergeParams) UserSettingKey(org.hisp.dhis.user.UserSettingKey) Date(java.util.Date) UserSetting(org.hisp.dhis.user.UserSetting) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with UserSetting

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

the class HibernateUserSettingStore method getAllUserSettings.

@Override
@SuppressWarnings("unchecked")
public List<UserSetting> getAllUserSettings(User user) {
    Session session = sessionFactory.getCurrentSession();
    Query<UserSetting> query = session.createQuery("from UserSetting us where us.user = :user");
    query.setParameter("user", user);
    query.setCacheable(CACHEABLE);
    return query.list();
}
Also used : Session(org.hibernate.Session) UserSetting(org.hisp.dhis.user.UserSetting)

Example 4 with UserSetting

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

the class HibernateUserSettingStore method getUserSetting.

@Override
@SuppressWarnings("unchecked")
public UserSetting getUserSetting(User user, String name) {
    Session session = sessionFactory.getCurrentSession();
    Query<UserSetting> query = session.createQuery("from UserSetting us where us.user = :user and us.name = :name");
    query.setParameter("user", user);
    query.setParameter("name", name);
    query.setCacheable(CACHEABLE);
    return query.uniqueResult();
}
Also used : Session(org.hibernate.Session) UserSetting(org.hisp.dhis.user.UserSetting)

Aggregations

UserSetting (org.hisp.dhis.user.UserSetting)4 Session (org.hibernate.Session)2 User (org.hisp.dhis.user.User)2 Date (java.util.Date)1 MergeParams (org.hisp.dhis.schema.MergeParams)1 CurrentUser (org.hisp.dhis.user.CurrentUser)1 UserCredentials (org.hisp.dhis.user.UserCredentials)1 UserSettingKey (org.hisp.dhis.user.UserSettingKey)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1