Pre Merge pull request !574 from amadeus5201/Ignore

This commit is contained in:
amadeus5201 2024-08-20 09:17:51 +00:00 committed by Gitee
commit 50a9244985
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 99 additions and 6 deletions

View File

@ -2,14 +2,15 @@ package org.dromara.common.mybatis.helper;
import cn.dev33.satoken.context.SaHolder; import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaStorage; import cn.dev33.satoken.context.model.SaStorage;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy; import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.dromara.common.core.utils.reflect.ReflectUtils;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -24,6 +25,23 @@ public class DataPermissionHelper {
private 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_PERMISSION = new ThreadLocal<>();
private static Stack<Integer> getReentrantIgnorePermissionStack() {
return Optional.ofNullable(REENTRANT_IGNORE_PERMISSION.get()).orElseGet(() -> {
REENTRANT_IGNORE_PERMISSION.set(new Stack<>());
return REENTRANT_IGNORE_PERMISSION.get();
});
}
private static void clearReentrantIgnore() {
REENTRANT_IGNORE_PERMISSION.remove();
}
private static void reentrantIgnoreIncrement() {
Stack<Integer> reentrantStack = getReentrantIgnorePermissionStack();
reentrantStack.push(reentrantStack.size() + 1);
}
/** /**
* 从上下文中获取指定键的变量值并将其转换为指定的类型 * 从上下文中获取指定键的变量值并将其转换为指定的类型
* *
@ -70,14 +88,41 @@ public class DataPermissionHelper {
* 开启忽略数据权限(开启后需手动调用 {@link #disableIgnore()} 关闭) * 开启忽略数据权限(开启后需手动调用 {@link #disableIgnore()} 关闭)
*/ */
public static void enableIgnore() { public static void enableIgnore() {
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (ignoreStrategy == null) {
InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build()); InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build());
} else {
if (!Boolean.TRUE.equals(ignoreStrategy.getDataPermission())) {
ignoreStrategy.setDataPermission(true);
}
}
reentrantIgnoreIncrement();
} }
/** /**
* 关闭忽略数据权限 * 关闭忽略数据权限
*/ */
public static void disableIgnore() { public static void disableIgnore() {
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (ignoreStrategy != null) {
boolean noOtherIgnoreStrategy = !Boolean.TRUE.equals(ignoreStrategy.getTenantLine())
&& !Boolean.TRUE.equals(ignoreStrategy.getDynamicTableName())
&& !Boolean.TRUE.equals(ignoreStrategy.getBlockAttack())
&& !Boolean.TRUE.equals(ignoreStrategy.getIllegalSql())
&& CollectionUtil.isEmpty(ignoreStrategy.getOthers());
Stack<Integer> reentrantStack = getReentrantIgnorePermissionStack();
boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1;
if (noOtherIgnoreStrategy && empty) {
InterceptorIgnoreHelper.clearIgnoreStrategy(); InterceptorIgnoreHelper.clearIgnoreStrategy();
} else if (empty) {
if (Boolean.TRUE.equals(ignoreStrategy.getDataPermission())) {
ignoreStrategy.setDataPermission(false);
clearReentrantIgnore();
}
}
}
} }
/** /**

View File

@ -1,6 +1,7 @@
package org.dromara.common.tenant.helper; package org.dromara.common.tenant.helper;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy; import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
@ -10,9 +11,12 @@ import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.GlobalConstants; import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.core.utils.SpringUtils; import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils; 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.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.common.satoken.utils.LoginHelper;
import java.util.Optional;
import java.util.Stack;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -28,6 +32,24 @@ public class TenantHelper {
private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new ThreadLocal<>(); private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new ThreadLocal<>();
private static final ThreadLocal<Stack<Integer>> REENTRANT_IGNORE_TENANT = new ThreadLocal<>();
private static Stack<Integer> getReentrantIgnoreTenantStack() {
return Optional.ofNullable(REENTRANT_IGNORE_TENANT.get()).orElseGet(() -> {
REENTRANT_IGNORE_TENANT.set(new Stack<>());
return REENTRANT_IGNORE_TENANT.get();
});
}
private static void clearReentrantIgnore() {
REENTRANT_IGNORE_TENANT.remove();
}
private static void reentrantIgnoreIncrement() {
Stack<Integer> reentrantStack = getReentrantIgnoreTenantStack();
reentrantStack.push(reentrantStack.size() + 1);
}
/** /**
* 租户功能是否启用 * 租户功能是否启用
*/ */
@ -39,14 +61,40 @@ public class TenantHelper {
* 开启忽略租户(开启后需手动调用 {@link #disableIgnore()} 关闭) * 开启忽略租户(开启后需手动调用 {@link #disableIgnore()} 关闭)
*/ */
public static void enableIgnore() { public static void enableIgnore() {
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (ignoreStrategy == null) {
InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
} else {
if (!Boolean.TRUE.equals(ignoreStrategy.getTenantLine())) {
ignoreStrategy.setTenantLine(true);
}
}
reentrantIgnoreIncrement();
} }
/** /**
* 关闭忽略租户 * 关闭忽略租户
*/ */
public static void disableIgnore() { public static void disableIgnore() {
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (ignoreStrategy != null) {
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 = getReentrantIgnoreTenantStack();
boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1;
if (noOtherIgnoreStrategy && empty) {
InterceptorIgnoreHelper.clearIgnoreStrategy(); InterceptorIgnoreHelper.clearIgnoreStrategy();
clearReentrantIgnore();
} else if (empty) {
ignoreStrategy.setDataPermission(false);
clearReentrantIgnore();
}
}
} }
/** /**