fix(system/auth): 修复第三方登录用户默认部门配置错误

Co-authored-by: KAI<1373639299@qq.com>
Co-authored-by: kiki1373639299<zkai0106@163.com>



# 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!在提交之前,请务必确保您 PR 的代码经过了完整测试,并且通过了代码规范检查。
-->

<!-- 在 [] 中输入 x 来勾选) -->

## PR 类型

<!-- 您的 PR 引入了哪种类型的变更? -->
<!-- 只支持选择一种类型,如果有多种类型,可以在更新日志中增加 “类型” 列。 -->

- [x] 新 feature
- [ ] Bug 修复
- [ ] 功能增强
- [ ] 文档变更
- [ ] 代码样式变更
- [ ] 重构
- [ ] 性能改进
- [ ] 单元测试
- [ ] CI/CD
- [ ] 其他

## PR 目的

<!-- 描述一下您的 PR 解决了什么问题。如果可以,请链接到相关 issues。 -->
优化第三方登录与用户部门关联逻辑
## 解决方案
- 为 sys_user_social 表的索引添加 tenant_id 字段支持多租户唯一性
- 第三方登录时默认分配系统内置根部门而非硬编码部门ID
- 删除已废弃的 SystemConstants.SUPER_DEPT_ID 常量定义
- 用户查询条件中移除对超级部门ID的特殊判断- 引入事务管理确保第三方账号绑定
<!-- 详细描述您是如何解决的问题 -->

## PR 测试

<!-- 如果可以,请为您的 PR 添加或更新单元测试。 -->
<!-- 请描述一下您是如何测试 PR 的。例如:创建/更新单元测试或添加相关的截图。 -->

## Changelog

| 模块  | Changelog | Related issues |
|-----|-----------| -------------- |
|     |           |                |

<!-- 如果有多种类型的变更,可以在变更日志表中增加 “类型” 列,该列的值与上方 “PR 类型” 相同。 -->
<!-- Related issues 格式为 Closes #<issue号>,或者 Fixes #<issue号>,或者 Resolves #<issue号>。 -->

## 其他信息

<!-- 请描述一下还有哪些注意事项。例如:如果引入了一个不向下兼容的变更,请描述其影响。 -->

## 提交前确认

- [x] PR 代码经过了完整测试,并且通过了代码规范检查
- [ ] 已经完整填写 Changelog,并链接到了相关 issues
- [x] PR 代码将要提交到 dev 分支

See merge request: continew/continew-admin!15
This commit is contained in:
kiki1373639299
2025-10-22 15:44:17 +08:00
committed by Charles_7c
parent 6e7db4f418
commit 5e7a2a4a74
5 changed files with 27 additions and 11 deletions

View File

@@ -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());
(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`);

View File

@@ -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());
(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");

View File

@@ -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<SocialLoginReq> {
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<SocialLoginReq> {
user.setGender(GenderEnum.valueOf(authUser.getGender().name()));
}
user.setAvatar(authUser.getAvatar());
user.setDeptId(SystemConstants.SUPER_DEPT_ID);
// 默认设置为系统内置数据的根部门 如果需要设置其他部门自行替换查询条件
DeptDO deptDO = deptService.getOne(new LambdaQueryWrapper<DeptDO>().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();

View File

@@ -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;
/**
* 全部权限标识
*/

View File

@@ -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<UserMapper, UserDO, UserRes
.eq(status != null, "t1.status", status)
.between(CollUtil.isNotEmpty(createTimeList), "t1.create_time", CollUtil.getFirst(createTimeList), CollUtil
.getLast(createTimeList))
.and(deptId != null && !SystemConstants.SUPER_DEPT_ID.equals(deptId), q -> {
.and(deptId != null, q -> {
List<Long> deptIdList = CollUtils.mapToList(deptService.listChildren(deptId), DeptDO::getId);
deptIdList.add(deptId);
q.in("t1.dept_id", deptIdList);