Search in sources :

Example 11 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class PermissionConversionUtil method toPermission.

/**
 * To permission.
 *
 * @param permissionEntity the permission entity
 * @param roleEntities the role entities
 * @return the permission
 */
public static Permission toPermission(final PermissionEntity permissionEntity, final Set<RoleEntity> roleEntities) {
    Permission permission = new Permission();
    permission.setName(permissionEntity.getName());
    permission.setPermissionId(permissionEntity.getPermissionId());
    permission.setIsEditable(permissionEntity.getIsEditable());
    permission.setIsAdminPermission(permissionEntity.getIsAdminPermission());
    permission.setStatus(permissionEntity.getStatusEntity().getStatus());
    permission.setDescription(permissionEntity.getDescription());
    if (!ValidatorUtil.isNull(roleEntities)) {
        List<Role> roles = new ArrayList<>();
        for (RoleEntity entity : roleEntities) {
            Role role = new Role();
            role.setRoleId(entity.getRoleId());
            role.setName(entity.getName());
            roles.add(role);
        }
        permission.setRoles(roles);
    }
    return permission;
}
Also used : Role(org.usermanagement.model.Role) RoleEntity(org.usermanagement.dao.entity.RoleEntity) Permission(org.usermanagement.model.Permission) ArrayList(java.util.ArrayList)

Example 12 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleConversionUtil method toPermissionByRole.

/**
 * To permission by role.
 *
 * @param roleEntityList the role entity list
 * @param permissionEntity the permission entity
 * @return the permission
 */
public static Permission toPermissionByRole(final Set<RoleEntity> roleEntityList, final PermissionEntity permissionEntity) {
    Permission permission = new Permission();
    permission.setName(permissionEntity.getName());
    permission.setDescription(permissionEntity.getDescription());
    permission.setPermissionId(permissionEntity.getPermissionId());
    List<Role> role = new ArrayList<>();
    for (RoleEntity roleEntity : roleEntityList) {
        Role roles = new Role();
        roles.setRoleId(roleEntity.getRoleId());
        roles.setName(roleEntity.getName());
        role.add(roles);
    }
    permission.setRoles(role);
    return permission;
}
Also used : Role(org.usermanagement.model.Role) RoleEntity(org.usermanagement.dao.entity.RoleEntity) Permission(org.usermanagement.model.Permission) ArrayList(java.util.ArrayList)

Example 13 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleService method deleteRoleById.

/**
 * Delete role by id.
 *
 * @param roleId the role id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void deleteRoleById(final Long roleId) {
    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 + ""));
    }
    Set<UserEntity> userEntityList = userRepository.findByRoles_roleId(roleId);
    if (userEntityList.size() > 0) {
        String users = "";
        for (UserEntity userEntity : userEntityList) {
            users += !"".equals(users) ? "," + userEntity.getName() : userEntity.getName();
        }
        LOGGER.warn("Role with role id '" + roleId + "' not allowed to delete. Error: " + messageUtil.getAttributeDeletionNotAllowed(roleEntity.getName(), users));
        throw new RequestValidationException(messageUtil.getAttributeDeletionNotAllowed(roleEntity.getName(), users));
    }
    roleRepository.delete(roleEntity);
    activityLogger.log(ActivityType.DELETE_ROLE, roleEntity.getName());
    LOGGER.info("Role(roleId: " + roleId + ") deleted successfully.");
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) UserEntity(org.usermanagement.dao.entity.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleService method getRolesById.

/**
 * Gets the roles by id.
 *
 * @param roleIds the role ids
 * @return the roles by id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Set<RoleEntity> getRolesById(final List<Long> roleIds) {
    Set<RoleEntity> roleEntities = new HashSet<>();
    List<RoleEntity> roleEntityList = roleRepository.findAll();
    for (Long roleId : roleIds) {
        RoleEntity roleEntity = roleEntityList.parallelStream().filter((entity) -> entity.getRoleId().equals(roleId)).findFirst().orElse(null);
        if (!ValidatorUtil.isNull(roleEntity)) {
            roleEntities.add(roleEntity);
        } else {
            LOGGER.warn("Role with role id '" + roleId + "' not found. Error: " + messageUtil.getAttributeNotFound("roles"));
            throw new RequestValidationException(messageUtil.getAttributeNotFound("roles"));
        }
    }
    return roleEntities;
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleService method updateRole.

/**
 * Update role.
 *
 * @param roleId the role id
 * @param role the role
 * @return the role
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Role updateRole(final Long roleId, final Role role) {
    roleValidator.validateUpdateRole(role, roleId);
    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 + ""));
    }
    if (role.getPermissionId() != null) {
        roleEntity.getPermissions().clear();
        for (Long permissionId : role.getPermissionId()) {
            PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
            if (permissionEntity != null) {
                roleEntity.getPermissions().add(permissionEntity);
            }
        }
    }
    roleEntity = RoleConversionUtil.toUpateRoleEntity(role, roleEntity);
    roleRepository.save(roleEntity);
    activityLogger.log(ActivityType.UPDATE_ROLE, roleEntity.getName());
    LOGGER.info("Role updated successfully (roleId: " + roleId + ")");
    return RoleConversionUtil.toRole(roleEntity, true, false);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RoleEntity (org.usermanagement.dao.entity.RoleEntity)21 Transactional (org.springframework.transaction.annotation.Transactional)12 RequestValidationException (org.usermanagement.exception.RequestValidationException)10 PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)7 HashSet (java.util.HashSet)5 UserEntity (org.usermanagement.dao.entity.UserEntity)5 Role (org.usermanagement.model.Role)4 ArrayList (java.util.ArrayList)3 UserInfo (org.usermanagement.model.UserInfo)3 SamlConfigEntity (org.openkilda.saml.dao.entity.SamlConfigEntity)2 SamlConfig (org.openkilda.saml.model.SamlConfig)2 Permission (org.usermanagement.model.Permission)2 AccessDeniedException (java.nio.file.AccessDeniedException)1 HashMap (java.util.HashMap)1 InvalidOtpException (org.openkilda.exception.InvalidOtpException)1 OtpRequiredException (org.openkilda.exception.OtpRequiredException)1 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)1 NameID (org.opensaml.saml2.core.NameID)1 MetadataProviderException (org.opensaml.saml2.metadata.provider.MetadataProviderException)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1