Search in sources :

Example 1 with PermissionEntity

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

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

the class PermissionService method createPermission.

/**
 * Creates the permission.
 *
 * @param permission the permission
 * @return the permission
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission createPermission(final Permission permission) {
    permissionValidator.validatePermission(permission);
    PermissionEntity permissionEntity = PermissionConversionUtil.toPermissionEntity(permission);
    permissionRepository.save(permissionEntity);
    activityLogger.log(ActivityType.CREATE_PERMISSION, permission.getName());
    LOGGER.info("Permission with name '" + permission.getName() + "' created successfully.");
    return PermissionConversionUtil.toPermission(permissionEntity, null);
}
Also used : PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with PermissionEntity

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

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

Example 5 with PermissionEntity

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

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