From c28d3cf1c45212e670b90fc0077b5e176a894bd2 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Tue, 12 Nov 2024 21:33:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E5=8F=98=E6=9B=B4=E6=A0=A1=E9=AA=8C=E5=8F=8A?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/system/service/RoleService.java | 8 ++++ .../system/service/impl/RoleServiceImpl.java | 38 +++++++++++++------ .../service/impl/UserRoleServiceImpl.java | 2 + .../system/service/impl/UserServiceImpl.java | 28 ++++++++++---- .../controller/system/RoleController.java | 2 +- .../db/changelog/mysql/main_data.sql | 1 + .../db/changelog/postgresql/main_data.sql | 1 + 7 files changed, 61 insertions(+), 19 deletions(-) diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java b/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java index 7a76f0b0..d369e639 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java @@ -91,4 +91,12 @@ public interface RoleService extends BaseService roleNames); + + /** + * 分配角色给用户 + * + * @param id 角色 ID + * @param userIds 用户 ID 列表 + */ + void assignToUsers(Long id, List userIds); } diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java index ca36027b..b3631728 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java @@ -26,7 +26,6 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import top.continew.admin.auth.service.OnlineUserService; import top.continew.admin.common.constant.CacheConstants; import top.continew.admin.common.constant.ContainerConstants; import top.continew.admin.common.constant.SysConstants; @@ -62,7 +61,6 @@ public class RoleServiceImpl extends BaseServiceImpl userIdList = userRoleService.listUserIdByRoleId(id); - userIdList.parallelStream().forEach(userId -> { - UserContext userContext = UserContextHolder.getContext(userId); - if (null != userContext) { - userContext.setRoles(this.listByUserId(userId)); - userContext.setPermissions(this.listPermissionByUserId(userId)); - UserContextHolder.setContext(userContext); - } - }); + this.updateUserContext(id); } } @@ -198,6 +188,15 @@ public class RoleServiceImpl extends BaseServiceImpllambdaQuery().in(RoleDO::getName, roleNames)); } + @Override + public void assignToUsers(Long id, List userIds) { + super.getById(id); + // 保存用户和角色关联 + userRoleService.assignRoleToUsers(id, userIds); + // 更新用户上下文 + this.updateUserContext(id); + } + /** * 名称是否存在 * @@ -219,4 +218,21 @@ public class RoleServiceImpl extends BaseServiceImpl userIdList = userRoleService.listUserIdByRoleId(roleId); + userIdList.parallelStream().forEach(userId -> { + UserContext userContext = UserContextHolder.getContext(userId); + if (null != userContext) { + userContext.setRoles(this.listByUserId(userId)); + userContext.setPermissions(this.listPermissionByUserId(userId)); + UserContextHolder.setContext(userContext); + } + }); + } } diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserRoleServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserRoleServiceImpl.java index a58468d7..1b9f6a28 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserRoleServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserRoleServiceImpl.java @@ -57,6 +57,8 @@ public class UserRoleServiceImpl implements UserRoleService { if (CollUtil.isEmpty(CollUtil.disjunction(roleIds, oldRoleIdList))) { return false; } + CheckUtils.throwIf(SysConstants.SUPER_USER_ID.equals(userId) && !roleIds + .contains(SysConstants.SUPER_ROLE_ID), "不允许变更超管用户角色"); // 删除原有关联 baseMapper.lambdaUpdate().eq(UserRoleDO::getUserId, userId).remove(); // 保存最新关联 diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java index 8838ed0f..13702bd1 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java @@ -182,12 +182,7 @@ public class UserServiceImpl extends BaseServiceImpl roleIds = updateReq.getRoleIds(); // 保存用户和角色关联 - userRoleService.assignRolesToUser(updateReq.getRoleIds(), id); + userRoleService.assignRolesToUser(roleIds, id); + // 更新用户上下文 + this.updateContext(id); } @Override @@ -685,4 +685,18 @@ public class UserServiceImpl extends BaseServiceImpl userIds) { - userRoleService.assignRoleToUsers(id, userIds); + baseService.assignToUsers(id, userIds); } } diff --git a/continew-webapi/src/main/resources/db/changelog/mysql/main_data.sql b/continew-webapi/src/main/resources/db/changelog/mysql/main_data.sql index dbbb455b..c159c0d5 100644 --- a/continew-webapi/src/main/resources/db/changelog/mysql/main_data.sql +++ b/continew-webapi/src/main/resources/db/changelog/mysql/main_data.sql @@ -16,6 +16,7 @@ VALUES (1016, '导出', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:export', 6, 1, 1, NOW(), NULL, NULL), (1017, '导入', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:import', 7, 1, 1, NOW(), NULL, NULL), (1018, '重置密码', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:resetPwd', 8, 1, 1, NOW(), NULL, NULL), +(1019, '分配角色', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:updateRole', 9, 1, 1, NOW(), NULL, NULL), (1030, '角色管理', 1000, 2, '/system/role', 'SystemRole', 'system/role/index', NULL, 'user-group', b'0', b'0', b'0', NULL, 2, 1, 1, NOW(), NULL, NULL), (1031, '列表', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:list', 1, 1, 1, NOW(), NULL, NULL), diff --git a/continew-webapi/src/main/resources/db/changelog/postgresql/main_data.sql b/continew-webapi/src/main/resources/db/changelog/postgresql/main_data.sql index 419aa448..1f20a723 100644 --- a/continew-webapi/src/main/resources/db/changelog/postgresql/main_data.sql +++ b/continew-webapi/src/main/resources/db/changelog/postgresql/main_data.sql @@ -16,6 +16,7 @@ VALUES (1016, '导出', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:export', 6, 1, 1, NOW(), NULL, NULL), (1017, '导入', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:import', 7, 1, 1, NOW(), NULL, NULL), (1018, '重置密码', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:resetPwd', 8, 1, 1, NOW(), NULL, NULL), +(1019, '分配角色', 1010, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:user:updateRole', 9, 1, 1, NOW(), NULL, NULL), (1030, '角色管理', 1000, 2, '/system/role', 'SystemRole', 'system/role/index', NULL, 'user-group', false, false, false, NULL, 2, 1, 1, NOW(), NULL, NULL), (1031, '列表', 1030, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:role:list', 1, 1, 1, NOW(), NULL, NULL),