修复租户名的重复问题

This commit is contained in:
YunaiV 2023-04-09 11:12:24 +08:00
parent aa16b8279f
commit 5c0161d762
6 changed files with 21 additions and 22 deletions

View File

@ -78,8 +78,8 @@ public class TenantSecurityWebFilter extends ApiRequestFilter {
// 2. 如果请求未带租户的编号不允许访问
if (tenantId == null) {
log.error("[doFilterInternal][URL({}/{}) 未传递租户编号]", request.getRequestURI(), request.getMethod());
String msg = "请求的租户标识未传递,请进行排查";
ServletUtils.writeJSON(response, CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), msg));
ServletUtils.writeJSON(response, CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(),
"请求的租户标识未传递,请进行排查"));
return;
}
// 3. 校验租户是合法例如说被禁用到期

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.framework.mybatis.core.query;
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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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) {
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 this;
}
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 this;

View File

@ -103,7 +103,7 @@ public interface ErrorCodeConstants {
ErrorCode TENANT_DISABLE = new ErrorCode(1002015001, "名字为【{}】的租户已被禁用");
ErrorCode TENANT_EXPIRE = new ErrorCode(1002015002, "名字为【{}】的租户已过期");
ErrorCode TENANT_CAN_NOT_UPDATE_SYSTEM = new ErrorCode(1002015003, "系统租户不能进行修改、删除等操作!");
ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1002015004, "已经存在该名称的租户");
ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1002015004, "名字为【{}】的租户已存在");
// ========== 租户套餐 1002016000 ==========
ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1002016000, "租户套餐不存在");

View File

@ -38,12 +38,6 @@ public interface TenantMapper extends BaseMapperX<TenantDO> {
.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) {
return selectOne(TenantDO::getName, name);
}

View File

@ -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.TenantMenuHandler;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
@ -99,8 +98,7 @@ public class TenantServiceImpl implements TenantService {
@Transactional(rollbackFor = Exception.class)
public Long createTenant(TenantCreateReqVO createReqVO) {
// 校验租户名称是否重复
validTenantName(createReqVO.getName(), null);
validTenantNameDuplicate(createReqVO.getName(), null);
// 校验套餐被禁用
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId());
@ -143,10 +141,8 @@ public class TenantServiceImpl implements TenantService {
public void updateTenant(TenantUpdateReqVO updateReqVO) {
// 校验存在
TenantDO tenant = validateUpdateTenant(updateReqVO.getId());
// 校验租户名称是否重复
validTenantName(updateReqVO.getName(), updateReqVO.getId());
validTenantNameDuplicate(updateReqVO.getName(), updateReqVO.getId());
// 校验套餐被禁用
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId());
@ -159,9 +155,17 @@ public class TenantServiceImpl implements TenantService {
}
}
protected void validTenantName(String tenantName, Long id) {
if (tenantMapper.selectCountByName(tenantName, id) > 0) {
throw exception(TENANT_NAME_DUPLICATE);
private void validTenantNameDuplicate(String name, Long id) {
TenantDO tenant = tenantMapper.selectByName(name);
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);
}
}

View File

@ -5,7 +5,6 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.nio.charset.StandardCharsets;
@ -87,7 +86,7 @@ public class ProjectReactor {
}
private static Collection<File> listFiles(String projectBaseDir) {
Collection<File> files = FileUtils.listFiles(new File(projectBaseDir), null, true);
Collection<File> files = FileUtil.loopFiles(projectBaseDir);
// 移除 IDEAGit 自身的文件Node 编译出来的文件
files = files.stream()
.filter(file -> !file.getPath().contains(separator + "target" + separator)