From 5e7a2a4a7458ddffb065edab5e2e1ac0fb4a4f9e Mon Sep 17 00:00:00 2001 From: kiki1373639299 Date: Wed, 22 Oct 2025 15:44:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(system/auth):=20=E4=BF=AE=E5=A4=8D=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=96=B9=E7=99=BB=E5=BD=95=E7=94=A8=E6=88=B7=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E9=83=A8=E9=97=A8=E9=85=8D=E7=BD=AE=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: KAI<1373639299@qq.com> Co-authored-by: kiki1373639299 # message auto-generated for no-merge-commit merge: !15 merge dev into dev feat(system):优化第三方登录与用户部门关联逻辑 Created-by: kiki1373639299 Commit-by: kiki1373639299;KAI Merged-by: Charles_7c Description: ## PR 类型 - [x] 新 feature - [ ] Bug 修复 - [ ] 功能增强 - [ ] 文档变更 - [ ] 代码样式变更 - [ ] 重构 - [ ] 性能改进 - [ ] 单元测试 - [ ] CI/CD - [ ] 其他 ## PR 目的 优化第三方登录与用户部门关联逻辑 ## 解决方案 - 为 sys_user_social 表的索引添加 tenant_id 字段支持多租户唯一性 - 第三方登录时默认分配系统内置根部门而非硬编码部门ID - 删除已废弃的 SystemConstants.SUPER_DEPT_ID 常量定义 - 用户查询条件中移除对超级部门ID的特殊判断- 引入事务管理确保第三方账号绑定 ## PR 测试 ## Changelog | 模块 | Changelog | Related issues | |-----|-----------| -------------- | | | | | ## 其他信息 ## 提交前确认 - [x] PR 代码经过了完整测试,并且通过了代码规范检查 - [ ] 已经完整填写 Changelog,并链接到了相关 issues - [x] PR 代码将要提交到 dev 分支 See merge request: continew/continew-admin!15 --- .../db/changelog/mysql/plugin/plugin_tenant.sql | 8 +++++++- .../changelog/postgresql/plugin/plugin_tenant.sql | 9 ++++++++- .../admin/auth/handler/SocialLoginHandler.java | 13 +++++++++++-- .../admin/system/constant/SystemConstants.java | 5 ----- .../admin/system/service/impl/UserServiceImpl.java | 3 +-- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/continew-server/src/main/resources/db/changelog/mysql/plugin/plugin_tenant.sql b/continew-server/src/main/resources/db/changelog/mysql/plugin/plugin_tenant.sql index e2cbed06..62f160d7 100644 --- a/continew-server/src/main/resources/db/changelog/mysql/plugin/plugin_tenant.sql +++ b/continew-server/src/main/resources/db/changelog/mysql/plugin/plugin_tenant.sql @@ -134,4 +134,10 @@ VALUES (3022, '详情', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:get', 2, 1, 1, NOW()), (3023, '新增', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:create', 3, 1, 1, NOW()), (3024, '修改', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:update', 4, 1, 1, NOW()), -(3025, '删除', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:delete', 5, 1, 1, NOW()); \ No newline at end of file +(3025, '删除', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:delete', 5, 1, 1, NOW()); + +-- changeset kai:20251022-01 +-- comment 重置租户场景索引 +ALTER TABLE `sys_user_social` +DROP INDEX `uk_source_open_id`, + ADD UNIQUE INDEX `uk_source_open_id` (`source`, `open_id`, `tenant_id`); \ No newline at end of file diff --git a/continew-server/src/main/resources/db/changelog/postgresql/plugin/plugin_tenant.sql b/continew-server/src/main/resources/db/changelog/postgresql/plugin/plugin_tenant.sql index 87d88aa6..48f784e9 100644 --- a/continew-server/src/main/resources/db/changelog/postgresql/plugin/plugin_tenant.sql +++ b/continew-server/src/main/resources/db/changelog/postgresql/plugin/plugin_tenant.sql @@ -172,4 +172,11 @@ VALUES (3022, '详情', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:get', 2, 1, 1, NOW()), (3023, '新增', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:create', 3, 1, 1, NOW()), (3024, '修改', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:update', 4, 1, 1, NOW()), -(3025, '删除', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:delete', 5, 1, 1, NOW()); \ No newline at end of file +(3025, '删除', 3020, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'tenant:package:delete', 5, 1, 1, NOW()); + +-- changeset kai:20251022-01 +-- comment 重置租户场景索引 +DROP INDEX IF EXISTS "uk_user_source_open_id"; +CREATE UNIQUE INDEX "uk_user_source_open_id" + ON "sys_user_social" ("source", "open_id", "tenant_id"); + diff --git a/continew-system/src/main/java/top/continew/admin/auth/handler/SocialLoginHandler.java b/continew-system/src/main/java/top/continew/admin/auth/handler/SocialLoginHandler.java index 606f9664..dfb2f935 100644 --- a/continew-system/src/main/java/top/continew/admin/auth/handler/SocialLoginHandler.java +++ b/continew-system/src/main/java/top/continew/admin/auth/handler/SocialLoginHandler.java @@ -23,6 +23,7 @@ import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.json.JSONUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.xkcoding.justauth.autoconfigure.JustAuthProperties; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; @@ -33,6 +34,7 @@ import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.request.AuthRequest; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; import top.continew.admin.auth.AbstractLoginHandler; import top.continew.admin.auth.enums.AuthTypeEnum; import top.continew.admin.auth.model.req.SocialLoginReq; @@ -41,13 +43,14 @@ import top.continew.admin.common.constant.RegexConstants; import top.continew.admin.common.enums.DisEnableStatusEnum; import top.continew.admin.common.enums.GenderEnum; import top.continew.admin.common.enums.RoleCodeEnum; -import top.continew.admin.system.constant.SystemConstants; import top.continew.admin.system.enums.MessageTemplateEnum; import top.continew.admin.system.enums.MessageTypeEnum; +import top.continew.admin.system.model.entity.DeptDO; import top.continew.admin.system.model.entity.user.UserDO; import top.continew.admin.system.model.entity.user.UserSocialDO; import top.continew.admin.system.model.req.MessageReq; import top.continew.admin.system.model.resp.ClientResp; +import top.continew.admin.system.service.DeptService; import top.continew.admin.system.service.MessageService; import top.continew.admin.system.service.UserRoleService; import top.continew.admin.system.service.UserSocialService; @@ -74,8 +77,10 @@ public class SocialLoginHandler extends AbstractLoginHandler { private final UserRoleService userRoleService; private final MessageService messageService; private final ApplicationProperties applicationProperties; + private final DeptService deptService; @Override + @Transactional public LoginResp login(SocialLoginReq req, ClientResp client, HttpServletRequest request) { // 获取第三方登录信息 AuthRequest authRequest = this.getAuthRequest(req.getSource()); @@ -108,7 +113,11 @@ public class SocialLoginHandler extends AbstractLoginHandler { user.setGender(GenderEnum.valueOf(authUser.getGender().name())); } user.setAvatar(authUser.getAvatar()); - user.setDeptId(SystemConstants.SUPER_DEPT_ID); + // 默认设置为系统内置数据的根部门 如果需要设置其他部门自行替换查询条件 + DeptDO deptDO = deptService.getOne(new LambdaQueryWrapper().eq(DeptDO::getIsSystem, true) + .eq(DeptDO::getParentId, 0)); + ValidationUtils.throwIf(deptDO == null, "未查询到系统内置部门"); + user.setDeptId(deptDO.getId()); user.setStatus(DisEnableStatusEnum.ENABLE); userService.save(user); Long userId = user.getId(); diff --git a/continew-system/src/main/java/top/continew/admin/system/constant/SystemConstants.java b/continew-system/src/main/java/top/continew/admin/system/constant/SystemConstants.java index c45c77c2..fd1c1c18 100644 --- a/continew-system/src/main/java/top/continew/admin/system/constant/SystemConstants.java +++ b/continew-system/src/main/java/top/continew/admin/system/constant/SystemConstants.java @@ -29,11 +29,6 @@ public class SystemConstants { */ public static final Long SUPER_ADMIN_ROLE_ID = 1L; - /** - * 顶级部门 ID - */ - public static final Long SUPER_DEPT_ID = 1L; - /** * 全部权限标识 */ diff --git a/continew-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java b/continew-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java index c9139a0a..b8f98a25 100644 --- a/continew-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java +++ b/continew-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java @@ -60,7 +60,6 @@ import top.continew.admin.common.context.UserContextHolder; import top.continew.admin.common.enums.DisEnableStatusEnum; import top.continew.admin.common.enums.GenderEnum; import top.continew.admin.common.util.SecureUtils; -import top.continew.admin.system.constant.SystemConstants; import top.continew.admin.system.enums.OptionCategoryEnum; import top.continew.admin.system.mapper.user.UserMapper; import top.continew.admin.system.model.entity.DeptDO; @@ -505,7 +504,7 @@ public class UserServiceImpl extends BaseServiceImpl { + .and(deptId != null, q -> { List deptIdList = CollUtils.mapToList(deptService.listChildren(deptId), DeptDO::getId); deptIdList.add(deptId); q.in("t1.dept_id", deptIdList);