diff --git a/backend/src/main/java/io/metersphere/controller/LoginController.java b/backend/src/main/java/io/metersphere/controller/LoginController.java index 62b3e8656e..7846954ed9 100644 --- a/backend/src/main/java/io/metersphere/controller/LoginController.java +++ b/backend/src/main/java/io/metersphere/controller/LoginController.java @@ -54,10 +54,10 @@ public class LoginController { List org = userRoles.stream().filter(ur -> ur.getRoleId().startsWith("org")).collect(Collectors.toList()); if (test.size() > 0) { String wsId = test.get(0).getSourceId(); - userService.switchUserRole(user, "workspace", wsId); + userService.switchUserRole("workspace", wsId); } else if (org.size() > 0) { String orgId = org.get(0).getSourceId(); - userService.switchUserRole(user, "organization", orgId); + userService.switchUserRole("organization", orgId); } } // 返回 userDTO diff --git a/backend/src/main/java/io/metersphere/controller/UserController.java b/backend/src/main/java/io/metersphere/controller/UserController.java index 8fc38ecb06..5338a0ef39 100644 --- a/backend/src/main/java/io/metersphere/controller/UserController.java +++ b/backend/src/main/java/io/metersphere/controller/UserController.java @@ -129,16 +129,14 @@ public class UserController { @PostMapping("/switch/source/org/{sourceId}") @RequiresRoles(RoleConstants.ORG_ADMIN) public UserDTO switchOrganization(@PathVariable(value = "sourceId") String sourceId) { - UserDTO user = SessionUtils.getUser(); - userService.switchUserRole(user,"organization",sourceId); + userService.switchUserRole("organization",sourceId); return SessionUtils.getUser(); } @PostMapping("/switch/source/ws/{sourceId}") @RequiresRoles(value = {RoleConstants.TEST_MANAGER,RoleConstants.TEST_VIEWER,RoleConstants.TEST_USER}, logical = Logical.OR) public UserDTO switchWorkspace(@PathVariable(value = "sourceId") String sourceId) { - UserDTO user = SessionUtils.getUser(); - userService.switchUserRole(user, "workspace", sourceId); + userService.switchUserRole("workspace", sourceId); return SessionUtils.getUser(); } diff --git a/backend/src/main/java/io/metersphere/controller/WorkspaceController.java b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java index 45f02ff1ae..580013ed18 100644 --- a/backend/src/main/java/io/metersphere/controller/WorkspaceController.java +++ b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java @@ -27,6 +27,7 @@ public class WorkspaceController { @PostMapping("add") @RequiresRoles(RoleConstants.ORG_ADMIN) public Workspace addWorkspace(@RequestBody Workspace workspace) { + workspaceService.checkWorkspaceOwnerByOrgAdmin(workspace.getId()); return workspaceService.saveWorkspace(workspace); } diff --git a/backend/src/main/java/io/metersphere/service/OrganizationService.java b/backend/src/main/java/io/metersphere/service/OrganizationService.java index 1be525c6ce..bc7c7b1e30 100644 --- a/backend/src/main/java/io/metersphere/service/OrganizationService.java +++ b/backend/src/main/java/io/metersphere/service/OrganizationService.java @@ -4,12 +4,14 @@ import io.metersphere.base.domain.*; import io.metersphere.base.mapper.OrganizationMapper; import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.UserRoleMapper; +import io.metersphere.base.mapper.WorkspaceMapper; import io.metersphere.base.mapper.ext.ExtOrganizationMapper; import io.metersphere.base.mapper.ext.ExtUserRoleMapper; import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.exception.MSException; import io.metersphere.controller.request.OrganizationRequest; import io.metersphere.dto.OrganizationMemberDTO; +import io.metersphere.dto.UserDTO; import io.metersphere.dto.UserRoleHelpDTO; import io.metersphere.i18n.Translator; import io.metersphere.user.SessionUser; @@ -39,6 +41,12 @@ public class OrganizationService { private UserMapper userMapper; @Resource private ExtOrganizationMapper extOrganizationMapper; + @Resource + private WorkspaceMapper workspaceMapper; + @Resource + private WorkspaceService workspaceService; + @Resource + private UserService userService; public Organization addOrganization(Organization organization) { long currentTimeMillis = System.currentTimeMillis(); @@ -123,7 +131,8 @@ public class OrganizationService { } public void checkOrgOwner(String organizationId) { - SessionUser user = SessionUtils.getUser(); + SessionUser sessionUser = SessionUtils.getUser(); + UserDTO user = userService.getUserDTO(sessionUser.getId()); List collect = user.getUserRoles().stream() .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) .map(UserRole::getSourceId) diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index b14ef7aa24..acb0dcc975 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -128,7 +128,11 @@ public class UserService { userMapper.updateByPrimaryKeySelective(user); } - public void switchUserRole(UserDTO user, String sign, String sourceId) { + public void switchUserRole(String sign, String sourceId) { + SessionUser sessionUser = SessionUtils.getUser(); + // 获取最新UserDTO + UserDTO user = getUserDTO(sessionUser.getId()); + User newUser = new User(); if (StringUtils.equals("organization", sign)) { user.setLastOrganizationId(sourceId); diff --git a/backend/src/main/java/io/metersphere/service/WorkspaceService.java b/backend/src/main/java/io/metersphere/service/WorkspaceService.java index 79b1c1c894..9b4d79ac0a 100644 --- a/backend/src/main/java/io/metersphere/service/WorkspaceService.java +++ b/backend/src/main/java/io/metersphere/service/WorkspaceService.java @@ -1,6 +1,7 @@ package io.metersphere.service; import io.metersphere.base.domain.*; +import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.UserRoleMapper; import io.metersphere.base.mapper.WorkspaceMapper; @@ -10,6 +11,7 @@ import io.metersphere.base.mapper.ext.ExtWorkspaceMapper; import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.exception.MSException; import io.metersphere.controller.request.WorkspaceRequest; +import io.metersphere.dto.UserDTO; import io.metersphere.dto.UserRoleHelpDTO; import io.metersphere.dto.WorkspaceDTO; import io.metersphere.dto.WorkspaceMemberDTO; @@ -41,6 +43,12 @@ public class WorkspaceService { private UserMapper userMapper; @Resource private ExtOrganizationMapper extOrganizationMapper; + @Resource + private ProjectService projectService; + @Resource + private ProjectMapper projectMapper; + @Resource + private UserService userService; public Workspace saveWorkspace(Workspace workspace) { if (StringUtils.isBlank(workspace.getName())) { @@ -98,7 +106,8 @@ public class WorkspaceService { public void checkWorkspaceOwnerByOrgAdmin(String workspaceId) { checkWorkspaceIsExist(workspaceId); WorkspaceExample example = new WorkspaceExample(); - SessionUser user = SessionUtils.getUser(); + SessionUser sessionUser = SessionUtils.getUser(); + UserDTO user = userService.getUserDTO(sessionUser.getId()); List orgIds = user.getUserRoles().stream() .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) .map(UserRole::getSourceId) @@ -114,7 +123,8 @@ public class WorkspaceService { public void checkWorkspaceOwner(String workspaceId) { checkWorkspaceIsExist(workspaceId); WorkspaceExample example = new WorkspaceExample(); - SessionUser user = SessionUtils.getUser(); + SessionUser sessionUser = SessionUtils.getUser(); + UserDTO user = userService.getUserDTO(sessionUser.getId()); List orgIds = user.getUserRoles().stream() .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) .map(UserRole::getSourceId)