Search in sources :

Example 6 with PermissionEntity

use of org.usermanagement.dao.entity.PermissionEntity 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 7 with PermissionEntity

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

the class RoleService method getRolesByPermissionId.

/**
 * Gets the roles by permission id.
 *
 * @param permissionId the permission id
 * @return the roles by permission id
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission getRolesByPermissionId(final Long permissionId) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
    return RoleConversionUtil.toPermissionByRole(roleEntityList, permissionEntity);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with PermissionEntity

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

the class UserService method populateUserInfo.

/**
 * Adds user information in session.
 *
 * @param userInfo the userInfo
 * @param username who's information is added in session.
 */
public void populateUserInfo(final UserInfo userInfo, final String username) {
    UserEntity user = getUserByUsername(username);
    Set<RoleEntity> roleEntities = user.getRoles();
    Set<String> roles = new HashSet<String>();
    Set<String> permissions = new HashSet<String>();
    for (RoleEntity roleEntity : roleEntities) {
        roles.add(roleEntity.getName());
        userInfo.setRole("ROLE_ADMIN");
        if (user.getUserId() != 1) {
            Set<PermissionEntity> permissionEntities = roleEntity.getPermissions();
            for (PermissionEntity permissionEntity : permissionEntities) {
                if (permissionEntity.getStatusEntity().getStatusCode().equalsIgnoreCase(Status.ACTIVE.getCode()) && !permissionEntity.getIsAdminPermission()) {
                    permissions.add(permissionEntity.getName());
                }
            }
        }
    }
    if (user.getUserId() == 1) {
        List<PermissionEntity> permissionEntities = permissionRepository.findAll();
        for (PermissionEntity permissionEntity : permissionEntities) {
            permissions.add(permissionEntity.getName());
        }
    }
    userInfo.setUserId(user.getUserId());
    userInfo.setUsername(user.getUsername());
    userInfo.setName(user.getName());
    userInfo.setRoles(roles);
    userInfo.setPermissions(permissions);
    userInfo.setIs2FaEnabled(user.getIs2FaEnabled());
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) UserEntity(org.usermanagement.dao.entity.UserEntity) HashSet(java.util.HashSet)

Example 9 with PermissionEntity

use of org.usermanagement.dao.entity.PermissionEntity 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 10 with PermissionEntity

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

the class RoleConversionUtil method toRole.

/**
 * To role.
 *
 * @param roleEntity the role entity
 * @param withPermissions the with permissions
 * @param withUsers the with users
 * @return the role
 */
public static Role toRole(final RoleEntity roleEntity, final boolean withPermissions, final boolean withUsers) {
    Role role = new Role();
    role.setName(roleEntity.getName());
    role.setRoleId(roleEntity.getRoleId());
    role.setStatus(roleEntity.getStatusEntity().getStatus());
    role.setDescription(roleEntity.getDescription());
    if (withPermissions) {
        List<Permission> permissionList = new ArrayList<Permission>();
        if (!ValidatorUtil.isNull(roleEntity.getPermissions())) {
            for (PermissionEntity permissionEntity : roleEntity.getPermissions()) {
                permissionList.add(PermissionConversionUtil.toPermission(permissionEntity, null));
            }
            role.setPermissions(permissionList);
        }
    }
    if (withUsers) {
        List<UserInfo> userInfoList = new ArrayList<>();
        for (UserEntity userEntity : roleEntity.getUsers()) {
            if (userEntity.getUserId() != 1) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(userEntity.getUserId());
                userInfo.setName(userEntity.getName());
                userInfoList.add(userInfo);
            }
        }
        role.setUserInfo(userInfoList);
    }
    return role;
}
Also used : Role(org.usermanagement.model.Role) Permission(org.usermanagement.model.Permission) ArrayList(java.util.ArrayList) UserInfo(org.usermanagement.model.UserInfo) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) UserEntity(org.usermanagement.dao.entity.UserEntity)

Aggregations

PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)11 Transactional (org.springframework.transaction.annotation.Transactional)8 RoleEntity (org.usermanagement.dao.entity.RoleEntity)7 RequestValidationException (org.usermanagement.exception.RequestValidationException)6 HashSet (java.util.HashSet)2 UserEntity (org.usermanagement.dao.entity.UserEntity)2 Role (org.usermanagement.model.Role)2 ArrayList (java.util.ArrayList)1 StatusEntity (org.usermanagement.dao.entity.StatusEntity)1 Permission (org.usermanagement.model.Permission)1 UserInfo (org.usermanagement.model.UserInfo)1