Commit 5c0161d762f1fe1b7c067970f9d8d2c49c8a08aa
1 parent
aa16b827
修复租户名的重复问题
Showing
6 changed files
with
21 additions
and
22 deletions
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java
| ... | ... | @@ -78,8 +78,8 @@ public class TenantSecurityWebFilter extends ApiRequestFilter { |
| 78 | 78 | // 2. 如果请求未带租户的编号,不允许访问。 |
| 79 | 79 | if (tenantId == null) { |
| 80 | 80 | log.error("[doFilterInternal][URL({}/{}) 未传递租户编号]", request.getRequestURI(), request.getMethod()); |
| 81 | - String msg = "请求的租户标识未传递,请进行排查"; | |
| 82 | - ServletUtils.writeJSON(response, CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), msg)); | |
| 81 | + ServletUtils.writeJSON(response, CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), | |
| 82 | + "请求的租户标识未传递,请进行排查")); | |
| 83 | 83 | return; |
| 84 | 84 | } |
| 85 | 85 | // 3. 校验租户是合法,例如说被禁用、到期 | ... | ... |
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/query/LambdaQueryWrapperX.java
| 1 | 1 | package cn.iocoder.yudao.framework.mybatis.core.query; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.ArrayUtil; |
| 4 | +import cn.hutool.core.util.ObjectUtil; | |
| 5 | +import cn.hutool.core.util.StrUtil; | |
| 4 | 6 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 5 | 7 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 6 | 8 | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| ... | ... | @@ -40,14 +42,14 @@ public class LambdaQueryWrapperX<T> extends LambdaQueryWrapper<T> { |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | 44 | public LambdaQueryWrapperX<T> eqIfPresent(SFunction<T, ?> column, Object val) { |
| 43 | - if (val instanceof String && StringUtils.hasText((String) val) || !(val instanceof String) && val != null) { | |
| 45 | + if (ObjectUtil.isNotEmpty(val)) { | |
| 44 | 46 | return (LambdaQueryWrapperX<T>) super.eq(column, val); |
| 45 | 47 | } |
| 46 | 48 | return this; |
| 47 | 49 | } |
| 48 | 50 | |
| 49 | 51 | public LambdaQueryWrapperX<T> neIfPresent(SFunction<T, ?> column, Object val) { |
| 50 | - if (val instanceof String && StringUtils.hasText((String) val) || !(val instanceof String) && val != null) { | |
| 52 | + if (ObjectUtil.isNotEmpty(val)) { | |
| 51 | 53 | return (LambdaQueryWrapperX<T>) super.ne(column, val); |
| 52 | 54 | } |
| 53 | 55 | return this; | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
| ... | ... | @@ -103,7 +103,7 @@ public interface ErrorCodeConstants { |
| 103 | 103 | ErrorCode TENANT_DISABLE = new ErrorCode(1002015001, "名字为【{}】的租户已被禁用"); |
| 104 | 104 | ErrorCode TENANT_EXPIRE = new ErrorCode(1002015002, "名字为【{}】的租户已过期"); |
| 105 | 105 | ErrorCode TENANT_CAN_NOT_UPDATE_SYSTEM = new ErrorCode(1002015003, "系统租户不能进行修改、删除等操作!"); |
| 106 | - ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1002015004, "已经存在该名称的租户"); | |
| 106 | + ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1002015004, "名字为【{}】的租户已存在"); | |
| 107 | 107 | |
| 108 | 108 | // ========== 租户套餐 1002016000 ========== |
| 109 | 109 | ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1002016000, "租户套餐不存在"); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenant/TenantMapper.java
| ... | ... | @@ -38,12 +38,6 @@ public interface TenantMapper extends BaseMapperX<TenantDO> { |
| 38 | 38 | .orderByDesc(TenantDO::getId)); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - default Long selectCountByName(String name, Long id) { | |
| 42 | - return selectCount(new LambdaQueryWrapperX<TenantDO>() | |
| 43 | - .eqIfPresent(TenantDO::getName, name) | |
| 44 | - .neIfPresent(TenantDO::getId, id)); | |
| 45 | - } | |
| 46 | - | |
| 47 | 41 | default TenantDO selectByName(String name) { |
| 48 | 42 | return selectOne(TenantDO::getName, name); |
| 49 | 43 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java
| ... | ... | @@ -29,7 +29,6 @@ import cn.iocoder.yudao.module.system.service.permission.RoleService; |
| 29 | 29 | import cn.iocoder.yudao.module.system.service.tenant.handler.TenantInfoHandler; |
| 30 | 30 | import cn.iocoder.yudao.module.system.service.tenant.handler.TenantMenuHandler; |
| 31 | 31 | import cn.iocoder.yudao.module.system.service.user.AdminUserService; |
| 32 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 33 | 32 | import lombok.extern.slf4j.Slf4j; |
| 34 | 33 | import org.springframework.beans.factory.annotation.Autowired; |
| 35 | 34 | import org.springframework.context.annotation.Lazy; |
| ... | ... | @@ -99,8 +98,7 @@ public class TenantServiceImpl implements TenantService { |
| 99 | 98 | @Transactional(rollbackFor = Exception.class) |
| 100 | 99 | public Long createTenant(TenantCreateReqVO createReqVO) { |
| 101 | 100 | // 校验租户名称是否重复 |
| 102 | - validTenantName(createReqVO.getName(), null); | |
| 103 | - | |
| 101 | + validTenantNameDuplicate(createReqVO.getName(), null); | |
| 104 | 102 | // 校验套餐被禁用 |
| 105 | 103 | TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId()); |
| 106 | 104 | |
| ... | ... | @@ -143,10 +141,8 @@ public class TenantServiceImpl implements TenantService { |
| 143 | 141 | public void updateTenant(TenantUpdateReqVO updateReqVO) { |
| 144 | 142 | // 校验存在 |
| 145 | 143 | TenantDO tenant = validateUpdateTenant(updateReqVO.getId()); |
| 146 | - | |
| 147 | 144 | // 校验租户名称是否重复 |
| 148 | - validTenantName(updateReqVO.getName(), updateReqVO.getId()); | |
| 149 | - | |
| 145 | + validTenantNameDuplicate(updateReqVO.getName(), updateReqVO.getId()); | |
| 150 | 146 | // 校验套餐被禁用 |
| 151 | 147 | TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId()); |
| 152 | 148 | |
| ... | ... | @@ -159,9 +155,17 @@ public class TenantServiceImpl implements TenantService { |
| 159 | 155 | } |
| 160 | 156 | } |
| 161 | 157 | |
| 162 | - protected void validTenantName(String tenantName, Long id) { | |
| 163 | - if (tenantMapper.selectCountByName(tenantName, id) > 0) { | |
| 164 | - throw exception(TENANT_NAME_DUPLICATE); | |
| 158 | + private void validTenantNameDuplicate(String name, Long id) { | |
| 159 | + TenantDO tenant = tenantMapper.selectByName(name); | |
| 160 | + if (tenant == null) { | |
| 161 | + return; | |
| 162 | + } | |
| 163 | + // 如果 id 为空,说明不用比较是否为相同名字的租户 | |
| 164 | + if (id == null) { | |
| 165 | + throw exception(TENANT_NAME_DUPLICATE, name); | |
| 166 | + } | |
| 167 | + if (!tenant.getId().equals(id)) { | |
| 168 | + throw exception(TENANT_NAME_DUPLICATE, name); | |
| 165 | 169 | } |
| 166 | 170 | } |
| 167 | 171 | ... | ... |
yudao-server/src/test/java/cn/iocoder/yudao/ProjectReactor.java
| ... | ... | @@ -5,7 +5,6 @@ import cn.hutool.core.io.FileUtil; |
| 5 | 5 | import cn.hutool.core.util.StrUtil; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.util.collection.SetUtils; |
| 7 | 7 | import lombok.extern.slf4j.Slf4j; |
| 8 | -import org.apache.commons.io.FileUtils; | |
| 9 | 8 | |
| 10 | 9 | import java.io.File; |
| 11 | 10 | import java.nio.charset.StandardCharsets; |
| ... | ... | @@ -87,7 +86,7 @@ public class ProjectReactor { |
| 87 | 86 | } |
| 88 | 87 | |
| 89 | 88 | private static Collection<File> listFiles(String projectBaseDir) { |
| 90 | - Collection<File> files = FileUtils.listFiles(new File(projectBaseDir), null, true); | |
| 89 | + Collection<File> files = FileUtil.loopFiles(projectBaseDir); | |
| 91 | 90 | // 移除 IDEA、Git 自身的文件、Node 编译出来的文件 |
| 92 | 91 | files = files.stream() |
| 93 | 92 | .filter(file -> !file.getPath().contains(separator + "target" + separator) | ... | ... |