|
|
|
@ -1,9 +1,12 @@
|
|
|
|
|
package org.dromara.common.mybatis.interceptor;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.ConcurrentHashSet;
|
|
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
|
|
|
import org.dromara.common.mybatis.annotation.DataColumn;
|
|
|
|
|
import org.dromara.common.mybatis.handler.PlusDataPermissionHandler;
|
|
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
|
|
import net.sf.jsqlparser.statement.delete.Delete;
|
|
|
|
@ -23,6 +26,7 @@ import org.apache.ibatis.session.RowBounds;
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据权限拦截器
|
|
|
|
@ -33,6 +37,10 @@ import java.util.List;
|
|
|
|
|
public class PlusDataPermissionInterceptor extends JsqlParserSupport implements InnerInterceptor {
|
|
|
|
|
|
|
|
|
|
private final PlusDataPermissionHandler dataPermissionHandler = new PlusDataPermissionHandler();
|
|
|
|
|
/**
|
|
|
|
|
* 无效注解方法缓存用于快速返回
|
|
|
|
|
*/
|
|
|
|
|
private final Set<String> invalidCacheSet = new ConcurrentHashSet<>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
|
|
@ -41,7 +49,12 @@ public class PlusDataPermissionInterceptor extends JsqlParserSupport implements
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 检查是否无效 无数据权限注解
|
|
|
|
|
if (dataPermissionHandler.isInvalid(ms.getId())) {
|
|
|
|
|
if (invalidCacheSet.contains(ms.getId())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DataColumn[] dataColumns = dataPermissionHandler.findAnnotation(ms.getId());
|
|
|
|
|
if (ArrayUtil.isEmpty(dataColumns)) {
|
|
|
|
|
invalidCacheSet.add(ms.getId());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 解析 sql 分配对应方法
|
|
|
|
@ -58,6 +71,10 @@ public class PlusDataPermissionInterceptor extends JsqlParserSupport implements
|
|
|
|
|
if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 检查是否无效 无数据权限注解
|
|
|
|
|
if (invalidCacheSet.contains(ms.getId())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
|
|
|
|
|
mpBs.sql(parserMulti(mpBs.sql(), ms.getId()));
|
|
|
|
|
}
|
|
|
|
|