mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 10:35:57 +08:00
update 优化忽略租户写法,在抛出异常时可以正确关闭忽略
fix 修复oracle脚本字段没对上
This commit is contained in:
parent
8d0d85444a
commit
b663197e45
@ -0,0 +1,20 @@
|
||||
package org.dromara.common.tenant.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 忽略租户注解
|
||||
*
|
||||
* @author yixiacoco
|
||||
*/
|
||||
@Inherited
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface IgnoreTenant {
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package org.dromara.common.tenant.aspect;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.dromara.common.tenant.annotation.IgnoreTenant;
|
||||
import org.dromara.common.tenant.helper.TenantHelper;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* 租户切面
|
||||
*
|
||||
* @author yixiacoco
|
||||
*/
|
||||
@Aspect
|
||||
@Order(-1)
|
||||
public class TenantAspect {
|
||||
|
||||
/**
|
||||
* 处理忽略租户
|
||||
*
|
||||
* @param point 织入点
|
||||
* @param ignoreTenant 忽略注解
|
||||
*/
|
||||
@Around("@within(ignoreTenant) || @annotation(ignoreTenant)")
|
||||
public Object handleIgnore(ProceedingJoinPoint point, IgnoreTenant ignoreTenant) throws Throwable {
|
||||
TenantHelper.enableIgnore();
|
||||
try {
|
||||
return point.proceed();
|
||||
} finally {
|
||||
TenantHelper.disableIgnore();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ import org.dromara.common.core.utils.reflect.ReflectUtils;
|
||||
import org.dromara.common.mybatis.config.MybatisPlusConfig;
|
||||
import org.dromara.common.redis.config.RedisConfig;
|
||||
import org.dromara.common.redis.config.properties.RedissonProperties;
|
||||
import org.dromara.common.tenant.aspect.TenantAspect;
|
||||
import org.dromara.common.tenant.core.TenantSaTokenDao;
|
||||
import org.dromara.common.tenant.handle.PlusTenantLineHandler;
|
||||
import org.dromara.common.tenant.handle.TenantKeyPrefixHandler;
|
||||
@ -97,4 +98,11 @@ public class TenantConfig {
|
||||
return new TenantSaTokenDao();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多租户切面
|
||||
*/
|
||||
@Bean
|
||||
public TenantAspect tenantAspect() {
|
||||
return new TenantAspect();
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,14 +6,16 @@ import cn.hutool.core.convert.Convert;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
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.redis.utils.RedisUtils;
|
||||
import org.dromara.common.satoken.utils.LoginHelper;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 租户助手
|
||||
@ -49,6 +51,34 @@ public class TenantHelper {
|
||||
InterceptorIgnoreHelper.clearIgnoreStrategy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 在忽略租户中执行
|
||||
*
|
||||
* @param handle 处理执行方法
|
||||
*/
|
||||
public static void ignore(Runnable handle) {
|
||||
enableIgnore();
|
||||
try {
|
||||
handle.run();
|
||||
} finally {
|
||||
disableIgnore();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在忽略租户中执行
|
||||
*
|
||||
* @param handle 处理执行方法
|
||||
*/
|
||||
public static <T> T ignore(Supplier<T> handle) {
|
||||
enableIgnore();
|
||||
try {
|
||||
return handle.get();
|
||||
} finally {
|
||||
disableIgnore();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置动态租户(一直有效 需要手动清理)
|
||||
* <p>
|
||||
|
||||
@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.core.constant.CacheNames;
|
||||
import org.dromara.common.core.exception.ServiceException;
|
||||
import org.dromara.common.core.utils.MapstructUtils;
|
||||
@ -24,8 +26,6 @@ import org.dromara.system.domain.bo.SysOssConfigBo;
|
||||
import org.dromara.system.domain.vo.SysOssConfigVo;
|
||||
import org.dromara.system.mapper.SysOssConfigMapper;
|
||||
import org.dromara.system.service.ISysOssConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -52,23 +52,26 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
|
||||
*/
|
||||
@Override
|
||||
public void init() {
|
||||
TenantHelper.enableIgnore();
|
||||
List<SysOssConfig> list = baseMapper.selectList(
|
||||
new LambdaQueryWrapper<SysOssConfig>().orderByAsc(TenantEntity::getTenantId));
|
||||
TenantHelper.disableIgnore();
|
||||
List<SysOssConfig> list = TenantHelper.ignore(() ->
|
||||
baseMapper.selectList(
|
||||
new LambdaQueryWrapper<SysOssConfig>().orderByAsc(TenantEntity::getTenantId))
|
||||
);
|
||||
Map<String, List<SysOssConfig>> map = StreamUtils.groupByKey(list, SysOssConfig::getTenantId);
|
||||
for (String tenantId : map.keySet()) {
|
||||
TenantHelper.setDynamic(tenantId);
|
||||
// 加载OSS初始化配置
|
||||
for (SysOssConfig config : map.get(tenantId)) {
|
||||
String configKey = config.getConfigKey();
|
||||
if ("0".equals(config.getStatus())) {
|
||||
RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey);
|
||||
try {
|
||||
for (String tenantId : map.keySet()) {
|
||||
TenantHelper.setDynamic(tenantId);
|
||||
// 加载OSS初始化配置
|
||||
for (SysOssConfig config : map.get(tenantId)) {
|
||||
String configKey = config.getConfigKey();
|
||||
if ("0".equals(config.getStatus())) {
|
||||
RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey);
|
||||
}
|
||||
CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config));
|
||||
}
|
||||
CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config));
|
||||
}
|
||||
} finally {
|
||||
TenantHelper.clearDynamic();
|
||||
}
|
||||
TenantHelper.clearDynamic();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -17,7 +17,7 @@ import org.dromara.common.core.utils.SpringUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.mybatis.core.page.PageQuery;
|
||||
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
||||
import org.dromara.common.tenant.helper.TenantHelper;
|
||||
import org.dromara.common.tenant.annotation.IgnoreTenant;
|
||||
import org.dromara.system.domain.*;
|
||||
import org.dromara.system.domain.bo.SysTenantBo;
|
||||
import org.dromara.system.domain.vo.SysTenantVo;
|
||||
@ -111,9 +111,9 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
||||
* 新增租户
|
||||
*/
|
||||
@Override
|
||||
@IgnoreTenant
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean insertByBo(SysTenantBo bo) {
|
||||
TenantHelper.enableIgnore();
|
||||
|
||||
SysTenant add = MapstructUtils.convert(bo, SysTenant.class);
|
||||
|
||||
@ -124,7 +124,6 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
||||
add.setTenantId(tenantId);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (!flag) {
|
||||
TenantHelper.disableIgnore();
|
||||
throw new ServiceException("创建租户失败");
|
||||
}
|
||||
bo.setId(add.getId());
|
||||
@ -187,7 +186,6 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
||||
}
|
||||
configMapper.insertBatch(sysConfigList);
|
||||
|
||||
TenantHelper.disableIgnore();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -342,9 +340,9 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
||||
* 同步租户套餐
|
||||
*/
|
||||
@Override
|
||||
@IgnoreTenant
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean syncTenantPackage(String tenantId, String packageId) {
|
||||
TenantHelper.enableIgnore();
|
||||
SysTenantPackage tenantPackage = tenantPackageMapper.selectById(packageId);
|
||||
List<SysRole> roles = roleMapper.selectList(
|
||||
new LambdaQueryWrapper<SysRole>().eq(SysRole::getTenantId, tenantId));
|
||||
@ -369,7 +367,6 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
||||
roleMenuMapper.delete(
|
||||
new LambdaQueryWrapper<SysRoleMenu>().in(SysRoleMenu::getRoleId, roleIds).notIn(!menuIds.isEmpty(), SysRoleMenu::getMenuId, menuIds));
|
||||
}
|
||||
TenantHelper.disableIgnore();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,8 +66,8 @@ comment on column test_tree.update_time is '更新时间';
|
||||
comment on column test_tree.update_by is '更新人';
|
||||
comment on column test_tree.del_flag is '删除标志';
|
||||
|
||||
insert into sys_user(user_id, tenant_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_dept, create_by, create_time, update_by, update_time, remark) values (3, '000000', 108, 'test', '本部门及以下 密码666666', 'sys_user', '', '', '0', null, '$2a$10$b8yUzN0C71sbz.PhNOCgJe.Tu1yWC3RNrTyjSQ8p1W0.aaUXUJ.Ne', '0', '0', '127.0.0.1', sysdate, 103, sysdate, 1, sysdate, 3, sysdate, null);
|
||||
insert into sys_user(user_id, tenant_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_dept, create_by, create_time, update_by, update_time, remark) values (4, '000000', 102, 'test1', '仅本人 密码666666', 'sys_user', '', '', '0', null, '$2a$10$b8yUzN0C71sbz.PhNOCgJe.Tu1yWC3RNrTyjSQ8p1W0.aaUXUJ.Ne', '0', '0', '127.0.0.1', sysdate, 103, sysdate, 1, sysdate, 4, sysdate, null);
|
||||
insert into sys_user(user_id, tenant_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_dept, create_by, create_time, update_by, update_time, remark) values (3, '000000', 108, 'test', '本部门及以下 密码666666', 'sys_user', '', '', '0', null, '$2a$10$b8yUzN0C71sbz.PhNOCgJe.Tu1yWC3RNrTyjSQ8p1W0.aaUXUJ.Ne', '0', '0', '127.0.0.1', sysdate, 103, 1, sysdate, 3, sysdate, null);
|
||||
insert into sys_user(user_id, tenant_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_dept, create_by, create_time, update_by, update_time, remark) values (4, '000000', 102, 'test1', '仅本人 密码666666', 'sys_user', '', '', '0', null, '$2a$10$b8yUzN0C71sbz.PhNOCgJe.Tu1yWC3RNrTyjSQ8p1W0.aaUXUJ.Ne', '0', '0', '127.0.0.1', sysdate, 103, 1, sysdate, 4, sysdate, null);
|
||||
|
||||
insert into sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_dept, create_by, create_time, update_by, update_time, remark) values (5, '测试菜单', 0, 5, 'demo', null, 1, 0, 'M', '0', '0', null, 'star', 103, 1, sysdate, 1, sysdate, '');
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user