From 5b90bb4bd589b10d0675df374ce26b2c2a9a936e Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Tue, 18 Oct 2022 10:39:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90=E4=BB=85=E6=98=AF=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E6=88=90=E5=91=98=E9=A6=96=E6=AC=A1=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=98=BE=E7=A4=BA=E9=94=99=E4=B9=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1018140 --user=李玉号 【系统设置】用户权限仅是项目成员首次登录页面显示错乱 https://www.tapd.cn/55049933/s/1265526 --- .../gateway/service/UserLoginService.java | 138 ++++++++++++++++++ .../frontend/src/business/SettingHome.vue | 2 +- 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java b/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java index 9aa42d634e..f8f322cd8a 100644 --- a/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java +++ b/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java @@ -2,6 +2,7 @@ package io.metersphere.gateway.service; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.*; +import io.metersphere.commons.constants.UserGroupType; import io.metersphere.commons.constants.UserSource; import io.metersphere.commons.constants.UserStatus; import io.metersphere.commons.exception.MSException; @@ -50,6 +51,7 @@ public class UserLoginService { userDTO = loginLocalMode(request.getUsername(), request.getPassword()); break; } + autoSwitch(userDTO); return Optional.of(SessionUser.fromUser(userDTO, session.getId())); } @@ -78,6 +80,142 @@ public class UserLoginService { return user; } + public void autoSwitch(UserDTO user) { + // 用户有 last_project_id 权限 + if (hasLastProjectPermission(user)) { + return; + } + // 用户有 last_workspace_id 权限 + if (hasLastWorkspacePermission(user)) { + return; + } + // 判断其他权限 + checkNewWorkspaceAndProject(user); + } + + private boolean hasLastProjectPermission(UserDTO user) { + if (StringUtils.isNotBlank(user.getLastProjectId())) { + List projectUserGroups = user.getUserGroups().stream() + .filter(ug -> StringUtils.equals(user.getLastProjectId(), ug.getSourceId())) + .collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(projectUserGroups)) { + Project project = projectMapper.selectByPrimaryKey(user.getLastProjectId()); + if (StringUtils.equals(project.getWorkspaceId(), user.getLastWorkspaceId())) { + return true; + } + // last_project_id 和 last_workspace_id 对应不上了 + user.setLastWorkspaceId(project.getWorkspaceId()); + updateUser(user); + return true; + } + } + return false; + } + + private boolean hasLastWorkspacePermission(UserDTO user) { + if (StringUtils.isNotBlank(user.getLastWorkspaceId())) { + List workspaceUserGroups = user.getUserGroups().stream() + .filter(ug -> StringUtils.equals(user.getLastWorkspaceId(), ug.getSourceId())) + .collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(workspaceUserGroups)) { + ProjectExample example = new ProjectExample(); + example.createCriteria().andWorkspaceIdEqualTo(user.getLastWorkspaceId()); + List projects = projectMapper.selectByExample(example); + // 工作空间下没有项目 + if (CollectionUtils.isEmpty(projects)) { + return true; + } + // 工作空间下有项目,选中有权限的项目 + List projectIds = projects.stream() + .map(Project::getId) + .collect(Collectors.toList()); + + List userGroups = user.getUserGroups(); + List projectGroupIds = user.getGroups() + .stream().filter(ug -> StringUtils.equals(ug.getType(), UserGroupType.PROJECT)) + .map(Group::getId) + .collect(Collectors.toList()); + List projectIdsWithPermission = userGroups.stream().filter(ug -> projectGroupIds.contains(ug.getGroupId())) + .filter(p -> StringUtils.isNotBlank(p.getSourceId())) + .map(UserGroup::getSourceId) + .filter(projectIds::contains) + .collect(Collectors.toList()); + + List intersection = projectIds.stream().filter(projectIdsWithPermission::contains).collect(Collectors.toList()); + // 当前工作空间下的所有项目都没有权限 + if (CollectionUtils.isEmpty(intersection)) { + return true; + } + Project project = projects.stream().filter(p -> StringUtils.equals(intersection.get(0), p.getId())).findFirst().get(); + String wsId = project.getWorkspaceId(); + user.setId(user.getId()); + user.setLastProjectId(project.getId()); + user.setLastWorkspaceId(wsId); + updateUser(user); + return true; + } + } + return false; + } + + private void checkNewWorkspaceAndProject(UserDTO user) { + List userGroups = user.getUserGroups(); + List projectGroupIds = user.getGroups() + .stream().filter(ug -> StringUtils.equals(ug.getType(), UserGroupType.PROJECT)) + .map(Group::getId) + .collect(Collectors.toList()); + List project = userGroups.stream().filter(ug -> projectGroupIds.contains(ug.getGroupId())) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(project)) { + List workspaceIds = user.getGroups() + .stream() + .filter(ug -> StringUtils.equals(ug.getType(), UserGroupType.WORKSPACE)) + .map(Group::getId) + .collect(Collectors.toList()); + List workspaces = userGroups.stream().filter(ug -> workspaceIds.contains(ug.getGroupId())) + .collect(Collectors.toList()); + if (workspaces.size() > 0) { + String wsId = workspaces.get(0).getSourceId(); + switchUserResource("workspace", wsId, user); + } else { + // 用户登录之后没有项目和工作空间的权限就把值清空 + user.setLastWorkspaceId(""); + user.setLastProjectId(""); + updateUser(user); + } + } else { + UserGroup userGroup = project.stream().filter(p -> StringUtils.isNotBlank(p.getSourceId())) + .collect(Collectors.toList()).get(0); + String projectId = userGroup.getSourceId(); + Project p = projectMapper.selectByPrimaryKey(projectId); + String wsId = p.getWorkspaceId(); + user.setId(user.getId()); + user.setLastProjectId(projectId); + user.setLastWorkspaceId(wsId); + updateUser(user); + } + } + + public void switchUserResource(String sign, String sourceId, UserDTO sessionUser) { + // 获取最新UserDTO + UserDTO user = getUserDTO(sessionUser.getId()); + User newUser = new User(); + + if (StringUtils.equals("workspace", sign)) { + user.setLastWorkspaceId(sourceId); + sessionUser.setLastWorkspaceId(sourceId); + List projects = getProjectListByWsAndUserId(sessionUser.getId(), sourceId); + if (projects.size() > 0) { + user.setLastProjectId(projects.get(0).getId()); + } else { + user.setLastProjectId(""); + } + } + BeanUtils.copyProperties(user, newUser); + // 切换工作空间或组织之后更新 session 里的 user + SessionUtils.putUser(SessionUser.fromUser(user, SessionUtils.getSessionId())); + userMapper.updateByPrimaryKeySelective(newUser); + } public UserDTO getLoginUser(String userId, List list) { UserExample example = new UserExample(); example.createCriteria().andIdEqualTo(userId).andSourceIn(list); diff --git a/system-setting/frontend/src/business/SettingHome.vue b/system-setting/frontend/src/business/SettingHome.vue index 3651491ca3..68287d5812 100644 --- a/system-setting/frontend/src/business/SettingHome.vue +++ b/system-setting/frontend/src/business/SettingHome.vue @@ -70,7 +70,7 @@ export default { } // 只有工作空间权限时跳转到项目列表 if (wsPermissions && !sysPermissions) { - this.$router.push('/setting/project/:type'); + window.location.href = '/#/setting/project/:type'; } }, mounted() {