mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-10-31 02:28:03 +08:00 
			
		
		
		
	修复租户名的重复问题
This commit is contained in:
		| @ -78,8 +78,8 @@ public class TenantSecurityWebFilter extends ApiRequestFilter { | |||||||
|             // 2. 如果请求未带租户的编号,不允许访问。 |             // 2. 如果请求未带租户的编号,不允许访问。 | ||||||
|             if (tenantId == null) { |             if (tenantId == null) { | ||||||
|                 log.error("[doFilterInternal][URL({}/{}) 未传递租户编号]", request.getRequestURI(), request.getMethod()); |                 log.error("[doFilterInternal][URL({}/{}) 未传递租户编号]", request.getRequestURI(), request.getMethod()); | ||||||
|                 String msg = "请求的租户标识未传递,请进行排查"; |                 ServletUtils.writeJSON(response, CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), | ||||||
|                 ServletUtils.writeJSON(response, CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), msg)); |                         "请求的租户标识未传递,请进行排查")); | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
|             // 3. 校验租户是合法,例如说被禁用、到期 |             // 3. 校验租户是合法,例如说被禁用、到期 | ||||||
|  | |||||||
| @ -1,6 +1,8 @@ | |||||||
| package cn.iocoder.yudao.framework.mybatis.core.query; | package cn.iocoder.yudao.framework.mybatis.core.query; | ||||||
|  |  | ||||||
| import cn.hutool.core.util.ArrayUtil; | import cn.hutool.core.util.ArrayUtil; | ||||||
|  | import cn.hutool.core.util.ObjectUtil; | ||||||
|  | import cn.hutool.core.util.StrUtil; | ||||||
| import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | ||||||
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||||||
| import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | ||||||
| @ -40,14 +42,14 @@ public class LambdaQueryWrapperX<T> extends LambdaQueryWrapper<T> { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public LambdaQueryWrapperX<T> eqIfPresent(SFunction<T, ?> column, Object val) { |     public LambdaQueryWrapperX<T> eqIfPresent(SFunction<T, ?> column, Object val) { | ||||||
|         if (val instanceof String && StringUtils.hasText((String) val) || !(val instanceof String) && val != null) { |         if (ObjectUtil.isNotEmpty(val)) { | ||||||
|             return (LambdaQueryWrapperX<T>) super.eq(column, val); |             return (LambdaQueryWrapperX<T>) super.eq(column, val); | ||||||
|         } |         } | ||||||
|         return this; |         return this; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public LambdaQueryWrapperX<T> neIfPresent(SFunction<T, ?> column, Object val) { |     public LambdaQueryWrapperX<T> neIfPresent(SFunction<T, ?> column, Object val) { | ||||||
|         if (val instanceof String && StringUtils.hasText((String) val) || !(val instanceof String) && val != null) { |         if (ObjectUtil.isNotEmpty(val)) { | ||||||
|             return (LambdaQueryWrapperX<T>) super.ne(column, val); |             return (LambdaQueryWrapperX<T>) super.ne(column, val); | ||||||
|         } |         } | ||||||
|         return this; |         return this; | ||||||
|  | |||||||
| @ -103,7 +103,7 @@ public interface ErrorCodeConstants { | |||||||
|     ErrorCode TENANT_DISABLE = new ErrorCode(1002015001, "名字为【{}】的租户已被禁用"); |     ErrorCode TENANT_DISABLE = new ErrorCode(1002015001, "名字为【{}】的租户已被禁用"); | ||||||
|     ErrorCode TENANT_EXPIRE = new ErrorCode(1002015002, "名字为【{}】的租户已过期"); |     ErrorCode TENANT_EXPIRE = new ErrorCode(1002015002, "名字为【{}】的租户已过期"); | ||||||
|     ErrorCode TENANT_CAN_NOT_UPDATE_SYSTEM = new ErrorCode(1002015003, "系统租户不能进行修改、删除等操作!"); |     ErrorCode TENANT_CAN_NOT_UPDATE_SYSTEM = new ErrorCode(1002015003, "系统租户不能进行修改、删除等操作!"); | ||||||
|     ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1002015004, "已经存在该名称的租户"); |     ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1002015004, "名字为【{}】的租户已存在"); | ||||||
|  |  | ||||||
|     // ========== 租户套餐 1002016000 ========== |     // ========== 租户套餐 1002016000 ========== | ||||||
|     ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1002016000, "租户套餐不存在"); |     ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1002016000, "租户套餐不存在"); | ||||||
|  | |||||||
| @ -38,12 +38,6 @@ public interface TenantMapper extends BaseMapperX<TenantDO> { | |||||||
|                 .orderByDesc(TenantDO::getId)); |                 .orderByDesc(TenantDO::getId)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     default Long selectCountByName(String name, Long id) { |  | ||||||
|         return selectCount(new LambdaQueryWrapperX<TenantDO>() |  | ||||||
|                 .eqIfPresent(TenantDO::getName, name) |  | ||||||
|                 .neIfPresent(TenantDO::getId, id)); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     default TenantDO selectByName(String name) { |     default TenantDO selectByName(String name) { | ||||||
|         return selectOne(TenantDO::getName, name); |         return selectOne(TenantDO::getName, name); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -29,7 +29,6 @@ import cn.iocoder.yudao.module.system.service.permission.RoleService; | |||||||
| import cn.iocoder.yudao.module.system.service.tenant.handler.TenantInfoHandler; | import cn.iocoder.yudao.module.system.service.tenant.handler.TenantInfoHandler; | ||||||
| import cn.iocoder.yudao.module.system.service.tenant.handler.TenantMenuHandler; | import cn.iocoder.yudao.module.system.service.tenant.handler.TenantMenuHandler; | ||||||
| import cn.iocoder.yudao.module.system.service.user.AdminUserService; | import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||||||
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |  | ||||||
| import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| import org.springframework.context.annotation.Lazy; | import org.springframework.context.annotation.Lazy; | ||||||
| @ -99,8 +98,7 @@ public class TenantServiceImpl implements TenantService { | |||||||
|     @Transactional(rollbackFor = Exception.class) |     @Transactional(rollbackFor = Exception.class) | ||||||
|     public Long createTenant(TenantCreateReqVO createReqVO) { |     public Long createTenant(TenantCreateReqVO createReqVO) { | ||||||
|         // 校验租户名称是否重复 |         // 校验租户名称是否重复 | ||||||
|         validTenantName(createReqVO.getName(), null); |         validTenantNameDuplicate(createReqVO.getName(), null); | ||||||
|  |  | ||||||
|         // 校验套餐被禁用 |         // 校验套餐被禁用 | ||||||
|         TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId()); |         TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId()); | ||||||
|  |  | ||||||
| @ -143,10 +141,8 @@ public class TenantServiceImpl implements TenantService { | |||||||
|     public void updateTenant(TenantUpdateReqVO updateReqVO) { |     public void updateTenant(TenantUpdateReqVO updateReqVO) { | ||||||
|         // 校验存在 |         // 校验存在 | ||||||
|         TenantDO tenant = validateUpdateTenant(updateReqVO.getId()); |         TenantDO tenant = validateUpdateTenant(updateReqVO.getId()); | ||||||
|  |  | ||||||
|         // 校验租户名称是否重复 |         // 校验租户名称是否重复 | ||||||
|         validTenantName(updateReqVO.getName(), updateReqVO.getId()); |         validTenantNameDuplicate(updateReqVO.getName(), updateReqVO.getId()); | ||||||
|  |  | ||||||
|         // 校验套餐被禁用 |         // 校验套餐被禁用 | ||||||
|         TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId()); |         TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId()); | ||||||
|  |  | ||||||
| @ -159,9 +155,17 @@ public class TenantServiceImpl implements TenantService { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     protected void validTenantName(String tenantName, Long id) { |     private void validTenantNameDuplicate(String name, Long id) { | ||||||
|         if (tenantMapper.selectCountByName(tenantName, id) > 0) { |         TenantDO tenant = tenantMapper.selectByName(name); | ||||||
|             throw exception(TENANT_NAME_DUPLICATE); |         if (tenant == null) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |         // 如果 id 为空,说明不用比较是否为相同名字的租户 | ||||||
|  |         if (id == null) { | ||||||
|  |             throw exception(TENANT_NAME_DUPLICATE, name); | ||||||
|  |         } | ||||||
|  |         if (!tenant.getId().equals(id)) { | ||||||
|  |             throw exception(TENANT_NAME_DUPLICATE, name); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | |||||||
| @ -5,7 +5,6 @@ import cn.hutool.core.io.FileUtil; | |||||||
| import cn.hutool.core.util.StrUtil; | import cn.hutool.core.util.StrUtil; | ||||||
| import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | ||||||
| import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||||
| import org.apache.commons.io.FileUtils; |  | ||||||
|  |  | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||||
| @ -87,7 +86,7 @@ public class ProjectReactor { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static Collection<File> listFiles(String projectBaseDir) { |     private static Collection<File> listFiles(String projectBaseDir) { | ||||||
|         Collection<File> files = FileUtils.listFiles(new File(projectBaseDir), null, true); |         Collection<File> files = FileUtil.loopFiles(projectBaseDir); | ||||||
|         // 移除 IDEA、Git 自身的文件、Node 编译出来的文件 |         // 移除 IDEA、Git 自身的文件、Node 编译出来的文件 | ||||||
|         files = files.stream() |         files = files.stream() | ||||||
|                 .filter(file -> !file.getPath().contains(separator + "target" + separator) |                 .filter(file -> !file.getPath().contains(separator + "target" + separator) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV