完成 RoleApi、PermissionApi 的 feign 支持

This commit is contained in:
YunaiV 2022-06-15 23:14:44 +08:00
parent 84744938be
commit b52a38d297
7 changed files with 80 additions and 75 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.datapermission.core.rule.DataPermissionRule;
@ -105,7 +106,9 @@ public class DeptDataPermissionRule implements DataPermissionRule {
DeptDataPermissionRespDTO deptDataPermission = loginUser.getContext(CONTEXT_KEY, DeptDataPermissionRespDTO.class);
// 从上下文中拿不到则调用逻辑进行获取
if (deptDataPermission == null) {
deptDataPermission = permissionApi.getDeptDataPermission(loginUser.getId());
CommonResult<DeptDataPermissionRespDTO> getDeptDataPermissionResult = permissionApi.getDeptDataPermission(loginUser.getId());
getDeptDataPermissionResult.checkError();
deptDataPermission = getDeptDataPermissionResult.getData();
if (deptDataPermission == null) {
log.error("[getExpression][LoginUser({}) 获取数据权限为 null]", JsonUtils.toJsonString(loginUser));
throw new NullPointerException(String.format("LoginUser(%d) Table(%s/%s) 未返回数据权限",

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.datapermission.core.rule.dept;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
@ -19,6 +20,7 @@ import org.mockito.MockedStatic;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.datapermission.core.rule.dept.DeptDataPermissionRule.EXPRESSION_NULL;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
@ -95,7 +97,7 @@ class DeptDataPermissionRuleTest extends BaseMockitoUnitTest {
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser);
// mock 方法DeptDataPermissionRespDTO
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO().setAll(true);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(success(deptDataPermission));
// 调用
Expression expression = rule.getExpression(tableName, tableAlias);
@ -118,7 +120,7 @@ class DeptDataPermissionRuleTest extends BaseMockitoUnitTest {
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser);
// mock 方法DeptDataPermissionRespDTO
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO();
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(success(deptDataPermission));
// 调用
Expression expression = rule.getExpression(tableName, tableAlias);
@ -142,7 +144,7 @@ class DeptDataPermissionRuleTest extends BaseMockitoUnitTest {
// mock 方法DeptDataPermissionRespDTO
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO()
.setDeptIds(SetUtils.asSet(10L, 20L)).setSelf(true);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(success(deptDataPermission));
// 调用
Expression expression = rule.getExpression(tableName, tableAlias);
@ -166,7 +168,7 @@ class DeptDataPermissionRuleTest extends BaseMockitoUnitTest {
// mock 方法DeptDataPermissionRespDTO
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO()
.setSelf(true);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(success(deptDataPermission));
// 添加 user 字段配置
rule.addUserColumn("t_user", "id");
@ -192,7 +194,7 @@ class DeptDataPermissionRuleTest extends BaseMockitoUnitTest {
// mock 方法DeptDataPermissionRespDTO
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO()
.setDeptIds(CollUtil.newLinkedHashSet(10L, 20L));
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(success(deptDataPermission));
// 添加 dept 字段配置
rule.addDeptColumn("t_user", "dept_id");
@ -218,7 +220,7 @@ class DeptDataPermissionRuleTest extends BaseMockitoUnitTest {
// mock 方法DeptDataPermissionRespDTO
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO()
.setDeptIds(CollUtil.newLinkedHashSet(10L, 20L)).setSelf(true);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission);
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(success(deptDataPermission));
// 添加 user 字段配置
rule.addUserColumn("t_user", "id");
// 添加 dept 字段配置

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.framework.security.core.service;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
@ -27,7 +28,9 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
@Override
public boolean hasAnyPermissions(String... permissions) {
return permissionApi.hasAnyPermissions(getLoginUserId(), permissions);
CommonResult<Boolean> hasAnyPermissionsResult = permissionApi.hasAnyPermissions(getLoginUserId(), permissions);
hasAnyPermissionsResult.checkError();
return hasAnyPermissionsResult.getData();
}
@Override
@ -37,7 +40,9 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
@Override
public boolean hasAnyRoles(String... roles) {
return permissionApi.hasAnyRoles(getLoginUserId(), roles);
CommonResult<Boolean> hasAnyRolesResult = permissionApi.hasAnyRoles(getLoginUserId(), roles);
hasAnyRolesResult.checkError();
return hasAnyRolesResult.getData();
}
@Override

View File

@ -1,8 +1,12 @@
package cn.iocoder.yudao.module.system.api.permission;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import cn.iocoder.yudao.module.system.enums.ApiConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -16,44 +20,32 @@ public interface PermissionApi {
String PREFIX = ApiConstants.PREFIX + "/permission";
/**
* 获得拥有多个角色的用户编号集合
*
* @param roleIds 角色编号集合
* @return 用户编号集合
*/
@GetMapping(PREFIX + "/user-role-id-list-by-role-id")
Set<Long> getUserRoleIdListByRoleIds(Collection<Long> roleIds);
@ApiOperation("获得拥有多个角色的用户编号集合")
@ApiImplicitParam(name = "roleIds", value = "角色编号集合", required = true, allowMultiple = true)
CommonResult<Set<Long>> getUserRoleIdListByRoleIds(@RequestParam("roleIds") Collection<Long> roleIds);
/**
* 判断是否有权限任一一个即可
*
* @param userId 用户编号
* @param permissions 权限
* @return 是否
*/
@GetMapping(PREFIX + "/has-any-permissions")
boolean hasAnyPermissions(@RequestParam("userId") Long userId,
@RequestParam("permissions") String... permissions);
@ApiOperation("判断是否有权限,任一一个即可")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataTypeClass = Long.class),
@ApiImplicitParam(name = "permissions", value = "权限", required = true, allowMultiple = true)
})
CommonResult<Boolean> hasAnyPermissions(@RequestParam("userId") Long userId,
@RequestParam("permissions") String... permissions);
/**
* 判断是否有角色任一一个即可
*
* @param userId 用户编号
* @param roles 角色数组
* @return 是否
*/
@GetMapping(PREFIX + "/has-any-roles")
boolean hasAnyRoles(@RequestParam("userId") Long userId,
@RequestParam("roles") String... roles);
@ApiOperation("判断是否有角色,任一一个即可")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataTypeClass = Long.class),
@ApiImplicitParam(name = "roles", value = "角色数组", required = true, allowMultiple = true)
})
CommonResult<Boolean> hasAnyRoles(@RequestParam("userId") Long userId,
@RequestParam("roles") String... roles);
/**
* 获得登陆用户的部门数据权限
*
* @param userId 用户编号
* @return 部门数据权限
*/
@GetMapping(PREFIX + "/get-dept-data-permission")
DeptDataPermissionRespDTO getDeptDataPermission(@RequestParam("userId") Long userId);
@ApiOperation("获得登陆用户的部门数据权限")
@ApiImplicitParam(name = "userId", value = "部门数据权限", required = true, dataTypeClass = Long.class)
CommonResult<DeptDataPermissionRespDTO> getDeptDataPermission(@RequestParam("userId") Long userId);
}

View File

@ -1,21 +1,23 @@
package cn.iocoder.yudao.module.system.api.permission;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.enums.ApiConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Collection;
/**
* 角色 API 接口
*
* @author 芋道源码
*/
@FeignClient(name = ApiConstants.NAME) // TODO 芋艿fallbackFactory =
@Api(tags = "RPC 服务 - 角色")
public interface RoleApi {
/**
* 校验角色们是否有效如下情况视为无效
* 1. 角色编号不存在
* 2. 角色被禁用
*
* @param ids 角色编号数组
*/
void validRoles(Collection<Long> ids);
String PREFIX = ApiConstants.PREFIX + "/role";
@GetMapping(PREFIX + "/valid")
@ApiImplicitParam(name = "ids", value = "角色编号数组", required = true, allowMultiple = true)
CommonResult<Boolean> validRoles(@RequestParam("ids") Collection<Long> ids);
}

View File

@ -1,9 +1,9 @@
package cn.iocoder.yudao.module.system.api.permission;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
@ -11,13 +11,9 @@ import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.*;
import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION;
/**
* 权限 API 实现类
*
* @author 芋道源码
*/
@RestController // 提供 RESTful API 接口 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口 Dubbo Consumer 调用
@Validated
@ -27,23 +23,23 @@ public class PermissionApiImpl implements PermissionApi {
private PermissionService permissionService;
@Override
public Set<Long> getUserRoleIdListByRoleIds(Collection<Long> roleIds) {
return permissionService.getUserRoleIdListByRoleIds(roleIds);
public CommonResult<Set<Long>> getUserRoleIdListByRoleIds(Collection<Long> roleIds) {
return success(permissionService.getUserRoleIdListByRoleIds(roleIds));
}
@Override
public boolean hasAnyPermissions(Long userId, String... permissions) {
return permissionService.hasAnyPermissions(userId, permissions);
public CommonResult<Boolean> hasAnyPermissions(Long userId, String... permissions) {
return success(permissionService.hasAnyPermissions(userId, permissions));
}
@Override
public boolean hasAnyRoles(Long userId, String... roles) {
return permissionService.hasAnyRoles(userId, roles);
public CommonResult<Boolean> hasAnyRoles(Long userId, String... roles) {
return success(permissionService.hasAnyRoles(userId, roles));
}
@Override
public DeptDataPermissionRespDTO getDeptDataPermission(Long userId) {
return permissionService.getDeptDataPermission(userId);
public CommonResult<DeptDataPermissionRespDTO> getDeptDataPermission(Long userId) {
return success(permissionService.getDeptDataPermission(userId));
}
}

View File

@ -1,24 +1,29 @@
package cn.iocoder.yudao.module.system.api.permission;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Collection;
/**
* 角色 API 实现类
*
* @author 芋道源码
*/
@Service
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION;
@RestController // 提供 RESTful API 接口 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口 Dubbo Consumer 调用
@Validated
public class RoleApiImpl implements RoleApi {
@Resource
private RoleService roleService;
@Override
public void validRoles(Collection<Long> ids) {
public CommonResult<Boolean> validRoles(Collection<Long> ids) {
roleService.validRoles(ids);
return success(true);
}
}