-
Notifications
You must be signed in to change notification settings - Fork 409
Copilot/fix admin role assignment #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,6 +164,10 @@ public void updatePermission(Long id, RolePermissionUpdateReq req) { | |
| public void assignToUsers(Long id, List<Long> userIds) { | ||
| RoleDO role = super.getById(id); | ||
| CheckUtils.throwIf(Boolean.TRUE.equals(role.getIsSystem()), "[{}] 是系统内置角色,不允许分配角色给其他用户", role.getName()); | ||
| // 防止将系统内置用户分配给非超级管理员角色 | ||
| if (!SystemConstants.SUPER_ADMIN_ROLE_ID.equals(id)) { | ||
| userRoleService.checkSystemUserAssignment(userIds); | ||
| } | ||
|
Comment on lines
+167
to
+170
|
||
| // 保存用户和角色关联 | ||
| userRoleService.assignRoleToUsers(id, userIds); | ||
| // 更新用户上下文 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,8 @@ public boolean assignRoleToUsers(Long roleId, List<Long> userIds) { | |
|
|
||
| @Override | ||
| public void deleteByIds(List<Long> ids) { | ||
| // 检查是否包含系统内置用户的角色关联 | ||
| this.checkSystemUserUnassignment(ids); | ||
| baseMapper.deleteByIds(ids); | ||
| } | ||
|
|
||
|
|
@@ -165,4 +167,42 @@ public boolean isRoleIdExists(List<Long> roleIds) { | |
| } | ||
| return baseMapper.lambdaQuery().in(UserRoleDO::getRoleId, roleIds).exists(); | ||
| } | ||
|
|
||
| @Override | ||
| public void checkSystemUserAssignment(List<Long> userIds) { | ||
| if (CollUtil.isEmpty(userIds)) { | ||
| return; | ||
| } | ||
| // 查询用户列表中是否包含系统内置用户 | ||
| List<UserDO> systemUsers = userService.lambdaQuery() | ||
| .select(UserDO::getId, UserDO::getNickname) | ||
| .in(UserDO::getId, userIds) | ||
| .eq(UserDO::getIsSystem, true) | ||
| .list(); | ||
| CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许分配给非超级管理员角色", systemUsers.get(0).getNickname()); | ||
| } | ||
|
Comment on lines
+170
to
+183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Java 方法参数是即时求值的。第 182 行 🐛 建议修复:仅在非空时获取昵称- CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许分配给非超级管理员角色", systemUsers.get(0).getNickname());
+ if (CollUtil.isNotEmpty(systemUsers)) {
+ CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许分配给非超级管理员角色", systemUsers.get(0).getNickname());
+ }或者更简洁地: - CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许分配给非超级管理员角色", systemUsers.get(0).getNickname());
+ CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许分配给非超级管理员角色",
+ CollUtil.isEmpty(systemUsers) ? "" : systemUsers.get(0).getNickname());🤖 Prompt for AI Agents |
||
|
|
||
| @Override | ||
| public void checkSystemUserUnassignment(List<Long> userRoleIds) { | ||
| if (CollUtil.isEmpty(userRoleIds)) { | ||
| return; | ||
| } | ||
| // 查询用户角色关联列表 | ||
| List<UserRoleDO> userRoleList = baseMapper.lambdaQuery() | ||
| .select(UserRoleDO::getUserId) | ||
| .in(UserRoleDO::getId, userRoleIds) | ||
| .list(); | ||
| if (CollUtil.isEmpty(userRoleList)) { | ||
| return; | ||
| } | ||
| // 获取用户ID列表 | ||
| List<Long> userIds = userRoleList.stream().map(UserRoleDO::getUserId).distinct().toList(); | ||
| // 查询是否包含系统内置用户 | ||
| List<UserDO> systemUsers = userService.lambdaQuery() | ||
| .select(UserDO::getId, UserDO::getNickname) | ||
| .in(UserDO::getId, userIds) | ||
| .eq(UserDO::getIsSystem, true) | ||
| .list(); | ||
| CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许取消分配角色", systemUsers.get(0).getNickname()); | ||
| } | ||
|
Comment on lines
+185
to
+207
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同样的 第 206 行与 🐛 建议修复- CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许取消分配角色", systemUsers.get(0).getNickname());
+ if (CollUtil.isNotEmpty(systemUsers)) {
+ CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许取消分配角色", systemUsers.get(0).getNickname());
+ }🤖 Prompt for AI Agents
Comment on lines
+186
to
+207
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for these methods should clarify their specific purpose regarding super admin roles. The current descriptions don't mention that checkSystemUserAssignment is intended to prevent assignment to non-super-admin roles, and checkSystemUserUnassignment should (but currently doesn't) allow unassignment of super admin roles. Consider updating the documentation to be more specific about the relationship with super admin roles.