fix=>权限和租户可以同时忽略,并且可重复忽略

This commit is contained in:
amadeus5201 2024-08-16 17:29:23 +08:00
parent 08d4493994
commit 32743d8a76
2 changed files with 86 additions and 4 deletions

View File

@ -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.Objects;
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<Integer> REENTRANT_IGNORE_PERMISSION = new ThreadLocal<>();
/**
* 从上下文中获取指定键的变量值并将其转换为指定的类型
*
@ -70,14 +75,50 @@ public class DataPermissionHelper {
* 开启忽略数据权限(开启后需手动调用 {@link #disableIgnore()} 关闭)
*/
public static void enableIgnore() {
InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build());
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (Objects.nonNull(ignoreStrategy)) {
if (!Boolean.TRUE.equals(ignoreStrategy.getDataPermission())) {
ignoreStrategy.setDataPermission(true);
REENTRANT_IGNORE_PERMISSION.set(1);
} else {
Integer count = REENTRANT_IGNORE_PERMISSION.get();
REENTRANT_IGNORE_PERMISSION.set(count + 1);
}
} else {
InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().dataPermission(true).build());
REENTRANT_IGNORE_PERMISSION.set(1);
}
}
/**
* 关闭忽略数据权限
*/
public static void disableIgnore() {
InterceptorIgnoreHelper.clearIgnoreStrategy();
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (Objects.nonNull(ignoreStrategy)) {
Integer count = REENTRANT_IGNORE_PERMISSION.get();
boolean hasCount = count.compareTo(1) > 0;
if (ignoreStrategy.getBlockAttack() == null && ignoreStrategy.getDynamicTableName() == null && ignoreStrategy.getTenantLine() == null && ignoreStrategy.getIllegalSql() == null && CollectionUtil.isEmpty(ignoreStrategy.getOthers())) {
if (!hasCount) {
InterceptorIgnoreHelper.clearIgnoreStrategy();
}
} else {
if (!hasCount) {
if (count.compareTo(1) == 0) {
ignoreStrategy.setDataPermission(null);
REENTRANT_IGNORE_PERMISSION.remove();
}
}
}
if (hasCount) {
REENTRANT_IGNORE_PERMISSION.set(count - 1);
}
} else {
InterceptorIgnoreHelper.clearIgnoreStrategy();
REENTRANT_IGNORE_PERMISSION.remove();
}
}
/**

View File

@ -1,6 +1,7 @@
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 com.alibaba.ttl.TransmittableThreadLocal;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
@ -11,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.Objects;
import java.util.function.Supplier;
/**
@ -29,6 +32,8 @@ public class TenantHelper {
private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>();
private static final ThreadLocal<Integer> REENTRANT_IGNORE_TENANT = new ThreadLocal<>();
/**
* 租户功能是否启用
*/
@ -40,14 +45,50 @@ public class TenantHelper {
* 开启忽略租户(开启后需手动调用 {@link #disableIgnore()} 关闭)
*/
public static void enableIgnore() {
InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (Objects.nonNull(ignoreStrategy)) {
if (!Boolean.TRUE.equals(ignoreStrategy.getTenantLine())) {
ignoreStrategy.setTenantLine(true);
REENTRANT_IGNORE_TENANT.set(1);
} else {
Integer count = REENTRANT_IGNORE_TENANT.get();
REENTRANT_IGNORE_TENANT.set(count + 1);
}
} else {
InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
REENTRANT_IGNORE_TENANT.set(1);
}
}
/**
* 关闭忽略租户
*/
public static void disableIgnore() {
InterceptorIgnoreHelper.clearIgnoreStrategy();
ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = (ThreadLocal<IgnoreStrategy>) ReflectUtils.getStaticFieldValue(ReflectUtils.getField(InterceptorIgnoreHelper.class, "IGNORE_STRATEGY_LOCAL"));
IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
if (Objects.nonNull(ignoreStrategy)) {
Integer count = REENTRANT_IGNORE_TENANT.get();
boolean hasCount = count.compareTo(1) > 0;
if (ignoreStrategy.getBlockAttack() == null && ignoreStrategy.getDynamicTableName() == null && ignoreStrategy.getIllegalSql() == null && ignoreStrategy.getDataPermission() == null && CollectionUtil.isEmpty(ignoreStrategy.getOthers())) {
if (!hasCount) {
InterceptorIgnoreHelper.clearIgnoreStrategy();
}
} else {
if (!hasCount) {
if (count.compareTo(1) == 0) {
ignoreStrategy.setTenantLine(null);
REENTRANT_IGNORE_TENANT.remove();
}
}
}
if (hasCount) {
REENTRANT_IGNORE_TENANT.set(count - 1);
}
} else {
InterceptorIgnoreHelper.clearIgnoreStrategy();
REENTRANT_IGNORE_TENANT.remove();
}
}
/**