From b2b6b1306ff52696b1ee724cd7f07271572f9980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Thu, 22 Jan 2026 11:31:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(user):=20=E4=BF=AE=E5=A4=8D=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=B3=A8=E5=86=8C=E4=B8=AD=E7=9A=84=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=92=8C=E8=A7=92=E8=89=B2=E5=88=86=E9=85=8D?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修正了超级管理员标识符变量名避免混淆 - 为普通用户注册添加租户ID获取逻辑,默认使用平台租户(5) - 在用户参数中传递租户ID和管理员状态信息 - 添加了对新用户的租户级别角色管理支持 - 实现了缺失用户角色的自动创建机制 - 增强了角色查询以考虑租户隔离 - 添加了对租户角色不存在情况的异常处理 --- .../system/controller/MainController.java | 16 +++++++++---- .../system/service/impl/UserServiceImpl.java | 23 ++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gxwebsoft/common/system/controller/MainController.java b/src/main/java/com/gxwebsoft/common/system/controller/MainController.java index 0bb4dbb..b58c9fc 100644 --- a/src/main/java/com/gxwebsoft/common/system/controller/MainController.java +++ b/src/main/java/com/gxwebsoft/common/system/controller/MainController.java @@ -626,10 +626,16 @@ public class MainController extends BaseController { String password = user.getPassword(); // 密码 String code = user.getCode(); // 短信验证码 String email = user.getEmail(); // 邮箱 + Boolean isAdmin = Boolean.TRUE.equals(user.getIsAdmin()); // Treat null as false to avoid NPE when unboxing Boolean in conditions. - final boolean isAdmin = Boolean.TRUE.equals(user.getIsSuperAdmin()); // 是否注册为超级管理员(是=>创建租户) + final boolean isSuperAdmin = Boolean.TRUE.equals(user.getIsSuperAdmin()); // 是否注册为超级管理员(是=>创建租户) - if (!isAdmin) { + if (!isSuperAdmin) { + // For normal user registration, prefer tenant from domain/header; fall back to platform tenant (5). + Integer tenantId = getTenantId(); + if (tenantId == null) { + tenantId = 5; + } // 短信验证 if (!StrUtil.equals(code, cacheClient.get(phone, String.class)) && !StrUtil.equals(code, redisUtil.get(CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS))) { throw new BusinessException("验证码不正确"); @@ -642,10 +648,12 @@ public class MainController extends BaseController { if (byPhone == null) { final UserParam userParam = new UserParam(); userParam.setPhone(phone); + userParam.setTenantId(tenantId); userParam.setEmail(email); userParam.setPassword(password); userParam.setUsername(username); userParam.setNickname(DesensitizedUtil.mobilePhone(phone)); + userParam.setIsAdmin(isAdmin); if (user.getTemplateId() != null) { userParam.setTemplateId(user.getTemplateId()); } @@ -786,7 +794,7 @@ public class MainController extends BaseController { String code = user.getCode(); // 短信验证码 String email = user.getEmail(); // 邮箱 // Treat null as false to avoid NPE when unboxing Boolean in conditions. - final boolean isAdmin = Boolean.TRUE.equals(user.getIsSuperAdmin()); // 是否注册为超级管理员(是=>创建租户) + final boolean isSuperAdmin = Boolean.TRUE.equals(user.getIsSuperAdmin()); // 是否注册为超级管理员(是=>创建租户) // 会员资料 final UserParam userParam = new UserParam(); @@ -808,7 +816,7 @@ public class MainController extends BaseController { userParam.setTemplateId(user.getTemplateId()); } - if (!isAdmin) { + if (!isSuperAdmin) { // 短信验证 if (!StrUtil.equals(code, cacheClient.get(phone, String.class)) && !StrUtil.equals(code, redisUtil.get(CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS))) { throw new BusinessException("验证码不正确"); diff --git a/src/main/java/com/gxwebsoft/common/system/service/impl/UserServiceImpl.java b/src/main/java/com/gxwebsoft/common/system/service/impl/UserServiceImpl.java index dd99a96..5ad6f46 100644 --- a/src/main/java/com/gxwebsoft/common/system/service/impl/UserServiceImpl.java +++ b/src/main/java/com/gxwebsoft/common/system/service/impl/UserServiceImpl.java @@ -261,6 +261,9 @@ public class UserServiceImpl extends ServiceImpl implements Us if(userParam.getRegion() != null){ addUser.setRegion(userParam.getRegion()); } + if(userParam.getIsAdmin() != null){ + addUser.setIsAdmin(userParam.getIsAdmin()); + } if(userParam.getAddress() != null){ addUser.setAddress(userParam.getAddress()); } @@ -273,7 +276,25 @@ public class UserServiceImpl extends ServiceImpl implements Us } addUser.setTenantId(userParam.getTenantId()); addUser.setRecommend(0); - Role role = roleService.getOne(new QueryWrapper().eq("role_code", "user"), false); + // Pick the default "user" role for the tenant. If it doesn't exist (fresh DB / incomplete init), + // create it to avoid NPE during registration/login. + QueryWrapper roleQw = new QueryWrapper().eq("role_code", "admin"); + if (addUser.getTenantId() != null) { + roleQw.eq("tenant_id", addUser.getTenantId()); + } + Role role = roleService.getOne(roleQw, false); + if (role == null && addUser.getTenantId() != null) { + Role defaultRole = new Role(); + defaultRole.setRoleName("注册用户"); + defaultRole.setRoleCode("user"); + defaultRole.setComments("普通注册用户"); + defaultRole.setTenantId(addUser.getTenantId()); + roleService.save(defaultRole); + role = defaultRole; + } + if (role == null) { + throw new BusinessException("缺少默认角色(role_code=user),请先初始化角色"); + } addUser.setRoleId(role.getRoleId()); if (saveUser(addUser)) { // 添加用户角色