Search in sources :

Example 21 with RequestValidationException

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

the class RoleService method createRole.

/**
 * Creates the role.
 *
 * @param role the role
 * @return the role
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role createRole(final Role role) {
    roleValidator.validateRole(role);
    Set<PermissionEntity> permissionEntities = new HashSet<>();
    List<PermissionEntity> permissionEntityList = permissionRepository.findAll();
    for (Long permissionId : role.getPermissionId()) {
        PermissionEntity permissionEntity = permissionEntityList.parallelStream().filter((entity) -> entity.getPermissionId().equals(permissionId)).findFirst().orElse(null);
        if (!ValidatorUtil.isNull(permissionEntity)) {
            permissionEntities.add(permissionEntity);
        } else {
            LOGGER.warn("Permission with id '" + permissionId + "' not found.");
            throw new RequestValidationException(messageUtil.getAttributeNotFound("permission"));
        }
    }
    RoleEntity roleEntity = RoleConversionUtil.toRoleEntity(role, permissionEntities);
    roleRepository.save(roleEntity);
    activityLogger.log(ActivityType.CREATE_ROLE, role.getName());
    LOGGER.info("Role with name '" + roleEntity.getName() + "' created successfully.");
    return RoleConversionUtil.toRole(roleEntity, true, false);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with RequestValidationException

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

the class UserService method deleteUserById.

/**
 * Delete user by id.
 *
 * @param userId the user id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void deleteUserById(final Long userId) {
    UserEntity userEntity = userRepository.findByUserId(userId);
    if (ValidatorUtil.isNull(userEntity)) {
        LOGGER.warn("User with user id '" + userId + "' not found. Error: " + messageUtil.getAttributeInvalid("user_id", userId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userId + ""));
    }
    userRepository.delete(userEntity);
    activityLogger.log(ActivityType.DELETE_USER, userEntity.getUsername());
    LOGGER.info("User deleted successfully (userId: " + userId + ")");
}
Also used : RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 23 with RequestValidationException

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

the class UserService method updateLoginDetail.

/**
 * Update login detail.
 *
 * @param userName the user name
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void updateLoginDetail(final String userName) {
    UserEntity userEntity = userRepository.findByUsernameIgnoreCase(userName);
    if (ValidatorUtil.isNull(userEntity)) {
        LOGGER.warn("User with username '" + userName + "' not found. Error: " + messageUtil.getAttributeInvalid("username", userName + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("username", userName + ""));
    }
    userEntity.setLoginTime(Calendar.getInstance().getTime());
    userEntity.setFailedLoginCount(null);
    userEntity.setUnlockTime(null);
    if (userEntity.getIs2FaEnabled()) {
        userEntity.setIs2FaConfigured(true);
    }
    userRepository.save(userEntity);
    LOGGER.info("User last login updated successfully (username: " + userName + ")");
}
Also used : RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with RequestValidationException

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

the class PermissionService method updatePermission.

/**
 * Update permission.
 *
 * @param permissionId the permission id
 * @param permission the permission
 * @return the permission
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission updatePermission(final Long permissionId, final Permission permission) {
    permissionValidator.validateUpdatePermission(permission, 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 + ""));
    }
    permissionEntity = PermissionConversionUtil.toUpatePermissionEntity(permission, permissionEntity);
    permissionRepository.save(permissionEntity);
    activityLogger.log(ActivityType.UPDATE_PERMISSION, permissionEntity.getName());
    LOGGER.info("Permission(permissionId: " + permissionId + ") updated successfully.");
    return PermissionConversionUtil.toPermission(permissionEntity, null);
}
Also used : PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 with RequestValidationException

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

the class UserValidator method validateChangePassword.

/**
 * Validate change password.
 *
 * @param userInfo the user info
 */
public void validateChangePassword(final UserInfo userInfo) {
    String regexOne = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\\w\\s]).{8,15}$";
    String regexTwo = "[%,&,+,\\,\\s,\"]";
    if (ValidatorUtil.isNull(userInfo.getNewPassword())) {
        LOGGER.warn("Validation fail for change user password request (id: " + userInfo.getUserId() + "). Error: " + messageUtil.getAttributeNotNull("password"));
        throw new RequestValidationException(messageUtil.getAttributeNotNull("password"));
    }
    Matcher matcherOne = Pattern.compile(regexOne).matcher(userInfo.getNewPassword());
    if (!matcherOne.matches()) {
        LOGGER.warn("Validation fail for change user password request (id: " + userInfo.getUserId() + "). Error: " + messageUtil.getAttributePasswordMustContain());
        throw new RequestValidationException(messageUtil.getAttributePasswordMustContain());
    }
    Matcher matcherTwo = Pattern.compile(regexTwo).matcher(userInfo.getNewPassword());
    if (matcherTwo.find()) {
        LOGGER.warn("Validation fail for change user password request (id: " + userInfo.getUserId() + "). Error: " + messageUtil.getAttributePasswordMustNotContain());
        throw new RequestValidationException(messageUtil.getAttributePasswordMustNotContain());
    }
    if (userInfo.getNewPassword().equals(userInfo.getPassword())) {
        LOGGER.warn("New password not valid (id: " + userInfo.getUserId() + "). Error: " + messageUtil.getAttributePasswordShouldNotSame());
        throw new RequestValidationException(messageUtil.getAttributePasswordShouldNotSame());
    }
    if (userInfo.getNewPassword().equals(userInfo.getPassword())) {
        LOGGER.warn("New password not valid (id: " + userInfo.getUserId() + "). Error: " + messageUtil.getAttributePasswordLength("8", "15"));
        throw new RequestValidationException(messageUtil.getAttributePasswordLength("8", "15"));
    }
}
Also used : Matcher(java.util.regex.Matcher) RequestValidationException(org.usermanagement.exception.RequestValidationException)

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