mirror of https://gitee.com/stylefeng/roses
【8.3.3】【dataScope】更新表达式的拼接
parent
cc07ef0064
commit
7e50b7d650
|
@ -1,25 +1,17 @@
|
||||||
package cn.stylefeng.roses.kernel.db.mp.datascope;
|
package cn.stylefeng.roses.kernel.db.mp.datascope;
|
||||||
|
|
||||||
import cn.hutool.core.collection.ListUtil;
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.stylefeng.roses.kernel.db.mp.datascope.config.DataScopeConfig;
|
import cn.stylefeng.roses.kernel.db.mp.datascope.config.DataScopeConfig;
|
||||||
import cn.stylefeng.roses.kernel.db.mp.datascope.holder.DataScopeHolder;
|
import cn.stylefeng.roses.kernel.db.mp.datascope.holder.DataScopeHolder;
|
||||||
import cn.stylefeng.roses.kernel.rule.enums.permission.DataScopeTypeEnum;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.handler.MultiDataPermissionHandler;
|
import com.baomidou.mybatisplus.extension.plugins.handler.MultiDataPermissionHandler;
|
||||||
import net.sf.jsqlparser.expression.Expression;
|
import net.sf.jsqlparser.expression.Expression;
|
||||||
import net.sf.jsqlparser.expression.LongValue;
|
import net.sf.jsqlparser.expression.LongValue;
|
||||||
import net.sf.jsqlparser.expression.Parenthesis;
|
import net.sf.jsqlparser.expression.Parenthesis;
|
||||||
import net.sf.jsqlparser.expression.StringValue;
|
|
||||||
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
|
||||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
||||||
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
|
|
||||||
import net.sf.jsqlparser.schema.Column;
|
import net.sf.jsqlparser.schema.Column;
|
||||||
import net.sf.jsqlparser.schema.Table;
|
import net.sf.jsqlparser.schema.Table;
|
||||||
import net.sf.jsqlparser.statement.select.LateralSubSelect;
|
|
||||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
|
||||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
|
||||||
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -55,118 +47,19 @@ public class ProjectDataScopeHandler implements MultiDataPermissionHandler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 数据校验处理
|
// 如果是全部权限,则不校验
|
||||||
dataScopeConfig = this.validateDataScopeConfig(dataScopeConfig);
|
if (dataScopeConfig.isTotalDataScope()) {
|
||||||
if (dataScopeConfig == null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取数据范围的类型
|
// 如果是需要校验仅创建人数据
|
||||||
DataScopeTypeEnum dataScopeTypeEnum = dataScopeConfig.getDataScopeType();
|
if (dataScopeConfig.isDoCreateUserValidate()) {
|
||||||
switch (dataScopeTypeEnum) {
|
|
||||||
|
|
||||||
// 如果是全部数据,返回空,不对sql进行处理
|
|
||||||
case ALL:
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// 如果是本部门数据,则限制查询只能查询本部门数据
|
|
||||||
case DEPT:
|
|
||||||
return getEqualsTo(dataScopeConfig.getOrgIdFieldName(), dataScopeConfig.getUserDeptId());
|
|
||||||
|
|
||||||
// 如果是本部门及以下部门
|
|
||||||
case DEPT_WITH_CHILD:
|
|
||||||
return deptWithChildScope(dataScopeConfig, dataScopeConfig.getUserDeptId());
|
|
||||||
|
|
||||||
// 本公司及以下数据
|
|
||||||
case COMPANY_WITH_CHILD:
|
|
||||||
return deptWithChildScope(dataScopeConfig, dataScopeConfig.getUserCompanyId());
|
|
||||||
|
|
||||||
// 指定部门数据
|
|
||||||
case DEFINE:
|
|
||||||
return getInExpression(dataScopeConfig);
|
|
||||||
|
|
||||||
// 仅本人数据
|
|
||||||
case SELF:
|
|
||||||
return getEqualsTo(dataScopeConfig.getUserIdFieldName(), dataScopeConfig.getUserId());
|
|
||||||
|
|
||||||
// 其他情况
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 校验数据范围配置是否正确
|
|
||||||
*
|
|
||||||
* @author fengshuonan
|
|
||||||
* @since 2024-02-29 11:00
|
|
||||||
*/
|
|
||||||
private DataScopeConfig validateDataScopeConfig(DataScopeConfig dataScopeConfig) {
|
|
||||||
if (dataScopeConfig == null) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DataScopeTypeEnum dataScopeType = dataScopeConfig.getDataScopeType();
|
// 如果是需要校验指定部门数据
|
||||||
if (dataScopeType == null) {
|
if (dataScopeConfig.isDoOrgScopeValidate()) {
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果数据范围为全部,直接返回空,也就是不进行数据范围sql拦截器
|
|
||||||
if (DataScopeTypeEnum.ALL.equals(dataScopeType)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果数据范围是本人,则查询本人id是否传递
|
|
||||||
else if (DataScopeTypeEnum.SELF.equals(dataScopeType)) {
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getUserId())) {
|
|
||||||
dataScopeConfig.setUserId(NONE_ID_VALUE);
|
|
||||||
}
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getUserIdFieldName())) {
|
|
||||||
dataScopeConfig.setUserIdFieldName(DEFAULT_USER_ID_FIELD_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是本公司及以下数据,则查询公司id是否传递
|
|
||||||
else if (DataScopeTypeEnum.COMPANY_WITH_CHILD.equals(dataScopeType)) {
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getUserCompanyId())) {
|
|
||||||
dataScopeConfig.setUserCompanyId(NONE_ID_VALUE);
|
|
||||||
}
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getOrgIdFieldName())) {
|
|
||||||
dataScopeConfig.setOrgIdFieldName(DEFAULT_ORG_ID_FIELD_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是本部门及以下数据,则查询部门id是否传递
|
|
||||||
else if (DataScopeTypeEnum.DEPT_WITH_CHILD.equals(dataScopeType)) {
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getUserDeptId())) {
|
|
||||||
dataScopeConfig.setUserDeptId(NONE_ID_VALUE);
|
|
||||||
}
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getOrgIdFieldName())) {
|
|
||||||
dataScopeConfig.setOrgIdFieldName(DEFAULT_ORG_ID_FIELD_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是本部门数据,则查询部门id是否传递
|
|
||||||
else if (DataScopeTypeEnum.DEPT.equals(dataScopeType)) {
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getUserDeptId())) {
|
|
||||||
dataScopeConfig.setUserDeptId(NONE_ID_VALUE);
|
|
||||||
}
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getOrgIdFieldName())) {
|
|
||||||
dataScopeConfig.setOrgIdFieldName(DEFAULT_ORG_ID_FIELD_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是指定部门数据,则查询部门id是否传递
|
|
||||||
else if (DataScopeTypeEnum.DEFINE.equals(dataScopeType)) {
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getSpecificOrgIds())) {
|
|
||||||
dataScopeConfig.setSpecificOrgIds(ListUtil.list(true, NONE_ID_VALUE));
|
|
||||||
}
|
|
||||||
if (ObjectUtil.isEmpty(dataScopeConfig.getOrgIdFieldName())) {
|
|
||||||
dataScopeConfig.setOrgIdFieldName(DEFAULT_ORG_ID_FIELD_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return dataScopeConfig;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -198,7 +91,7 @@ public class ProjectDataScopeHandler implements MultiDataPermissionHandler {
|
||||||
|
|
||||||
// 创建 IN 表达式的值列表
|
// 创建 IN 表达式的值列表
|
||||||
ExpressionList expressionList = new ExpressionList();
|
ExpressionList expressionList = new ExpressionList();
|
||||||
expressionList.setExpressions(dataScopeConfig.getSpecificOrgIds().stream().map(LongValue::new).collect(Collectors.toList()));
|
expressionList.setExpressions(dataScopeConfig.getUserOrgIdList().stream().map(LongValue::new).collect(Collectors.toList()));
|
||||||
|
|
||||||
// 创建 IN 表达式
|
// 创建 IN 表达式
|
||||||
InExpression inExpression = new InExpression();
|
InExpression inExpression = new InExpression();
|
||||||
|
@ -207,45 +100,4 @@ public class ProjectDataScopeHandler implements MultiDataPermissionHandler {
|
||||||
return inExpression;
|
return inExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取本部门及以下
|
|
||||||
* <p>
|
|
||||||
* 拼接条件最终效果如下:org_id in (select org_id from sys_hr_organization where org_pids like "%[id]%" or org_id = id)
|
|
||||||
*
|
|
||||||
* @author fengshuonan
|
|
||||||
* @since 2024-02-29 20:14
|
|
||||||
*/
|
|
||||||
private static Expression deptWithChildScope(DataScopeConfig dataScopeConfig, Long deptOrCompanyId) {
|
|
||||||
|
|
||||||
// 创建 org_id 列
|
|
||||||
Column orgIdColumn = new Column(dataScopeConfig.getOrgIdFieldName());
|
|
||||||
|
|
||||||
// 创建子查询 select 部分
|
|
||||||
LateralSubSelect subSelect = new LateralSubSelect();
|
|
||||||
subSelect.setPrefix("");
|
|
||||||
|
|
||||||
PlainSelect selectBody = new PlainSelect();
|
|
||||||
selectBody.setSelectItems(ListUtil.of(new SelectItem<>(orgIdColumn)));
|
|
||||||
selectBody.setFromItem(new Table("sys_hr_organization"));
|
|
||||||
|
|
||||||
// 创建 LIKE 表达式
|
|
||||||
LikeExpression likeExpression = new LikeExpression();
|
|
||||||
likeExpression.setLeftExpression(new Column("org_pids"));
|
|
||||||
likeExpression.setRightExpression(new StringValue("%[" + deptOrCompanyId + "]%"));
|
|
||||||
|
|
||||||
// 设置等于表达式
|
|
||||||
EqualsTo equalsTo = new EqualsTo();
|
|
||||||
equalsTo.setLeftExpression(orgIdColumn);
|
|
||||||
equalsTo.setRightExpression(new LongValue(deptOrCompanyId));
|
|
||||||
|
|
||||||
// 创建 OR 表达式
|
|
||||||
OrExpression orExpression = new OrExpression(likeExpression, equalsTo);
|
|
||||||
|
|
||||||
// 设置子查询的 WHERE 条件
|
|
||||||
selectBody.setWhere(orExpression);
|
|
||||||
subSelect.setSelect(selectBody);
|
|
||||||
|
|
||||||
// 创建 IN 表达式
|
|
||||||
return new InExpression(orgIdColumn, subSelect);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue