Search in sources :

Example 1 with RolePermissionEntity

use of com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity in project boot-admin by hb0730.

the class RolePermissionServiceImpl method savePermissionIdByRoleId.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean savePermissionIdByRoleId(@Nonnull Long id, @Nonnull Collection<Long> permissionIds) {
    Assert.notNull(id, "角色id为空");
    Assert.notEmpty(permissionIds, "权限id为空");
    LambdaQueryWrapper<PermissionEntity> queryWrapper = Wrappers.lambdaQuery(PermissionEntity.class).in(PermissionEntity::getId, permissionIds).select(PermissionEntity::getId);
    List<PermissionEntity> entities = permissionMapper.selectList(queryWrapper);
    if (CollectionUtils.isEmpty(entities)) {
        throw new BusinessException("请传入正确的权限id");
    }
    Set<Long> ids = entities.parallelStream().map(PermissionEntity::getId).collect(Collectors.toSet());
    List<RolePermissionEntity> rolePermissionEntities = new ArrayList<>(ids.size());
    for (Long permissionId : ids) {
        RolePermissionEntity entity = new RolePermissionEntity();
        entity.setPermissionId(permissionId);
        entity.setRoleId(id);
        rolePermissionEntities.add(entity);
    }
    return super.saveBatch(rolePermissionEntities);
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity) RolePermissionEntity(com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity) RolePermissionEntity(com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with RolePermissionEntity

use of com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity in project boot-admin by hb0730.

the class RoleServiceImpl method updateRolePermission.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateRolePermission(@Nonnull Long id, List<Long> permissionIds) {
    Assert.notNull(id, "角色id不为空");
    LambdaQueryWrapper<RolePermissionEntity> queryWrapper = Wrappers.lambdaQuery(RolePermissionEntity.class).eq(RolePermissionEntity::getRoleId, id);
    // 角色 没有权限
    if (CollectionUtils.isEmpty(permissionIds)) {
        rolePermissionService.remove(queryWrapper);
        return true;
    }
    List<RolePermissionEntity> permissionEntities = rolePermissionService.list(queryWrapper);
    // 权限
    if (CollectionUtils.isEmpty(permissionEntities)) {
        rolePermissionService.savePermissionIdByRoleId(id, permissionIds);
        return true;
    }
    // 旧的
    Set<Long> oldPermissionIds = permissionEntities.parallelStream().map(RolePermissionEntity::getPermissionId).collect(Collectors.toSet());
    Set<Long> oldPermissionIds2 = Sets.newHashSet(oldPermissionIds);
    // 新
    Set<Long> newPermissionIds = Sets.newHashSet(permissionIds);
    // 旧数据-新数据=已过期数据
    oldPermissionIds.removeAll(newPermissionIds);
    if (!CollectionUtils.isEmpty(oldPermissionIds)) {
        LambdaQueryWrapper<RolePermissionEntity> query = Wrappers.lambdaQuery(RolePermissionEntity.class).eq(RolePermissionEntity::getRoleId, id).in(RolePermissionEntity::getPermissionId, oldPermissionIds);
        rolePermissionService.remove(query);
    }
    // 新数据-旧数据=新增数据
    newPermissionIds.removeAll(oldPermissionIds2);
    if (!CollectionUtils.isEmpty(newPermissionIds)) {
        rolePermissionService.savePermissionIdByRoleId(id, newPermissionIds);
    }
    eventPublisher.publishEvent(new RolePermissionEvent(this, id));
    return true;
}
Also used : RolePermissionEvent(com.hb0730.boot.admin.event.role.RolePermissionEvent) RolePermissionEntity(com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RolePermissionEntity (com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity)2 Transactional (org.springframework.transaction.annotation.Transactional)2 RolePermissionEvent (com.hb0730.boot.admin.event.role.RolePermissionEvent)1 BusinessException (com.hb0730.boot.admin.exceptions.BusinessException)1 PermissionEntity (com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)1