Skip to content

Commit eac02db

Browse files
Copilotgopkg-dev
andcommitted
Add validation to prevent system users from being assigned to non-admin roles
Co-authored-by: gopkg-dev <58848833+gopkg-dev@users.noreply.github.com>
1 parent 1e9fbea commit eac02db

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

continew-system/src/main/java/top/continew/admin/system/service/UserRoleService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,18 @@ public interface UserRoleService {
103103
* @return 是否已关联(true:已关联;false:未关联)
104104
*/
105105
boolean isRoleIdExists(List<Long> roleIds);
106+
107+
/**
108+
* 检查系统内置用户是否在用户列表中
109+
*
110+
* @param userIds 用户 ID 列表
111+
*/
112+
void checkSystemUserAssignment(List<Long> userIds);
113+
114+
/**
115+
* 检查系统内置用户是否在用户角色关联列表中
116+
*
117+
* @param userRoleIds 用户角色关联 ID 列表
118+
*/
119+
void checkSystemUserUnassignment(List<Long> userRoleIds);
106120
}

continew-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ public void updatePermission(Long id, RolePermissionUpdateReq req) {
164164
public void assignToUsers(Long id, List<Long> userIds) {
165165
RoleDO role = super.getById(id);
166166
CheckUtils.throwIf(Boolean.TRUE.equals(role.getIsSystem()), "[{}] 是系统内置角色,不允许分配角色给其他用户", role.getName());
167+
// 防止将系统内置用户分配给非超级管理员角色
168+
if (!SystemConstants.SUPER_ADMIN_ROLE_ID.equals(id)) {
169+
userRoleService.checkSystemUserAssignment(userIds);
170+
}
167171
// 保存用户和角色关联
168172
userRoleService.assignRoleToUsers(id, userIds);
169173
// 更新用户上下文

continew-system/src/main/java/top/continew/admin/system/service/impl/UserRoleServiceImpl.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public boolean assignRoleToUsers(Long roleId, List<Long> userIds) {
120120

121121
@Override
122122
public void deleteByIds(List<Long> ids) {
123+
// 检查是否包含系统内置用户的角色关联
124+
this.checkSystemUserUnassignment(ids);
123125
baseMapper.deleteByIds(ids);
124126
}
125127

@@ -165,4 +167,47 @@ public boolean isRoleIdExists(List<Long> roleIds) {
165167
}
166168
return baseMapper.lambdaQuery().in(UserRoleDO::getRoleId, roleIds).exists();
167169
}
170+
171+
@Override
172+
public void checkSystemUserAssignment(List<Long> userIds) {
173+
if (CollUtil.isEmpty(userIds)) {
174+
return;
175+
}
176+
// 查询用户列表中是否包含系统内置用户
177+
List<UserDO> systemUsers = userService.lambdaQuery()
178+
.select(UserDO::getId, UserDO::getNickname)
179+
.in(UserDO::getId, userIds)
180+
.eq(UserDO::getIsSystem, true)
181+
.list();
182+
CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许分配给非超级管理员角色",
183+
systemUsers.isEmpty() ? "" : systemUsers.get(0).getNickname());
184+
}
185+
186+
@Override
187+
public void checkSystemUserUnassignment(List<Long> userRoleIds) {
188+
if (CollUtil.isEmpty(userRoleIds)) {
189+
return;
190+
}
191+
// 查询用户角色关联列表
192+
List<UserRoleDO> userRoleList = baseMapper.lambdaQuery()
193+
.select(UserRoleDO::getUserId)
194+
.in(UserRoleDO::getId, userRoleIds)
195+
.list();
196+
if (CollUtil.isEmpty(userRoleList)) {
197+
return;
198+
}
199+
// 获取用户ID列表
200+
List<Long> userIds = userRoleList.stream()
201+
.map(UserRoleDO::getUserId)
202+
.distinct()
203+
.toList();
204+
// 查询是否包含系统内置用户
205+
List<UserDO> systemUsers = userService.lambdaQuery()
206+
.select(UserDO::getId, UserDO::getNickname)
207+
.in(UserDO::getId, userIds)
208+
.eq(UserDO::getIsSystem, true)
209+
.list();
210+
CheckUtils.throwIfNotEmpty(systemUsers, "[{}] 是系统内置用户,不允许取消分配角色",
211+
systemUsers.isEmpty() ? "" : systemUsers.get(0).getNickname());
212+
}
168213
}

0 commit comments

Comments
 (0)