Search in sources :

Example 11 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class UserService method saveOrUpdateSettings.

/**
 * Save or update settings.
 *
 * @param userInfo the user info
 * @return the user info
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo saveOrUpdateSettings(UserInfo userInfo) {
    if (ValidatorUtil.isNull(userInfo.getUserId())) {
        LOGGER.warn("Validation failed for user (id: " + userInfo.getUserId() + "). Error: " + messageUtil.getAttributeInvalid("user_id", userInfo.getUserId() + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userInfo.getUserId() + ""));
    }
    UserSettingEntity userSettingEntity = userSettingRepository.findOneByUserId(userInfo.getUserId());
    if (userSettingEntity == null) {
        userSettingEntity = new UserSettingEntity();
        userSettingEntity.setUserId(userInfo.getUserId());
    }
    userSettingEntity.setSettings(IConstants.Settings.TOPOLOGY_SETTING);
    userSettingEntity.setData(userInfo.getData());
    userSettingEntity = userSettingRepository.save(userSettingEntity);
    // activityLogger.log(ActivityType.UPDATE_USER_SETTINGS,
    // userInfo.getUserId() + "");
    LOGGER.info("User Settings saved successfully for user(user_id: " + userInfo.getUserId() + ").");
    return userInfo;
}
Also used : UserSettingEntity(org.usermanagement.dao.entity.UserSettingEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class UserService method reset2fa.

/**
 * Reset 2 fa.
 *
 * @param userId the user id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void reset2fa(final long userId) {
    UserEntity userEntity = userRepository.findByUserId(userId);
    if (ValidatorUtil.isNull(userEntity)) {
        LOGGER.warn("User Entity not found for user(user_id: " + userId + ")");
        throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userId + ""));
    }
    userEntity.setIs2FaConfigured(false);
    userEntity.setTwoFaKey(null);
    userEntity = userRepository.save(userEntity);
    activityLogger.log(ActivityType.RESET_2FA, userEntity.getUsername());
    LOGGER.info("2FA reset successfully for user(user_id: " + userId + ").");
    try {
        if (!userEntity.getIs2FaConfigured()) {
            Map<String, Object> context = new HashMap<>();
            context.put("name", userEntity.getName());
            mailService.send(userEntity.getEmail(), mailUtils.getSubjectReset2fa(), TemplateService.Template.RESET_2FA, context);
            LOGGER.info("Reset 2FA mail sent successfully for user(user_id: " + userId + ").");
        }
    } catch (Exception e) {
        LOGGER.warn("Reset 2FA mail failed for user: " + userId);
    }
}
Also used : HashMap(java.util.HashMap) RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) OtpRequiredException(org.openkilda.exception.OtpRequiredException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) InvalidOtpException(org.openkilda.exception.InvalidOtpException) TwoFaKeyNotSetException(org.openkilda.exception.TwoFaKeyNotSetException) AccessDeniedException(java.nio.file.AccessDeniedException) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class UserService method changePassword.

/**
 * Change password.
 *
 * @param userInfo the user info
 * @param userId the user id
 * @return the user info
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo changePassword(final UserInfo userInfo, final Long userId) {
    userValidator.validateChangePassword(userInfo);
    UserEntity userEntity = userRepository.findByUserId(userId);
    if (ValidatorUtil.isNull(userEntity)) {
        LOGGER.warn("User Entity not found for user(id: " + userId + ")");
        throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userId + ""));
    }
    if (!StringUtil.matches(userInfo.getPassword(), userEntity.getPassword())) {
        LOGGER.warn("Password not matched for user (id: " + userId + "). Error: " + messageUtil.getAttributePasswordInvalid());
        throw new RequestValidationException(messageUtil.getAttributePasswordInvalid());
    }
    if (userEntity.getIs2FaEnabled()) {
        if (!userEntity.getIs2FaConfigured()) {
            LOGGER.warn("2FA key is not configured for user(id: " + userId + "). Error: " + messageUtil.getAttribute2faNotConfiured());
            throw new TwoFaKeyNotSetException(messageUtil.getAttribute2faNotConfiured());
        } else {
            if (userInfo.getCode() == null || userInfo.getCode().isEmpty()) {
                LOGGER.warn("OTP code is madatory as 2FA is configured for user (id: " + userId + "). Error: " + messageUtil.getAttributeNotNull("OTP"));
                throw new OtpRequiredException(messageUtil.getAttributeNotNull("OTP"));
            } else if (!TwoFactorUtility.validateOtp(userInfo.getCode(), userEntity.getTwoFaKey())) {
                LOGGER.warn("Invalid OTP for user (id: " + userId + "). Error: " + messageUtil.getAttributeNotvalid("OTP"));
                throw new RequestValidationException(messageUtil.getAttributeNotvalid("OTP"));
            }
        }
    }
    userEntity.setPassword(StringUtil.encodeString(userInfo.getNewPassword()));
    userEntity.setUpdatedDate(new Date());
    userEntity = userRepository.save(userEntity);
    activityLogger.log(ActivityType.CHANGE_PASSWORD, userEntity.getUsername());
    LOGGER.info("User(userId: " + userId + ") password changed successfully.");
    try {
        Map<String, Object> context = new HashMap<>();
        context.put("name", userEntity.getName());
        mailService.send(userEntity.getEmail(), mailUtils.getSubjectChangePassword(), TemplateService.Template.CHANGE_PASSWORD, context);
        LOGGER.info("Changed password mail sent successfully for user(userId: " + userId + ").");
    } catch (Exception e) {
        LOGGER.warn("Change password email failed for username: " + userEntity.getUsername());
    }
    return UserConversionUtil.toUserInfo(userEntity);
}
Also used : HashMap(java.util.HashMap) RequestValidationException(org.usermanagement.exception.RequestValidationException) TwoFaKeyNotSetException(org.openkilda.exception.TwoFaKeyNotSetException) UserEntity(org.usermanagement.dao.entity.UserEntity) OtpRequiredException(org.openkilda.exception.OtpRequiredException) Date(java.util.Date) OtpRequiredException(org.openkilda.exception.OtpRequiredException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) InvalidOtpException(org.openkilda.exception.InvalidOtpException) TwoFaKeyNotSetException(org.openkilda.exception.TwoFaKeyNotSetException) AccessDeniedException(java.nio.file.AccessDeniedException) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class PermissionService method getPermissionById.

/**
 * Gets the permission by id.
 *
 * @param permissionId the permission id
 * @return the permission by id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission getPermissionById(final Long permissionId) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    if (ValidatorUtil.isNull(permissionEntity)) {
        LOGGER.warn("Permission with permissionId '" + permissionId + "' not found. Error: " + messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
    }
    Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
    return PermissionConversionUtil.toPermission(permissionEntity, roleEntityList);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class PermissionService method deletePermissionById.

/**
 * Delete permission by id.
 *
 * @param permissionId the permission id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void deletePermissionById(final Long permissionId) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    if (ValidatorUtil.isNull(permissionEntity)) {
        throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
    }
    Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
    if (roleEntityList.size() > 0) {
        String roles = "";
        for (RoleEntity roleEntity : roleEntityList) {
            roles += !"".equals(roles) ? "," + roleEntity.getName() : roleEntity.getName();
        }
        LOGGER.warn("Permission with permissionId '" + permissionId + "' not allowed to delete. Error: " + messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
        throw new RequestValidationException(messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
    }
    permissionRepository.delete(permissionEntity);
    LOGGER.info("Permission(permissionId: " + permissionId + ") deleted successfully.");
    activityLogger.log(ActivityType.DELETE_PERMISSION, permissionEntity.getName());
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RequestValidationException (org.usermanagement.exception.RequestValidationException)25 Transactional (org.springframework.transaction.annotation.Transactional)16 RoleEntity (org.usermanagement.dao.entity.RoleEntity)9 UserEntity (org.usermanagement.dao.entity.UserEntity)7 PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)6 AccessDeniedException (java.nio.file.AccessDeniedException)4 HashMap (java.util.HashMap)3 InvalidOtpException (org.openkilda.exception.InvalidOtpException)3 OtpRequiredException (org.openkilda.exception.OtpRequiredException)3 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)3 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)3 UserInfo (org.usermanagement.model.UserInfo)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 SamlConfigEntity (org.openkilda.saml.dao.entity.SamlConfigEntity)2 OauthConfigEntity (org.openkilda.store.auth.dao.entity.OauthConfigEntity)2 UrlDto (org.openkilda.store.model.UrlDto)2 Role (org.usermanagement.model.Role)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1