【8.3.3】【datascope】更新数据范围,从新整理DataScopeConfig类

dev-8.3.3
stylefeng 2025-01-25 22:40:01 +08:00
parent fe5e2286ab
commit 36f586f092
4 changed files with 147 additions and 62 deletions

View File

@ -1,10 +1,9 @@
package cn.stylefeng.roses.kernel.db.mp.datascope.config;
import cn.stylefeng.roses.kernel.db.mp.datascope.ProjectDataScopeHandler;
import cn.stylefeng.roses.kernel.rule.enums.permission.DataScopeTypeEnum;
import lombok.Data;
import java.util.List;
import java.util.Set;
/**
*
@ -16,38 +15,34 @@ import java.util.List;
public class DataScopeConfig {
/**
*
*
* <p>
*
*/
private DataScopeTypeEnum dataScopeType;
private boolean totalDataScope = false;
/**
* id
* id
* <p>
* id
*/
private Long userId;
/**
* id
*/
private Long userDeptId;
/**
* id
*/
private Long userCompanyId;
/**
* ID DEFINE 使
*/
private List<Long> specificOrgIds;
/**
*
*/
private String orgIdFieldName = ProjectDataScopeHandler.DEFAULT_ORG_ID_FIELD_NAME;
/**
*
*/
private String userIdFieldName = ProjectDataScopeHandler.DEFAULT_USER_ID_FIELD_NAME;
/**
* id
* <p>
*
*/
private Set<Long> userOrgIdList;
/**
*
*/
private String orgIdFieldName = ProjectDataScopeHandler.DEFAULT_ORG_ID_FIELD_NAME;
}

View File

@ -88,4 +88,12 @@ public interface SysRoleDataScopeService extends IService<SysRoleDataScope> {
*/
Set<Long> getRoleBindOrgIdList(List<Long> roleIdList);
/**
*
*
* @author fengshuonan
* @since 2025/1/25 21:22
*/
List<SysRoleDataScope> getRoleDataScopeList(List<Long> roleIdList);
}

View File

@ -194,6 +194,13 @@ public class SysRoleDataScopeServiceImpl extends ServiceImpl<SysRoleDataScopeMap
return new HashSet<>();
}
@Override
public List<SysRoleDataScope> getRoleDataScopeList(List<Long> roleIdList) {
LambdaQueryWrapper<SysRoleDataScope> wrapper = new LambdaQueryWrapper<>();
wrapper.in(SysRoleDataScope::getRoleId, roleIdList);
return this.list(wrapper);
}
@Override
public void validateHaveRoleBind(Set<Long> beRemovedRoleIdList) {
// none

View File

@ -1,21 +1,24 @@
package cn.stylefeng.roses.kernel.sys.modular.role.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.DbOperatorApi;
import cn.stylefeng.roses.kernel.db.mp.datascope.ProjectDataScopeHandler;
import cn.stylefeng.roses.kernel.db.mp.datascope.UserRoleDataScopeApi;
import cn.stylefeng.roses.kernel.db.mp.datascope.config.DataScopeConfig;
import cn.stylefeng.roses.kernel.rule.enums.permission.DataScopeTypeEnum;
import cn.stylefeng.roses.kernel.sys.api.OrganizationServiceApi;
import cn.stylefeng.roses.kernel.sys.api.SysUserRoleServiceApi;
import cn.stylefeng.roses.kernel.sys.modular.role.entity.SysRole;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.CompanyDeptDTO;
import cn.stylefeng.roses.kernel.sys.modular.role.entity.SysRoleDataScope;
import cn.stylefeng.roses.kernel.sys.modular.role.service.SysRoleDataScopeService;
import cn.stylefeng.roses.kernel.sys.modular.role.service.SysRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -40,13 +43,16 @@ public class UserRoleDataScopeImpl implements UserRoleDataScopeApi {
@Resource
private DbOperatorApi dbOperatorApi;
@Resource
private OrganizationServiceApi organizationServiceApi;
@Override
public DataScopeConfig getUserRoleDataScopeConfig() {
// 获取当前登录用户id
LoginUser loginUser = LoginContext.me().getLoginUserNullable();
if (loginUser == null) {
return null;
return nullDataScopeConfig();
}
// 获取用户的角色id集合
@ -58,43 +64,112 @@ public class UserRoleDataScopeImpl implements UserRoleDataScopeApi {
userRoleIdList.remove(defaultRoleId);
}
// 获取这些角色对应的【最高】的数据范围取数据范围10-50最大的数字
Integer maxDataScope = 0;
Long finalRoleId = null;
List<SysRole> roleDataScopeType = sysRoleService.getRoleDataScopeType(userRoleIdList);
for (SysRole sysRole : roleDataScopeType) {
if (sysRole.getDataScopeType() > maxDataScope) {
maxDataScope = sysRole.getDataScopeType();
finalRoleId = sysRole.getRoleId();
}
// 获取这些角色的所有的数据范围信息的汇总
List<SysRoleDataScope> roleDataScopeList = sysRoleDataScopeService.getRoleDataScopeList(userRoleIdList);
if (ObjectUtil.isEmpty(roleDataScopeList)) {
return nullDataScopeConfig();
}
// 通过这些角色的数据范围信息,构建出数据范围配置
return parseSysRoleDataScope(roleDataScopeList, loginUser);
}
/**
*
*
* @author fengshuonan
* @since 2025/1/25 21:59
*/
private DataScopeConfig nullDataScopeConfig() {
DataScopeConfig dataScopeConfig = new DataScopeConfig();
// 设置数据最终的数据范围
dataScopeConfig.setDataScopeType(DataScopeTypeEnum.codeToEnum(maxDataScope));
// 如果数据范围是指定部门,则需要单独查下这个角色对应的部门数据有哪些
if (DataScopeTypeEnum.DEFINE.getCode().equals(maxDataScope)) {
Set<Long> roleBindOrgIdList = sysRoleDataScopeService.getRoleBindOrgIdList(ListUtil.list(false, finalRoleId));
dataScopeConfig.setSpecificOrgIds(new ArrayList<>(roleBindOrgIdList));
}
// 设置用户id
dataScopeConfig.setUserId(loginUser.getUserId());
// 如果数据范围是本公司及以下则查询当前用户的公司id
if (DataScopeTypeEnum.COMPANY_WITH_CHILD.getCode().equals(maxDataScope)) {
Long currentUserCompanyId = LoginContext.me().getCurrentUserCompanyId();
dataScopeConfig.setUserCompanyId(currentUserCompanyId);
}
// 如果是本部门数据或者本部门及以下需要查询当前用户的部门id
if (DataScopeTypeEnum.DEPT.getCode().equals(maxDataScope) || DataScopeTypeEnum.DEPT_WITH_CHILD.getCode().equals(maxDataScope)) {
dataScopeConfig.setUserDeptId(loginUser.getCurrentOrgId());
}
dataScopeConfig.setUserId(ProjectDataScopeHandler.NONE_ID_VALUE);
dataScopeConfig.setUserOrgIdList(CollectionUtil.set(false, ProjectDataScopeHandler.NONE_ID_VALUE));
return dataScopeConfig;
}
/**
*
*
* @author fengshuonan
* @since 2025/1/25 22:01
*/
private DataScopeConfig parseSysRoleDataScope(List<SysRoleDataScope> sysRoleDataScopeList, LoginUser loginUser) {
DataScopeConfig dataScopeConfig = new DataScopeConfig();
// 如果包含了全部数据,则将结果设置为全部数据,并直接返回结果
for (SysRoleDataScope sysRoleDataScope : sysRoleDataScopeList) {
if (DataScopeTypeEnum.ALL.getCode().equals(sysRoleDataScope.getDataScopeType())) {
dataScopeConfig.setTotalDataScope(true);
return dataScopeConfig;
}
}
// 如果有仅包含自己的数据,则将结果设置为仅包含自己的数据
for (SysRoleDataScope sysRoleDataScope : sysRoleDataScopeList) {
if (DataScopeTypeEnum.SELF.getCode().equals(sysRoleDataScope.getDataScopeType())) {
dataScopeConfig.setUserId(loginUser.getUserId());
}
}
// 开始处理除了仅本人、全部数据外的数据范围
Set<Long> userOrgIdList = new HashSet<>();
for (SysRoleDataScope sysRoleDataScope : sysRoleDataScopeList) {
// 如果是20-本部门数据
if (DataScopeTypeEnum.DEPT.getCode().equals(sysRoleDataScope.getDataScopeType())) {
userOrgIdList.add(loginUser.getCurrentOrgId());
}
// 如果是30-本部门及以下数据
else if (DataScopeTypeEnum.DEPT_WITH_CHILD.getCode().equals(sysRoleDataScope.getDataScopeType())) {
Set<Long> pointOrgAndSub = this.getPointOrgAndSub(loginUser.getCurrentOrgId());
userOrgIdList.addAll(pointOrgAndSub);
}
// 如果是31-本公司及以下数据
else if (DataScopeTypeEnum.COMPANY_WITH_CHILD.getCode().equals(sysRoleDataScope.getDataScopeType())) {
// 获取当前部门的公司id
CompanyDeptDTO companyDeptInfo = organizationServiceApi.getCompanyDeptInfo(loginUser.getCurrentOrgId());
if (companyDeptInfo == null) {
continue;
}
Long companyId = companyDeptInfo.getCompanyId();
Set<Long> pointOrgAndSub = this.getPointOrgAndSub(companyId);
userOrgIdList.addAll(pointOrgAndSub);
}
// 如果是32-指定机构层级及以下
else if (DataScopeTypeEnum.DEFINE_ORG_LEVEL_WITH_CHILD.getCode().equals(sysRoleDataScope.getDataScopeType())) {
// todo
}
// 如果是40-指定机构集合数据
else if (DataScopeTypeEnum.DEFINE.getCode().equals(sysRoleDataScope.getDataScopeType())) {
userOrgIdList.addAll(sysRoleDataScope.getDefineOrgList());
}
// 如果是41-指定机构及以下
else if (DataScopeTypeEnum.DEFINE_ORG_WITH_CHILD.getCode().equals(sysRoleDataScope.getDataScopeType())) {
Long defineOrgId = sysRoleDataScope.getDefineOrgId();
Set<Long> pointOrgAndSub = this.getPointOrgAndSub(defineOrgId);
userOrgIdList.addAll(pointOrgAndSub);
}
}
dataScopeConfig.setUserOrgIdList(userOrgIdList);
return dataScopeConfig;
}
/**
* id
*
* @author fengshuonan
* @since 2025/1/25 22:24
*/
public Set<Long> getPointOrgAndSub(Long orgId) {
Set<Long> subOrgIdList = this.dbOperatorApi.findSubListByParentId("sys_hr_organization", "org_pids", "org_id", orgId);
subOrgIdList.add(orgId);
return subOrgIdList;
}
}