diff --git a/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java b/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java index 7f9a74a61..801db32f4 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java @@ -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 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) 未返回数据权限", diff --git a/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/test/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRuleTest.java b/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/test/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRuleTest.java index a50e2382f..bfbb5f9da 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/test/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRuleTest.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/test/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRuleTest.java @@ -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 字段配置 diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java index 78caadea2..a53fb922b 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java @@ -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 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 hasAnyRolesResult = permissionApi.hasAnyRoles(getLoginUserId(), roles); + hasAnyRolesResult.checkError(); + return hasAnyRolesResult.getData(); } @Override diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApi.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApi.java index a34101496..85914629b 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApi.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApi.java @@ -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 getUserRoleIdListByRoleIds(Collection roleIds); + @ApiOperation("获得拥有多个角色的用户编号集合") + @ApiImplicitParam(name = "roleIds", value = "角色编号集合", required = true, allowMultiple = true) + CommonResult> getUserRoleIdListByRoleIds(@RequestParam("roleIds") Collection 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 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 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 getDeptDataPermission(@RequestParam("userId") Long userId); } diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApi.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApi.java index 14133e9d9..d70eb22c7 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApi.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApi.java @@ -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 ids); + String PREFIX = ApiConstants.PREFIX + "/role"; + + @GetMapping(PREFIX + "/valid") + @ApiImplicitParam(name = "ids", value = "角色编号数组", required = true, allowMultiple = true) + CommonResult validRoles(@RequestParam("ids") Collection ids); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApiImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApiImpl.java index d53c3c4e2..fcdad6f29 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApiImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/PermissionApiImpl.java @@ -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 getUserRoleIdListByRoleIds(Collection roleIds) { - return permissionService.getUserRoleIdListByRoleIds(roleIds); + public CommonResult> getUserRoleIdListByRoleIds(Collection roleIds) { + return success(permissionService.getUserRoleIdListByRoleIds(roleIds)); } @Override - public boolean hasAnyPermissions(Long userId, String... permissions) { - return permissionService.hasAnyPermissions(userId, permissions); + public CommonResult 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 hasAnyRoles(Long userId, String... roles) { + return success(permissionService.hasAnyRoles(userId, roles)); } @Override - public DeptDataPermissionRespDTO getDeptDataPermission(Long userId) { - return permissionService.getDeptDataPermission(userId); + public CommonResult getDeptDataPermission(Long userId) { + return success(permissionService.getDeptDataPermission(userId)); } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApiImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApiImpl.java index 2c4f94737..806f15bbb 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApiImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApiImpl.java @@ -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 ids) { + public CommonResult validRoles(Collection ids) { roleService.validRoles(ids); + return success(true); } }