【8.0】【tenant】移除旧租户相关业务

pull/57/head
fengshuonan 2023-08-30 09:58:28 +08:00
parent 3811e45d1d
commit 1dd597025a
17 changed files with 12 additions and 402 deletions

View File

@ -82,11 +82,6 @@ public interface RuleConstants {
*/
String CHINESE_TRAN_LANGUAGE_CODE = "chinese";
/**
*
*/
String TENANT_DB_PREFIX = "sys_tenant_db_";
/**
* base64<img src=""/>使
*/

View File

@ -1,21 +0,0 @@
package cn.stylefeng.roses.kernel.rule.constants;
/**
*
*
* @author fengshuonan
* @since 2021/9/24 17:20
*/
public interface TenantConstants {
/**
*
*/
String TENANT_DB_PREFIX = "sys_tenant_db_";
/**
* master
*/
String MASTER_DATASOURCE_NAME = "master";
}

View File

@ -1,45 +0,0 @@
package cn.stylefeng.roses.kernel.rule.tenant;
/**
* 使
* <p>
* 使使
*
* @author fengshuonan
* @since 2022/11/8 0:58
*/
public class OnceTenantCodeHolder {
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:59
*/
public static void setTenantCode(String aesKey) {
CONTEXT_HOLDER.set(aesKey);
}
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:59
*/
public static String getTenantCode() {
return CONTEXT_HOLDER.get();
}
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:59
*/
public static void clearTenantCode() {
CONTEXT_HOLDER.remove();
}
}

View File

@ -1,45 +0,0 @@
package cn.stylefeng.roses.kernel.rule.tenant;
/**
* 使
* <p>
* HttpContextLoginUser使
*
* @author fengshuonan
* @since 2022/11/8 0:58
*/
public class RequestTenantCodeHolder {
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:59
*/
public static void setTenantCode(String aesKey) {
CONTEXT_HOLDER.set(aesKey);
}
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:59
*/
public static String getTenantCode() {
return CONTEXT_HOLDER.get();
}
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:59
*/
public static void clearTenantCode() {
CONTEXT_HOLDER.remove();
}
}

View File

@ -1,19 +0,0 @@
package cn.stylefeng.roses.kernel.rule.tenant;
/**
*
*
* @author fengshuonan
* @since 2022/11/7 20:16
*/
public interface TenantPrefixApi {
/**
*
*
* @author fengshuonan
* @since 2022/11/7 20:29
*/
String getTenantPrefix();
}

View File

@ -44,15 +44,12 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.payload.DefaultJwtPayload;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.demo.expander.DemoConfigExpander;
import cn.stylefeng.roses.kernel.dsctn.api.constants.DatasourceContainerConstants;
import cn.stylefeng.roses.kernel.dsctn.api.context.CurrentDataSourceContext;
import cn.stylefeng.roses.kernel.jwt.JwtTokenOperator;
import cn.stylefeng.roses.kernel.jwt.api.JwtApi;
import cn.stylefeng.roses.kernel.jwt.api.exception.JwtException;
import cn.stylefeng.roses.kernel.jwt.api.exception.enums.JwtExceptionEnum;
import cn.stylefeng.roses.kernel.jwt.api.pojo.config.JwtConfig;
import cn.stylefeng.roses.kernel.log.api.LoginLogServiceApi;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.sys.api.SysUserServiceApi;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.UserValidateDTO;
import com.alibaba.fastjson.JSON;
@ -243,15 +240,7 @@ public class AuthServiceImpl implements AuthServiceApi {
// 获取用户的信息
String account = defaultJwtPayload.getAccount();
// 获取用户租户信息
String tenantCode = defaultJwtPayload.getTenantCode();
LoginUser loginUser;
try {
// 如果有特定租户则进行切换操作
if (StrUtil.isNotEmpty(tenantCode) && !DatasourceContainerConstants.MASTER_DATASOURCE_NAME.equals(tenantCode)) {
CurrentDataSourceContext.setDataSourceName(RuleConstants.TENANT_DB_PREFIX + tenantCode);
}
// 获取用户信息
UserValidateDTO userValidateDTO = sysUserServiceApi.getUserLoginValidateDTO(account);
@ -261,10 +250,6 @@ public class AuthServiceImpl implements AuthServiceApi {
// 创建用户会话信息
sessionManagerApi.updateSession(token, loginUser);
} finally {
// 清除租户信息
CurrentDataSourceContext.clearDataSourceName();
}
return loginUser;
}

View File

@ -26,7 +26,6 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.sso.SsoProperties;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.demo.expander.DemoConfigExpander;
import cn.stylefeng.roses.kernel.log.api.LoginLogServiceApi;
import cn.stylefeng.roses.kernel.rule.tenant.RequestTenantCodeHolder;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import cn.stylefeng.roses.kernel.scanner.api.exception.ScannerException;
import cn.stylefeng.roses.kernel.scanner.api.exception.enums.ScannerExceptionEnum;
@ -103,9 +102,6 @@ public class LoginService {
// 1.2 判断账号是否密码重试次数过多被冻结
Integer loginErrorCount = validatePasswordRetryTimes(loginRequest);
// 1.3 暂存多租户编码v7.3.2增加,方便缓存调用过程中获取多租户的前缀)
RequestTenantCodeHolder.setTenantCode(loginRequest.getTenantCode());
// 2. 如果开启了验证码校验,则验证当前请求的验证码是否正确
validateCaptcha(loginRequest);

View File

@ -1,21 +0,0 @@
package cn.stylefeng.roses.kernel.auth.callback;
import cn.stylefeng.roses.kernel.rule.tenant.RequestTenantCodeHolder;
import cn.stylefeng.roses.kernel.rule.threadlocal.RemoveThreadLocalApi;
import org.springframework.stereotype.Component;
/**
* ThreadLocal
*
* @author fengshuonan
* @since 2021/10/29 11:37
*/
@Component
public class TenantCodeRemoveThreadLocalHolder implements RemoveThreadLocalApi {
@Override
public void removeThreadLocalAction() {
RequestTenantCodeHolder.clearTenantCode();
}
}

View File

@ -26,9 +26,6 @@ package cn.stylefeng.roses.kernel.cache.api;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.rule.constants.TenantConstants;
import cn.stylefeng.roses.kernel.rule.tenant.TenantPrefixApi;
import java.util.Collection;
import java.util.Map;
@ -151,20 +148,6 @@ public interface CacheOperatorApi<T> {
*/
String getCommonKeyPrefix();
/**
*
* <p>
* key::key
* <p>
* master:
*
* @author fengshuonan
* @since 2022/11/9 19:02
*/
default Boolean divideByTenant() {
return false;
}
/**
*
* <p>
@ -174,11 +157,7 @@ public interface CacheOperatorApi<T> {
* @since 2022/11/9 10:41
*/
default String getFinalPrefix() {
// 获取租户前缀
String tenantPrefix = getTenantPrefix();
// 计算最终前缀
return tenantPrefix + CACHE_DELIMITER + getCommonKeyPrefix() + CACHE_DELIMITER;
return getCommonKeyPrefix() + CACHE_DELIMITER;
}
/**
@ -216,39 +195,4 @@ public interface CacheOperatorApi<T> {
return StrUtil.removePrefix(finalKey, getFinalPrefix());
}
/**
*
*
* @author fengshuonan
* @since 2022/11/9 10:35
*/
default String getTenantPrefix() {
// 缓存是否按租户维度切分
Boolean divideByTenantFlag = divideByTenant();
// 如果不按租户维度切分则默认都返回为master
if (!divideByTenantFlag) {
return TenantConstants.MASTER_DATASOURCE_NAME;
}
// 用户的租户前缀
String tenantPrefix = "";
try {
TenantPrefixApi tenantPrefixApi = SpringUtil.getBean(TenantPrefixApi.class);
if (tenantPrefixApi != null) {
tenantPrefix = tenantPrefixApi.getTenantPrefix();
}
} catch (Exception e) {
// 如果找不到这个bean则没有加载多租户插件
}
// 如果租户前缀为空,则设置为主租户的编码
if (ObjectUtil.isEmpty(tenantPrefix)) {
tenantPrefix = TenantConstants.MASTER_DATASOURCE_NAME;
}
return tenantPrefix;
}
}

View File

@ -1,59 +0,0 @@
package cn.stylefeng.roses.kernel.cache.api.tenant;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.rule.tenant.OnceTenantCodeHolder;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:45
*/
@SuppressWarnings("all")
public class TenantCacheProxy implements InvocationHandler {
/**
*
*/
private CacheOperatorApi<?> targetCacheObject = null;
/**
*
*/
private String tenantCode = null;
/**
*
*
* @param tenantCode
* @param target
* @author fengshuonan
* @since 2022/11/8 1:05
*/
public <T> CacheOperatorApi<T> bindCacheObject(String tenantCode, CacheOperatorApi<T> target) {
this.targetCacheObject = target;
this.tenantCode = tenantCode;
return (CacheOperatorApi<T>) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getSuperclass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
try {
// 设置租户编码
OnceTenantCodeHolder.setTenantCode(tenantCode);
// 执行原有缓存操作类
result = method.invoke(targetCacheObject, args);
} finally {
// 清除租户编码
OnceTenantCodeHolder.clearTenantCode();
}
return result;
}
}

View File

@ -1,44 +0,0 @@
package cn.stylefeng.roses.kernel.cache.api.tenant;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import java.util.HashMap;
import java.util.Map;
/**
*
*
* @author fengshuonan
* @since 2022/11/8 0:45
*/
@SuppressWarnings("all")
public class TenantCacheProxyFactory {
/**
*
*/
private static final Map<String, CacheOperatorApi<?>> proxyCacheMap = new HashMap<>();
/**
*
*
* @author fengshuonan
* @since 2022/11/8 1:18
*/
public static <T> CacheOperatorApi<T> createTenantCacheProxy(String tenantCode, CacheOperatorApi<?> cacheOperatorApi) {
// 计算缓存的key
String key = tenantCode + cacheOperatorApi.getCommonKeyPrefix();
// 直接获取缓存中的代理
if (proxyCacheMap.containsKey(key)) {
return (CacheOperatorApi<T>) proxyCacheMap.get(key);
}
TenantCacheProxy tenantCacheProxy = new TenantCacheProxy();
CacheOperatorApi<?> resultCacheOperator = tenantCacheProxy.bindCacheObject(tenantCode, cacheOperatorApi);
proxyCacheMap.put(key, resultCacheOperator);
return (CacheOperatorApi<T>) resultCacheOperator;
}
}

View File

@ -11,7 +11,6 @@ import cn.stylefeng.roses.kernel.db.api.sqladapter.database.CreateDatabaseSql;
import cn.stylefeng.roses.kernel.db.api.sqladapter.database.GetDatabasesSql;
import cn.stylefeng.roses.kernel.db.api.sqladapter.table.TableFieldListSql;
import cn.stylefeng.roses.kernel.db.api.sqladapter.table.TableListSql;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
@ -48,12 +47,9 @@ public class DatabaseUtil {
while (resultSet.next()) {
String database = resultSet.getString("database");
if (StrUtil.isNotBlank(database)) {
if (StrUtil.startWith(database, RuleConstants.TENANT_DB_PREFIX)) {
database = database.replaceAll(RuleConstants.TENANT_DB_PREFIX, "");
databasesList.add(database);
}
}
}
return databasesList;
} catch (Exception e) {
log.error("查询所有库错误!", e);

View File

@ -88,11 +88,6 @@ public enum DatasourceContainerExceptionEnum implements AbstractExceptionEnum {
*/
EDIT_DATASOURCE_NAME_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DatasourceContainerConstants.DS_CTN_EXCEPTION_STEP_CODE + "10", "修改数据源失败,不能修改数据源名称,原名称为={}"),
/**
*
*/
TENANT_DATASOURCE_CANT_DELETE(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DatasourceContainerConstants.DS_CTN_EXCEPTION_STEP_CODE + "11", "租户数据源不能删除"),
/**
*
*/

View File

@ -47,7 +47,6 @@ import cn.stylefeng.roses.kernel.group.api.GroupApi;
import cn.stylefeng.roses.kernel.group.api.constants.GroupConstants;
import cn.stylefeng.roses.kernel.group.api.pojo.SysGroupDTO;
import cn.stylefeng.roses.kernel.group.api.pojo.SysGroupRequest;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
@ -167,11 +166,6 @@ public class DatabaseInfoServiceImpl extends ServiceImpl<DatabaseInfoMapper, Dat
DatabaseInfo databaseInfo = this.queryDatabaseInfoById(databaseInfoRequest);
// 如果是租户数据库不能删除
if (databaseInfo.getDbName().startsWith(RuleConstants.TENANT_DB_PREFIX)) {
throw new DatasourceContainerException(DatasourceContainerExceptionEnum.TENANT_DATASOURCE_CANT_DELETE);
}
// 不能删除主数据源
if (MASTER_DATASOURCE_NAME.equals(databaseInfo.getDbName())) {
throw new DatasourceContainerException(DatasourceContainerExceptionEnum.MASTER_DATASOURCE_CANT_DELETE);

View File

@ -26,11 +26,8 @@ package cn.stylefeng.roses.kernel.validator.api.validators.unique.service;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.DbOperatorApi;
import cn.stylefeng.roses.kernel.db.api.context.DbOperatorContext;
import cn.stylefeng.roses.kernel.rule.constants.TenantConstants;
import cn.stylefeng.roses.kernel.validator.api.exception.ParamValidateException;
import cn.stylefeng.roses.kernel.validator.api.exception.enums.ValidatorExceptionEnum;
import cn.stylefeng.roses.kernel.validator.api.pojo.UniqueValidateParam;
@ -69,7 +66,7 @@ public class TableUniqueValueService {
if (!uniqueValidateParam.getExcludeCurrentRecord()
&& !uniqueValidateParam.getExcludeLogicDeleteItems()) {
String sqlTemplate = "select count(*) from {} where {} = {0}";
String finalSql = StrUtil.format(sqlTemplate, calcTenantTableName(uniqueValidateParam.getTableName()), uniqueValidateParam.getColumnName());
String finalSql = StrUtil.format(sqlTemplate, uniqueValidateParam.getTableName(), uniqueValidateParam.getColumnName());
resultCount = dbOperatorApi.selectCount(finalSql, uniqueValidateParam.getValue());
}
@ -78,7 +75,7 @@ public class TableUniqueValueService {
&& uniqueValidateParam.getExcludeLogicDeleteItems()) {
String sqlTemplate = "select count(*) from {} where {} = {0} and ({} is null or {} <> '{}')";
String finalSql = StrUtil.format(sqlTemplate,
calcTenantTableName(uniqueValidateParam.getTableName()),
uniqueValidateParam.getTableName(),
uniqueValidateParam.getColumnName(),
uniqueValidateParam.getLogicDeleteFieldName(),
uniqueValidateParam.getLogicDeleteFieldName(),
@ -94,7 +91,7 @@ public class TableUniqueValueService {
paramIdValidate(uniqueValidateParam);
String sqlTemplate = "select count(*) from {} where {} = {0} and {} <> {1}";
String finalSql = StrUtil.format(sqlTemplate, calcTenantTableName(uniqueValidateParam.getTableName()), uniqueValidateParam.getColumnName(), uniqueValidateParam.getIdFieldName());
String finalSql = StrUtil.format(sqlTemplate, uniqueValidateParam.getTableName(), uniqueValidateParam.getColumnName(), uniqueValidateParam.getIdFieldName());
resultCount = dbOperatorApi.selectCount(finalSql, uniqueValidateParam.getValue(), uniqueValidateParam.getId());
}
@ -107,7 +104,7 @@ public class TableUniqueValueService {
String sqlTemplate = "select count(*) from {} where {} = {0} and {} <> {1} and ({} is null or {} <> '{}')";
String finalSql = StrUtil.format(sqlTemplate,
calcTenantTableName(uniqueValidateParam.getTableName()),
uniqueValidateParam.getTableName(),
uniqueValidateParam.getColumnName(),
uniqueValidateParam.getIdFieldName(),
uniqueValidateParam.getLogicDeleteFieldName(),
@ -151,28 +148,4 @@ public class TableUniqueValueService {
}
}
/**
* code
*
* @author fengshuonan
* @since 2021/9/24 17:01
*/
private static String calcTenantTableName(String originTableName) {
// 获取租户编码
LoginUser loginUser = LoginContext.me().getLoginUserNullable();
if (loginUser == null) {
return originTableName;
}
String tenantCode = loginUser.getTenantCode();
// 如果是主数据源可以忽略
if (StrUtil.isBlank(tenantCode) || TenantConstants.MASTER_DATASOURCE_NAME.equals(tenantCode)) {
return originTableName;
}
return TenantConstants.TENANT_DB_PREFIX + tenantCode + "." + originTableName;
}
}

View File

@ -54,13 +54,6 @@ public class HrOrgApprover extends BaseEntity {
@ChineseDescription("用户id")
private Long userId;
/**
* id
*/
@TableField("tenant_id")
@ChineseDescription("租户id")
private Long tenantId;
//-------------------------------非实体字段-------------------------------
//-------------------------------非实体字段-------------------------------
//-------------------------------非实体字段-------------------------------

View File

@ -48,11 +48,4 @@ public class PortalUserApp extends BaseEntity {
@ChineseDescription("关联的菜单id")
private Long menuId;
/**
* id
*/
@TableField("tenant_id")
@ChineseDescription("租户id")
private Long tenantId;
}