mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 优化 忽略租户与忽略数据权限支持嵌套使用
This commit is contained in:
		@@ -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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -24,6 +27,8 @@ public class DataPermissionHelper {
 | 
			
		||||
 | 
			
		||||
    private static final String DATA_PERMISSION_KEY = "data:permission";
 | 
			
		||||
 | 
			
		||||
    private static final ThreadLocal<Stack<Integer>> REENTRANT_IGNORE = ThreadLocal.withInitial(Stack::new);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 从上下文中获取指定键的变量值,并将其转换为指定的类型
 | 
			
		||||
     *
 | 
			
		||||
@@ -66,18 +71,50 @@ 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);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,9 @@
 | 
			
		||||
package org.dromara.common.tenant.helper;
 | 
			
		||||
 | 
			
		||||
import cn.dev33.satoken.stp.StpUtil;
 | 
			
		||||
import cn.hutool.core.collection.CollectionUtil;
 | 
			
		||||
import cn.hutool.core.convert.Convert;
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
 | 
			
		||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
 | 
			
		||||
import lombok.AccessLevel;
 | 
			
		||||
@@ -10,9 +12,11 @@ import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.dromara.common.core.constant.GlobalConstants;
 | 
			
		||||
import org.dromara.common.core.utils.SpringUtils;
 | 
			
		||||
import org.dromara.common.core.utils.StringUtils;
 | 
			
		||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
 | 
			
		||||
import org.dromara.common.redis.utils.RedisUtils;
 | 
			
		||||
import org.dromara.common.satoken.utils.LoginHelper;
 | 
			
		||||
 | 
			
		||||
import java.util.Stack;
 | 
			
		||||
import java.util.function.Supplier;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -28,6 +32,8 @@ public class TenantHelper {
 | 
			
		||||
 | 
			
		||||
    private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new ThreadLocal<>();
 | 
			
		||||
 | 
			
		||||
    private static final ThreadLocal<Stack<Integer>> REENTRANT_IGNORE = ThreadLocal.withInitial(Stack::new);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 租户功能是否启用
 | 
			
		||||
     */
 | 
			
		||||
@@ -35,18 +41,49 @@ public class TenantHelper {
 | 
			
		||||
        return Convert.toBool(SpringUtils.getProperty("tenant.enable"), false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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().tenantLine(true).build());
 | 
			
		||||
        IgnoreStrategy ignoreStrategy = getIgnoreStrategy();
 | 
			
		||||
        if (ObjectUtil.isNull(ignoreStrategy)) {
 | 
			
		||||
            InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
 | 
			
		||||
        } else {
 | 
			
		||||
            ignoreStrategy.setTenantLine(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.getDataPermission())
 | 
			
		||||
                && 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.setTenantLine(false);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user