فهرست منبع

创建和注册用户查询租户下用户个数,临时更改不拼接租户,只查询自己租户下的用户数

liyanbo 3 هفته پیش
والد
کامیت
0c6e1687fe

+ 24 - 7
byzs-module-system/src/main/java/cn/iocoder/byzs/module/system/service/user/AdminUserServiceImpl.java

@@ -11,6 +11,7 @@ import cn.iocoder.byzs.framework.common.util.collection.CollectionUtils;
 import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
 import cn.iocoder.byzs.framework.common.util.validation.ValidationUtils;
 import cn.iocoder.byzs.framework.datapermission.core.util.DataPermissionUtils;
+import cn.iocoder.byzs.framework.tenant.core.context.TenantContextHolder;
 import cn.iocoder.byzs.framework.tenant.core.util.TenantUtils;
 import cn.iocoder.byzs.module.infra.api.config.ConfigApi;
 import cn.iocoder.byzs.module.system.controller.admin.auth.vo.AuthRegisterReqVO;
@@ -92,9 +93,17 @@ public class AdminUserServiceImpl implements AdminUserService {
     public Long createUser(UserSaveReqVO createReqVO) {
         // 1.1 校验账户配合
         tenantService.handleTenantInfo(tenant -> {
-            long count = userMapper.selectCount();
-            if (count >= tenant.getAccountCount()) {
-                throw exception(USER_COUNT_MAX, tenant.getAccountCount());
+            // 设置拼接租户,确保只查询当前租户的用户数量
+            boolean oldSplice = TenantContextHolder.isSplice();
+            try {
+                TenantContextHolder.setSplice(true);
+                long count = userMapper.selectCount();
+                if (count >= tenant.getAccountCount()) {
+                    throw exception(USER_COUNT_MAX, tenant.getAccountCount());
+                }
+            } finally {
+                // 恢复原始状态
+                TenantContextHolder.setSplice(oldSplice);
             }
         });
         // 1.2 校验正确性
@@ -124,9 +133,17 @@ public class AdminUserServiceImpl implements AdminUserService {
         }
         // 1.2 校验账户配合
         tenantService.handleTenantInfo(tenant -> {
-            long count = userMapper.selectCount();
-            if (count >= tenant.getAccountCount()) {
-                throw exception(USER_COUNT_MAX, tenant.getAccountCount());
+            // 设置拼接租户,确保只查询当前租户的用户数量
+            boolean oldSplice = TenantContextHolder.isSplice();
+            try {
+                TenantContextHolder.setSplice(true);
+                long count = userMapper.selectCount();
+                if (count >= tenant.getAccountCount()) {
+                    throw exception(USER_COUNT_MAX, tenant.getAccountCount());
+                }
+            } finally {
+                // 恢复原始状态
+                TenantContextHolder.setSplice(oldSplice);
             }
         });
         // 1.3 校验正确性
@@ -545,4 +562,4 @@ public class AdminUserServiceImpl implements AdminUserService {
         return passwordEncoder.encode(password);
     }
 
-}
+}