Search in sources :

Example 6 with RequestValidationException

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

the class SamlConversionUtil method toUpdateSamlConfigEntity.

/**
 * To update saml config entity.
 *
 * @param samlConfigEntity the saml config entity
 * @param roleEntities the role entities
 * @param file the metadata file
 * @param name the provider name
 * @param url the metadata url
 * @param entityId the entityId
 * @param status the provider status
 * @param userCreation the userCreation
 * @param attribute the attribute
 * @return the requireManagerUpdateStatus
 */
public static boolean toUpdateSamlConfigEntity(SamlConfigEntity samlConfigEntity, Set<RoleEntity> roleEntities, MultipartFile file, String name, String url, String entityId, boolean status, boolean userCreation, String attribute) {
    Blob blob = null;
    boolean requireManagerUpdate = false;
    try {
        if (file != null) {
            byte[] bytes = file.getBytes();
            try {
                blob = new SerialBlob(bytes);
            } catch (SerialException e) {
                LOGGER.error("Error occurred while updating saml provider" + e);
            } catch (SQLException e) {
                LOGGER.error("Error occurred while updating saml provider" + e);
            }
            samlConfigEntity.setMetadata(blob);
            samlConfigEntity.setType(IConstants.ProviderType.FILE);
            requireManagerUpdate = true;
            samlConfigEntity.setUrl(null);
        } else if (url != null) {
            samlConfigEntity.setUrl(url);
            samlConfigEntity.setType(IConstants.ProviderType.URL);
            requireManagerUpdate = true;
            samlConfigEntity.setMetadata(null);
        }
        samlConfigEntity.setEntityId(entityId);
        if (userCreation) {
            samlConfigEntity.setUserCreation(true);
            if (!samlConfigEntity.getRoles().isEmpty()) {
                samlConfigEntity.getRoles().clear();
            }
            samlConfigEntity.getRoles().addAll(roleEntities);
        } else {
            samlConfigEntity.setRoles(null);
        }
        samlConfigEntity.setName(name);
        samlConfigEntity.setUserCreation(userCreation);
        samlConfigEntity.setAttribute(attribute);
        samlConfigEntity.setStatus(status);
    } catch (RequestValidationException e) {
        throw new RequestValidationException(e.getMessage());
    } catch (FileNotFoundException e) {
        LOGGER.error("Error occurred while updating saml provider" + e);
    } catch (IOException e) {
        LOGGER.error("Error occurred while updating saml provider" + e);
    }
    return requireManagerUpdate;
}
Also used : SerialBlob(javax.sql.rowset.serial.SerialBlob) Blob(java.sql.Blob) SerialException(javax.sql.rowset.serial.SerialException) SQLException(java.sql.SQLException) FileNotFoundException(java.io.FileNotFoundException) SerialBlob(javax.sql.rowset.serial.SerialBlob) IOException(java.io.IOException) RequestValidationException(org.usermanagement.exception.RequestValidationException)

Example 7 with RequestValidationException

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

the class RoleService method assignRoleByPermissionId.

/**
 * Assign role by permission id.
 *
 * @param permissionId the permission id
 * @param request the request
 * @return the permission
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission assignRoleByPermissionId(final Long permissionId, final Permission request) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    if (ValidatorUtil.isNull(permissionEntity)) {
        LOGGER.warn("Permission with permissionId '" + permissionId + "' not found. Error: " + messageUtil.getAttributeInvalid("permissionId", permissionId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("permissionId", permissionId + ""));
    }
    permissionEntity.getRoles().clear();
    if (request.getRoles() != null) {
        for (Role role : request.getRoles()) {
            RoleEntity roleEntity = roleRepository.findByRoleId(role.getRoleId());
            permissionEntity.getRoles().add(roleEntity);
        }
    }
    permissionRepository.save(permissionEntity);
    activityLogger.log(ActivityType.ASSIGN_ROLES_TO_PERMISSION, permissionEntity.getName());
    LOGGER.info("Roles assigned with permission successfully (permissionId: " + permissionId + ")");
    return RoleConversionUtil.toPermissionByRole(permissionEntity.getRoles(), permissionEntity);
}
Also used : Role(org.usermanagement.model.Role) RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with RequestValidationException

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

the class RoleService method getRoleByName.

/**
 * Gets the role by name.
 *
 * @param role the role
 * @return the role by name
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public List<Role> getRoleByName(final Set<String> role) {
    List<Role> roles = new ArrayList<Role>();
    List<RoleEntity> roleEntities = roleRepository.findByNameIn(role);
    if (ValidatorUtil.isNull(roleEntities)) {
        LOGGER.warn("Roles with name '" + role + "' not found. Error: " + messageUtil.getAttributeInvalid("role", role + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("role", role + ""));
    }
    for (RoleEntity roleEntity : roleEntities) {
        if (Status.ACTIVE.getStatusEntity().getStatus().equalsIgnoreCase(roleEntity.getStatusEntity().getStatus())) {
            roles.add(RoleConversionUtil.toRole(roleEntity, true, false));
        }
    }
    return roles;
}
Also used : Role(org.usermanagement.model.Role) RoleEntity(org.usermanagement.dao.entity.RoleEntity) ArrayList(java.util.ArrayList) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with RequestValidationException

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

the class UserService method resetPassword.

/**
 * Reset password.
 *
 * @param userId the user id
 * @param adminFlag the admin flag
 * @return the user info
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo resetPassword(final long userId, final boolean adminFlag) {
    UserInfo userinfo = new UserInfo();
    userinfo.setUserId(userId);
    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 + ""));
    }
    String randomPassword = ValidatorUtil.randomAlphaNumeric(16);
    userEntity = UserConversionUtil.toResetPwdUserEntity(userEntity, randomPassword);
    if (adminFlag) {
        userEntity.setIs2FaConfigured(false);
        userEntity.setTwoFaKey(null);
    }
    userEntity = userRepository.save(userEntity);
    if (adminFlag) {
        activityLogger.log(ActivityType.ADMIN_RESET_PASSWORD, userEntity.getUsername());
    } else {
        activityLogger.log(ActivityType.RESET_PASSWORD, userEntity.getUsername());
    }
    LOGGER.info("Password reset successfully for user(userId: " + userId + ").");
    try {
        if (!adminFlag) {
            Map<String, Object> context = new HashMap<>();
            context.put("name", userEntity.getName());
            context.put("password", randomPassword);
            mailService.send(userEntity.getEmail(), mailUtils.getSubjectResetPassword(), TemplateService.Template.RESET_ACCOUNT_PASSWORD, context);
            LOGGER.info("Reset password mail sent successfully for user(userId: " + userId + ").");
        }
    } catch (Exception e) {
        LOGGER.warn("Reset password mail failed for username: " + userEntity.getUsername());
    }
    userinfo.setPassword(randomPassword);
    return userinfo;
}
Also used : HashMap(java.util.HashMap) UserInfo(org.usermanagement.model.UserInfo) 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 10 with RequestValidationException

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

the class UserService method assignUserByRoleId.

/**
 * Assign user by role id.
 *
 * @param roleId the role id
 * @param role the role
 * @return the role
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role assignUserByRoleId(final Long roleId, final Role role) {
    RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
    if (ValidatorUtil.isNull(roleEntity)) {
        LOGGER.warn("Role with role id '" + roleId + "' not found. Error: " + messageUtil.getAttributeInvalid("role_id", roleId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("role_id", roleId + ""));
    }
    roleEntity.getUsers().clear();
    if (role.getUserInfo() != null) {
        for (UserInfo user : role.getUserInfo()) {
            UserEntity userEntity = userRepository.findByUserId(user.getUserId());
            roleEntity.getUsers().add(userEntity);
        }
    }
    roleEntity = roleRepository.save(roleEntity);
    activityLogger.log(ActivityType.ASSIGN_USERS_BY_ROLE, roleEntity.getName());
    LOGGER.info("Users assigned with role successfully (role id: " + roleId + ")");
    return RoleConversionUtil.toRole(roleEntity, false, true);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) UserInfo(org.usermanagement.model.UserInfo) RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) 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