mirror of https://gitee.com/stylefeng/roses
【8.3.3】【dataScope】调整用户数据范围的筛选
parent
8f94db520d
commit
79da038b11
|
@ -3,15 +3,12 @@ package cn.stylefeng.roses.kernel.sys.modular.user.factory;
|
|||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.stylefeng.roses.kernel.db.api.DbOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.db.mp.datascope.UserRoleDataScopeApi;
|
||||
import cn.stylefeng.roses.kernel.db.mp.datascope.config.DataScopeConfig;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUser;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserOrgService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -32,90 +29,28 @@ public class UserDataScopeFactory {
|
|||
|
||||
UserRoleDataScopeApi userRoleDataScopeApi = SpringUtil.getBean(UserRoleDataScopeApi.class);
|
||||
SysUserOrgService sysUserOrgService = SpringUtil.getBean(SysUserOrgService.class);
|
||||
DbOperatorApi dbOperatorApi = SpringUtil.getBean(DbOperatorApi.class);
|
||||
|
||||
|
||||
// 获取当前用户的数据范围
|
||||
DataScopeConfig userRoleDataScopeConfig = userRoleDataScopeApi.getUserRoleDataScopeConfig();
|
||||
|
||||
switch (userRoleDataScopeConfig.getDataScopeType()) {
|
||||
// 如果是本人数据
|
||||
case SELF:
|
||||
queryWrapper.eq(SysUser::getUserId, userRoleDataScopeConfig.getUserId());
|
||||
break;
|
||||
|
||||
// 如果是本部门数据
|
||||
case DEPT:
|
||||
|
||||
// 获取本部门下的所有用户id
|
||||
Set<Long> deptUserIdList = sysUserOrgService.getOrgUserIdList(CollectionUtil.set(false, userRoleDataScopeConfig.getUserDeptId()));
|
||||
if (ObjectUtil.isEmpty(deptUserIdList)) {
|
||||
deptUserIdList = CollectionUtil.set(false, -1L);
|
||||
}
|
||||
queryWrapper.in(SysUser::getUserId, deptUserIdList);
|
||||
break;
|
||||
|
||||
// 如果是本部门及有以下部门数据
|
||||
case DEPT_WITH_CHILD:
|
||||
|
||||
// 获取本部门及以下部门有哪些部门
|
||||
Set<Long> subDeptOrgIdList = dbOperatorApi.findSubListByParentId("sys_hr_organization", "org_pids", "org_id", userRoleDataScopeConfig.getUserDeptId());
|
||||
if (ObjectUtil.isEmpty(subDeptOrgIdList)) {
|
||||
subDeptOrgIdList = new HashSet<>();
|
||||
}
|
||||
subDeptOrgIdList.add(userRoleDataScopeConfig.getUserDeptId());
|
||||
|
||||
// 获取部门下的用户
|
||||
Set<Long> subDeptOrgUserIdList = sysUserOrgService.getOrgUserIdList(subDeptOrgIdList);
|
||||
if (ObjectUtil.isEmpty(subDeptOrgUserIdList)) {
|
||||
subDeptOrgUserIdList = CollectionUtil.set(false, -1L);
|
||||
}
|
||||
|
||||
queryWrapper.in(SysUser::getUserId, subDeptOrgUserIdList);
|
||||
break;
|
||||
|
||||
// 如果是本公司及以下部门数据
|
||||
case COMPANY_WITH_CHILD:
|
||||
|
||||
// 获取本部门及以下部门有哪些部门
|
||||
Set<Long> subCompanyOrgIdList = dbOperatorApi.findSubListByParentId("sys_hr_organization", "org_pids", "org_id", userRoleDataScopeConfig.getUserCompanyId());
|
||||
if (ObjectUtil.isEmpty(subCompanyOrgIdList)) {
|
||||
subCompanyOrgIdList = new HashSet<>();
|
||||
}
|
||||
subCompanyOrgIdList.add(userRoleDataScopeConfig.getUserCompanyId());
|
||||
|
||||
// 获取部门下的用户
|
||||
Set<Long> subCompanyUserIdList = sysUserOrgService.getOrgUserIdList(subCompanyOrgIdList);
|
||||
if (ObjectUtil.isEmpty(subCompanyUserIdList)) {
|
||||
subCompanyUserIdList = CollectionUtil.set(false, -1L);
|
||||
}
|
||||
|
||||
queryWrapper.in(SysUser::getUserId, subCompanyUserIdList);
|
||||
|
||||
break;
|
||||
|
||||
// 如果是指定部门数据
|
||||
case DEFINE:
|
||||
|
||||
// 获取指定部门下的用户列表
|
||||
List<Long> specificOrgIds = userRoleDataScopeConfig.getSpecificOrgIds();
|
||||
if (ObjectUtil.isEmpty(specificOrgIds)) {
|
||||
specificOrgIds = CollectionUtil.list(false, -1L);
|
||||
}
|
||||
|
||||
Set<Long> specificOrgUserIdList = sysUserOrgService.getOrgUserIdList(new HashSet<>(specificOrgIds));
|
||||
if (ObjectUtil.isEmpty(specificOrgUserIdList)) {
|
||||
specificOrgUserIdList = CollectionUtil.set(false, -1L);
|
||||
}
|
||||
|
||||
queryWrapper.in(SysUser::getUserId, specificOrgUserIdList);
|
||||
break;
|
||||
|
||||
// 如果是全部数据
|
||||
case ALL:
|
||||
break;
|
||||
// 如果是获取全部数据,则不限制
|
||||
if (userRoleDataScopeConfig.isTotalDataScope()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果限制了查询当前人
|
||||
if (userRoleDataScopeConfig.isDoCreateUserValidate()) {
|
||||
queryWrapper.eq(SysUser::getUserId, userRoleDataScopeConfig.getUserId());
|
||||
}
|
||||
|
||||
// 如果限制了查询指定部门
|
||||
if (userRoleDataScopeConfig.isDoOrgScopeValidate()) {
|
||||
Set<Long> specificOrgUserIdList = sysUserOrgService.getOrgUserIdList(userRoleDataScopeConfig.getUserOrgIdList());
|
||||
if (ObjectUtil.isEmpty(specificOrgUserIdList)) {
|
||||
specificOrgUserIdList = CollectionUtil.set(false, -1L);
|
||||
}
|
||||
queryWrapper.in(SysUser::getUserId, specificOrgUserIdList);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue