|
|
@@ -2,14 +2,17 @@ package org.dromara.common.mybatis.helper;
|
|
|
|
|
|
import cn.dev33.satoken.context.SaHolder;
|
|
|
import cn.dev33.satoken.context.model.SaStorage;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
|
|
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
|
|
import lombok.AccessLevel;
|
|
|
import lombok.NoArgsConstructor;
|
|
|
+import org.dromara.common.core.utils.reflect.ReflectUtils;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Stack;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
/**
|
|
|
@@ -22,7 +25,9 @@ import java.util.function.Supplier;
|
|
|
@SuppressWarnings("unchecked cast")
|
|
|
public class DataPermissionHelper {
|
|
|
|
|
|
- public static final String DATA_PERMISSION_KEY = "data:permission";
|
|
|
+ private static final String DATA_PERMISSION_KEY = "data:permission";
|
|
|
+
|
|
|
+ private static final ThreadLocal<Stack<Integer>> REENTRANT_IGNORE = ThreadLocal.withInitial(Stack::new);
|
|
|
|
|
|
/**
|
|
|
* 从上下文中获取指定键的变量值,并将其转换为指定的类型
|
|
|
@@ -66,23 +71,54 @@ public class DataPermissionHelper {
|
|
|
throw new NullPointerException("data permission context type exception");
|
|
|
}
|
|
|
|
|
|
+ private static IgnoreStrategy getIgnoreStrategy() {
|
|
|
+ Object ignoreStrategyLocal = ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
|
|
|
+ if (ignoreStrategyLocal instanceof ThreadLocal<?> IGNORE_STRATEGY_LOCAL) {
|
|
|
+ if (IGNORE_STRATEGY_LOCAL.get() instanceof IgnoreStrategy ignoreStrategy) {
|
|
|
+ return ignoreStrategy;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 开启忽略数据权限(开启后需手动调用 {@link #disableIgnore()} 关闭)
|
|
|
*/
|
|
|
public static void enableIgnore() {
|
|
|
- InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build());
|
|
|
+ IgnoreStrategy ignoreStrategy = getIgnoreStrategy();
|
|
|
+ if (ObjectUtil.isNull(ignoreStrategy)) {
|
|
|
+ InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build());
|
|
|
+ } else {
|
|
|
+ ignoreStrategy.setDataPermission(true);
|
|
|
+ }
|
|
|
+ Stack<Integer> reentrantStack = REENTRANT_IGNORE.get();
|
|
|
+ reentrantStack.push(reentrantStack.size() + 1);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 关闭忽略数据权限
|
|
|
*/
|
|
|
public static void disableIgnore() {
|
|
|
- InterceptorIgnoreHelper.clearIgnoreStrategy();
|
|
|
+ IgnoreStrategy ignoreStrategy = getIgnoreStrategy();
|
|
|
+ if (ObjectUtil.isNotNull(ignoreStrategy)) {
|
|
|
+ boolean noOtherIgnoreStrategy = !Boolean.TRUE.equals(ignoreStrategy.getDynamicTableName())
|
|
|
+ && !Boolean.TRUE.equals(ignoreStrategy.getBlockAttack())
|
|
|
+ && !Boolean.TRUE.equals(ignoreStrategy.getIllegalSql())
|
|
|
+ && !Boolean.TRUE.equals(ignoreStrategy.getTenantLine())
|
|
|
+ && CollectionUtil.isEmpty(ignoreStrategy.getOthers());
|
|
|
+ Stack<Integer> reentrantStack = REENTRANT_IGNORE.get();
|
|
|
+ boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1;
|
|
|
+ if (noOtherIgnoreStrategy && empty) {
|
|
|
+ InterceptorIgnoreHelper.clearIgnoreStrategy();
|
|
|
+ } else if (empty) {
|
|
|
+ ignoreStrategy.setDataPermission(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在忽略数据权限中执行
|
|
|
- * <p>禁止在忽略数据权限中执行忽略数据权限</p>
|
|
|
*
|
|
|
* @param handle 处理执行方法
|
|
|
*/
|
|
|
@@ -97,7 +133,6 @@ public class DataPermissionHelper {
|
|
|
|
|
|
/**
|
|
|
* 在忽略数据权限中执行
|
|
|
- * <p>禁止在忽略数据权限中执行忽略数据权限</p>
|
|
|
*
|
|
|
* @param handle 处理执行方法
|
|
|
*/
|