mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 09:35:29 +08:00
Pre Merge pull request !756 from Colin/feat-tenantIgnore
This commit is contained in:
commit
a675ce166a
@ -0,0 +1,17 @@
|
||||
package org.dromara.common.core.annotation;
|
||||
|
||||
/**
|
||||
* 租户忽略
|
||||
*
|
||||
* @author Colin
|
||||
* @date 2025/09/01
|
||||
*/
|
||||
public @interface TenantIgnore {
|
||||
|
||||
/**
|
||||
* 是否开启忽略租户,默认为 true 开启
|
||||
* <p>
|
||||
*/
|
||||
boolean enable() default true;
|
||||
|
||||
}
|
||||
@ -1,17 +1,22 @@
|
||||
package org.dromara.common.tenant.handle;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import lombok.AllArgsConstructor;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.SqlParserUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.NullValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import org.dromara.common.core.annotation.TenantIgnore;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.tenant.helper.TenantHelper;
|
||||
import org.dromara.common.tenant.properties.TenantProperties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 自定义租户处理器
|
||||
@ -19,11 +24,25 @@ import java.util.List;
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class PlusTenantLineHandler implements TenantLineHandler {
|
||||
|
||||
private final TenantProperties tenantProperties;
|
||||
|
||||
/**
|
||||
* 忽略的表
|
||||
* <p>
|
||||
* KEY:表名
|
||||
* VALUE:是否忽略
|
||||
*/
|
||||
private final Map<String, Boolean> ignoreTables = new ConcurrentHashMap<>();
|
||||
|
||||
public PlusTenantLineHandler(TenantProperties tenantProperties) {
|
||||
this.tenantProperties = tenantProperties;
|
||||
// 添加忽略的表
|
||||
Optional.ofNullable(tenantProperties.getExcludes()).ifPresent(tables ->
|
||||
tables.forEach(table -> addIgnoreTable(table, true)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
String tenantId = TenantHelper.getTenantId();
|
||||
@ -39,18 +58,36 @@ public class PlusTenantLineHandler implements TenantLineHandler {
|
||||
public boolean ignoreTable(String tableName) {
|
||||
String tenantId = TenantHelper.getTenantId();
|
||||
// 判断是否有租户
|
||||
Boolean ignore = false;
|
||||
if (StringUtils.isNotBlank(tenantId)) {
|
||||
// 不需要过滤租户的表
|
||||
List<String> excludes = tenantProperties.getExcludes();
|
||||
// 非业务表
|
||||
List<String> tables = ListUtil.toList(
|
||||
"gen_table",
|
||||
"gen_table_column"
|
||||
);
|
||||
tables.addAll(excludes);
|
||||
return StringUtils.equalsAnyIgnoreCase(tableName, tables.toArray(new String[0]));
|
||||
tableName = SqlParserUtils.removeWrapperSymbol(tableName);
|
||||
ignore = ignoreTables.get(tableName.toLowerCase());
|
||||
if (ignore == null) {
|
||||
ignore = computeIgnoreTable(tableName);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return ignore;
|
||||
}
|
||||
|
||||
private void addIgnoreTable(String tableName, boolean ignore) {
|
||||
ignoreTables.put(tableName.toLowerCase(), ignore);
|
||||
}
|
||||
|
||||
private boolean computeIgnoreTable(String tableName) {
|
||||
boolean ignore;
|
||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(tableName);
|
||||
if (tableInfo == null) {
|
||||
//找不到的表,说明不是项目里的或未被mp管理,不进行拦截(忽略租户)
|
||||
ignore = true;
|
||||
} else {
|
||||
// 如果添加了 @TenantIgnore 注解,看是否忽略租户
|
||||
TenantIgnore tenantIgnore = AnnotationUtil.getAnnotation(tableInfo.getEntityType(), TenantIgnore.class);
|
||||
ignore = tenantIgnore != null && tenantIgnore.enable();
|
||||
}
|
||||
// 添加到缓存
|
||||
addIgnoreTable(tableName, ignore);
|
||||
return ignore;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -4,14 +4,16 @@ import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
||||
import org.dromara.generator.constant.GenConstants;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.dromara.common.core.annotation.TenantIgnore;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
||||
import org.dromara.generator.constant.GenConstants;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -23,8 +25,10 @@ import java.util.List;
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("gen_table")
|
||||
@TenantIgnore
|
||||
public class GenTable extends BaseEntity {
|
||||
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
|
||||
@ -4,13 +4,13 @@ import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import org.dromara.common.core.annotation.TenantIgnore;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
||||
|
||||
/**
|
||||
* 代码生成业务字段表 gen_table_column
|
||||
@ -20,6 +20,7 @@ import jakarta.validation.constraints.NotBlank;
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("gen_table_column")
|
||||
@TenantIgnore
|
||||
public class GenTableColumn extends BaseEntity {
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user