Browse Source

租户添加或修改时租户名称改为不可以重复,否则会造成重复名称的租户无法正常登录的问题

shizhong 2 years ago
parent
commit
55358aa170

+ 1 - 0
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java

@@ -103,6 +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, "已经存在该名称的租户");
 
     // ========== 租户套餐 1002016000 ==========
     ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1002016000, "租户套餐不存在");

+ 19 - 0
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java

@@ -29,6 +29,7 @@ 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;
@@ -97,6 +98,9 @@ public class TenantServiceImpl implements TenantService {
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Long createTenant(TenantCreateReqVO createReqVO) {
+        // 校验租户名称是否重复
+        validTenantName(createReqVO.getName(), null);
+
         // 校验套餐被禁用
         TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId());
 
@@ -139,6 +143,10 @@ public class TenantServiceImpl implements TenantService {
     public void updateTenant(TenantUpdateReqVO updateReqVO) {
         // 校验存在
         TenantDO tenant = validateUpdateTenant(updateReqVO.getId());
+
+        // 校验租户名称是否重复
+        validTenantName(updateReqVO.getName(), updateReqVO.getId());
+
         // 校验套餐被禁用
         TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId());
 
@@ -151,6 +159,17 @@ public class TenantServiceImpl implements TenantService {
         }
     }
 
+    protected void validTenantName(String tenantName, Long id) {
+        LambdaQueryWrapper<TenantDO> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(TenantDO::getName, tenantName);
+        if (id != null) {
+            wrapper.ne(TenantDO::getId, id);
+        }
+        if (tenantMapper.selectCount(wrapper) > 0) {
+            throw exception(TENANT_NAME_DUPLICATE);
+        }
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {