From 9d4c5511b6455ecc681fd11bded0eec3235fc068 Mon Sep 17 00:00:00 2001 From: ye_star <17705172417@189.cn> Date: Tue, 21 Feb 2023 21:28:39 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=907.3.3=E3=80=91=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=8C=83=E5=9B=B4:=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E7=94=A8=E6=88=B7id=E9=9B=86=E5=90=88,=E5=B9=B6=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=80=9A=E8=BF=87=E7=94=A8=E6=88=B7id=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roses/kernel/system/api/DataScopeApi.java | 7 +++++ .../kernel/system/api/SysUserRoleApi.java | 19 ++++++++++++ .../kernel/system/api/UserOrgServiceApi.java | 9 ++++++ .../service/DataScopeService.java | 29 +++++++++++++++---- .../user/service/SysUserRoleService.java | 3 +- .../impl/SysUserOrgServiceServiceImpl.java | 9 ++++++ .../service/impl/SysUserRoleServiceImpl.java | 1 - 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/SysUserRoleApi.java diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/DataScopeApi.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/DataScopeApi.java index c978500d0..f94da992d 100644 --- a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/DataScopeApi.java +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/DataScopeApi.java @@ -52,4 +52,11 @@ public interface DataScopeApi { */ DataScopeDTO getDataScope(Long userId, List sysRoles); + /** + * 获取用户的数据范围 + * @param userId 用户id + * @return 数据范围内容 + */ + DataScopeDTO getDataScope(Long userId); + } diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/SysUserRoleApi.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/SysUserRoleApi.java new file mode 100644 index 000000000..f2025a376 --- /dev/null +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/SysUserRoleApi.java @@ -0,0 +1,19 @@ +package cn.stylefeng.roses.kernel.system.api; + +import java.util.List; + +/** + * @Author: yx + * @Date: 2022-12-29 13:27 + */ +public interface SysUserRoleApi { + /** + * 根据userId查询角色集合 + * + * @param userId 用户id + * @return 用户角色集合 + * @author chenjinlong + * @date 2021/2/3 15:09 + */ + List findRoleIdsByUserId(Long userId); +} diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/UserOrgServiceApi.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/UserOrgServiceApi.java index 90b6e86c6..d24236970 100644 --- a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/UserOrgServiceApi.java +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/UserOrgServiceApi.java @@ -26,6 +26,8 @@ package cn.stylefeng.roses.kernel.system.api; import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserOrgDTO; +import java.util.Set; + /** * 用户组织机构服务api * @@ -51,4 +53,11 @@ public interface UserOrgServiceApi { */ SysUserOrgDTO getUserOrgByUserId(Long userId); + /** + * 获取组织下用户id + * + * @author yx + * @date 2020/12/19 22:33 + */ + Set getUserIdsByOrgIds(Set organizationIds); } diff --git a/kernel-s-system/system-business-organization/src/main/java/cn/stylefeng/roses/kernel/system/modular/organization/service/DataScopeService.java b/kernel-s-system/system-business-organization/src/main/java/cn/stylefeng/roses/kernel/system/modular/organization/service/DataScopeService.java index a584ec824..4c2d587c6 100644 --- a/kernel-s-system/system-business-organization/src/main/java/cn/stylefeng/roses/kernel/system/modular/organization/service/DataScopeService.java +++ b/kernel-s-system/system-business-organization/src/main/java/cn/stylefeng/roses/kernel/system/modular/organization/service/DataScopeService.java @@ -24,12 +24,12 @@ */ package cn.stylefeng.roses.kernel.system.modular.organization.service; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.ObjectUtil; import cn.stylefeng.roses.kernel.auth.api.enums.DataScopeTypeEnum; +import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum; import cn.stylefeng.roses.kernel.db.api.DbOperatorApi; -import cn.stylefeng.roses.kernel.system.api.DataScopeApi; -import cn.stylefeng.roses.kernel.system.api.RoleServiceApi; -import cn.stylefeng.roses.kernel.system.api.UserOrgServiceApi; -import cn.stylefeng.roses.kernel.system.api.UserServiceApi; +import cn.stylefeng.roses.kernel.system.api.*; import cn.stylefeng.roses.kernel.system.api.exception.SystemModularException; import cn.stylefeng.roses.kernel.system.api.exception.enums.organization.DataScopeExceptionEnum; import cn.stylefeng.roses.kernel.system.api.pojo.organization.DataScopeDTO; @@ -64,6 +64,10 @@ public class DataScopeService implements DataScopeApi { @Resource private DbOperatorApi dbOperatorApi; + @Resource + private SysUserRoleApi sysUserRoleApi; + + @Override public DataScopeDTO getDataScope(Long userId, List sysRoles) { @@ -125,11 +129,26 @@ public class DataScopeService implements DataScopeApi { List userBindDataScope = userServiceApi.getUserBindDataScope(userId); organizationIds.addAll(userBindDataScope); - // 3. 组装返回结果 + // 3. 如果userIds为空 organizationIds不为空 则userIds 为organizationIds下所有用户id + if (CollectionUtil.isEmpty(userIds) && CollectionUtil.isNotEmpty(organizationIds)) { + userIds.addAll(userOrgServiceApi.getUserIdsByOrgIds(organizationIds)); + } + + // 4. 组装返回结果 dataScopeResponse.setUserIds(userIds); dataScopeResponse.setOrganizationIds(organizationIds); return dataScopeResponse; } + @Override + public DataScopeDTO getDataScope(Long userId) { + // 2. 获取用户角色信息 + List roleIds = sysUserRoleApi.findRoleIdsByUserId(userId); + if (ObjectUtil.isEmpty(roleIds)) { + throw new SystemModularException(AuthExceptionEnum.ROLE_IS_EMPTY); + } + List roleResponseList = roleServiceApi.getRolesByIds(roleIds); + return this.getDataScope(userId, roleResponseList); + } } diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/SysUserRoleService.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/SysUserRoleService.java index 9e5488b35..5759f19a5 100644 --- a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/SysUserRoleService.java +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/SysUserRoleService.java @@ -24,6 +24,7 @@ */ package cn.stylefeng.roses.kernel.system.modular.user.service; +import cn.stylefeng.roses.kernel.system.api.SysUserRoleApi; import cn.stylefeng.roses.kernel.system.api.pojo.user.request.SysUserRequest; import cn.stylefeng.roses.kernel.system.api.pojo.user.request.UserRoleRequest; import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUserRole; @@ -37,7 +38,7 @@ import java.util.List; * @author chenjinlong * @date 2021/2/3 15:23 */ -public interface SysUserRoleService extends IService { +public interface SysUserRoleService extends IService , SysUserRoleApi { /** * 新增 diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserOrgServiceServiceImpl.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserOrgServiceServiceImpl.java index 0001e6f79..24f17a2cf 100644 --- a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserOrgServiceServiceImpl.java +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserOrgServiceServiceImpl.java @@ -41,6 +41,8 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; /** @@ -80,6 +82,13 @@ public class SysUserOrgServiceServiceImpl extends ServiceImpl getUserIdsByOrgIds(Set organizationIds) { + List userOrgs = this.lambdaQuery().in(SysUserOrg::getOrgId, organizationIds).list(); + return userOrgs.stream().map(SysUserOrg::getUserId).collect(Collectors.toSet()); + } + + @Override public void add(UserOrgRequest userOrgResponse) { diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java index 81e503ec5..eb2dd4900 100644 --- a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java @@ -101,7 +101,6 @@ public class SysUserRoleServiceImpl extends ServiceImpl findRoleIdsByUserId(Long userId) { // 先从缓存获取用户绑定的角色