fix(多租户):过滤超级管理员租户条件

This commit is contained in:
2025-05-17 17:55:39 +08:00
parent 783753bd60
commit 8b1552db8a
2 changed files with 15 additions and 34 deletions
@@ -1,7 +1,9 @@
package com.central.db.config;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.context.TenantContextHolder;
import com.central.common.model.LoginAppUser;
import com.central.common.properties.TenantProperties;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
@@ -29,6 +31,12 @@ public class TenantAutoConfigure {
*/
@Override
public Expression getTenantId() {
// 判断当前用户是否为超级管理员
if (isSuperAdmin()) {
// 超级管理员返回Null,不加租户条件
return new NullValue();
}
String tenant = TenantContextHolder.getTenant();
if (tenant != null) {
return new StringValue(TenantContextHolder.getTenant());
@@ -48,4 +56,11 @@ public class TenantAutoConfigure {
}
};
}
private boolean isSuperAdmin() {
// 你可以通过ThreadLocal、SecurityContext、LoginUserContextHolder等获取当前用户角色
// 例如:
LoginAppUser user = LoginUserContextHolder.getUser();
return user != null && user.getUsername().equals("admin");
}
}
@@ -1,34 +0,0 @@
package com.central.db.interceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.central.common.context.LoginUserContextHolder;
import com.central.common.context.TenantContextHolder;
import com.central.common.model.LoginAppUser;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
/**
* 超级管理员可以管理租户数据权限开放
* @date 2025-05-15
* @author lsl
*/
public class CustomTenantLineHandler implements TenantLineHandler {
@Override
public Expression getTenantId() {
// 判断当前用户是否为超级管理员
if (isSuperAdmin()) {
// 超级管理员返回Null,不加租户条件
return new NullValue();
}
// 普通用户返回租户ID
return new StringValue(TenantContextHolder.getTenant());
}
private boolean isSuperAdmin() {
// 你可以通过ThreadLocal、SecurityContext、LoginUserContextHolder等获取当前用户角色
// 例如:
LoginAppUser user = LoginUserContextHolder.getUser();
return user != null && user.getUsername().equals("admin");
}
}