fix(system/user): 系统内置用户禁止修改角色

Co-authored-by: lishuyan<1206770390@qq.com>



# message auto-generated for no-merge-commit merge:
merge feat/dev into dev

fix(user-role): 🐛 禁止修改系统内置用户的角色分配

Created-by: lishuyanla
Commit-by: lishuyan
Merged-by: Charles_7c
Description: <!--
  非常感谢您的 PR!在提交之前,请务必确保您 PR 的代码经过了完整测试,并且通过了代码规范检查。
-->

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

## PR 类型

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

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

## PR 目的

<!-- 描述一下您的 PR 解决了什么问题。如果可以,请链接到相关 issues。 -->

修复 可以修改系统内置用户的角色分配 的BUG

## 解决方案

<!-- 详细描述您是如何解决的问题 -->

## PR 测试

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

## Changelog

| 模块         | Changelog                                                    | Related issues |
| ------------ | ------------------------------------------------------------ | -------------- |
| 系统管理模块 | fix(user-role): 🐛 禁止修改系统内置用户的角色分配<br/><br/>- 在用户角色分配功能中增加了对系统内置用户的检查<br/>- 如果尝试修改系统内置用户的角色,会抛出异常并提示错误信息<br/>- 这个修改确保了系统内置用户的权限不会被意外更改,提高了系统安全性 |                |

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

## 其他信息

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

## 提交前确认

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

See merge request: continew/continew-admin!8
This commit is contained in:
lishuyanla
2025-07-28 17:31:22 +08:00
committed by Charles_7c
parent 035d37ae2d
commit 93d8168e9f

View File

@@ -32,16 +32,19 @@ import top.continew.admin.common.enums.RoleCodeEnum;
import top.continew.admin.system.constant.SystemConstants;
import top.continew.admin.system.mapper.UserRoleMapper;
import top.continew.admin.system.model.entity.UserRoleDO;
import top.continew.admin.system.model.entity.user.UserDO;
import top.continew.admin.system.model.query.RoleUserQuery;
import top.continew.admin.system.model.resp.role.RoleUserResp;
import top.continew.admin.system.service.RoleService;
import top.continew.admin.system.service.UserRoleService;
import top.continew.admin.system.service.UserService;
import top.continew.starter.core.util.CollUtils;
import top.continew.starter.core.util.validation.CheckUtils;
import top.continew.starter.data.util.QueryWrapperHelper;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@@ -59,6 +62,9 @@ public class UserRoleServiceImpl implements UserRoleService {
@Lazy
@Resource
private RoleService roleService;
@Lazy
@Resource
private UserService userService;
@Override
@AutoOperate(type = RoleUserResp.class, on = "list")
@@ -79,6 +85,11 @@ public class UserRoleServiceImpl implements UserRoleService {
@Override
@Transactional(rollbackFor = Exception.class)
public boolean assignRolesToUser(List<Long> roleIds, Long userId) {
UserDO userDO = userService.getById(userId);
if (Boolean.TRUE.equals(userDO.getIsSystem())) {
Collection<Long> disjunctionRoleIds = CollUtil.disjunction(roleIds, this.listRoleIdByUserId(userId));
CheckUtils.throwIfNotEmpty(disjunctionRoleIds, "[{}] 是系统内置用户,不允许变更角色", userDO.getNickname());
}
// 超级管理员和租户管理员角色不允许分配
CheckUtils.throwIf(roleIds.contains(SystemConstants.SUPER_ADMIN_ROLE_ID), "不允许分配超级管理员角色");
Set<String> roleCodeSet = CollUtils.mapToSet(roleService.listByUserId(userId), RoleContext::getCode);