From 65d60bc18234006a1fa7f2678ddcc2b9820238bf Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Thu, 9 Dec 2021 18:24:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E6=8E=A5=E5=8F=A3=E3=80=81=E4=BF=AE=E6=94=B9=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 8 +- .../metersphere/service/UserGroupService.java | 14 ++-- .../io/metersphere/service/UserService.java | 79 +++++++++---------- 3 files changed, 46 insertions(+), 55 deletions(-) diff --git a/backend/src/main/java/io/metersphere/controller/UserController.java b/backend/src/main/java/io/metersphere/controller/UserController.java index 196f103879..a8f9ebb971 100644 --- a/backend/src/main/java/io/metersphere/controller/UserController.java +++ b/backend/src/main/java/io/metersphere/controller/UserController.java @@ -110,15 +110,9 @@ public class UserController { userService.updateCurrentUserByResourceId(resourceId); } - @PostMapping("/switch/source/org/{sourceId}") - public UserDTO switchOrganization(@PathVariable(value = "sourceId") String sourceId) { - userService.switchUserRole("organization", sourceId); - return SessionUtils.getUser(); - } - @PostMapping("/switch/source/ws/{sourceId}") public UserDTO switchWorkspace(@PathVariable(value = "sourceId") String sourceId) { - userService.switchUserRole("workspace", sourceId); + userService.switchUserResource("workspace", sourceId); return SessionUtils.getUser(); } diff --git a/backend/src/main/java/io/metersphere/service/UserGroupService.java b/backend/src/main/java/io/metersphere/service/UserGroupService.java index 7399ee53b0..dbd6b2b208 100644 --- a/backend/src/main/java/io/metersphere/service/UserGroupService.java +++ b/backend/src/main/java/io/metersphere/service/UserGroupService.java @@ -24,18 +24,18 @@ public class UserGroupService { userRoleExample.createCriteria().andUserIdEqualTo(userId); List userGroups = userGroupMapper.selectByExample(userRoleExample); List collect = userGroups.stream() - .map(userRole -> userRole.getGroupId()) + .map(UserGroup::getGroupId) .distinct() .collect(Collectors.toList()); - for (int i = 0; i < collect.size(); i++) { + for (String id : collect) { Map map = new HashMap<>(2); - map.put("id", collect.get(i)); + map.put("id", id); map.put("ids", new ArrayList<>()); - for (int j = 0; j < userGroups.size(); j++) { - String role = userGroups.get(j).getGroupId(); - if (StringUtils.equals(role, collect.get(i))) { + for (UserGroup userGroup : userGroups) { + String groupId = userGroup.getGroupId(); + if (StringUtils.equals(groupId, id)) { List ids = (List) map.get("ids"); - ids.add(userGroups.get(j).getSourceId()); + ids.add(userGroup.getSourceId()); } } list.add(map); diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index 7473fbe825..c02fe22255 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -107,22 +107,20 @@ public class UserService { return extUserMapper.queryName(); } - public UserDTO insert(UserRequest user) { - checkUserParam(user); - // - String id = user.getId(); - User user1 = userMapper.selectByPrimaryKey(id); - if (user1 != null) { + public UserDTO insert(UserRequest userRequest) { + checkUserParam(userRequest); + String id = userRequest.getId(); + User user = userMapper.selectByPrimaryKey(id); + if (user != null) { MSException.throwException(Translator.get("user_id_already_exists")); } else { - createUser(user); + createUser(userRequest); } - - List> groups = user.getGroups(); + List> groups = userRequest.getGroups(); if (!groups.isEmpty()) { - insertUserGroup(groups, user.getId()); + insertUserGroup(groups, userRequest.getId()); } - return getUserDTO(user.getId()); + return getUserDTO(userRequest.getId()); } public void insertUserGroup(List> groups, String userId) { @@ -385,14 +383,13 @@ public class UserService { } } - public void switchUserRole(String sign, String sourceId) { + public void switchUserResource(String sign, String sourceId) { SessionUser sessionUser = SessionUtils.getUser(); // 获取最新UserDTO UserDTO user = getUserDTO(sessionUser.getId()); User newUser = new User(); if (StringUtils.equals("workspace", sign)) { - Workspace workspace = workspaceMapper.selectByPrimaryKey(sourceId); user.setLastWorkspaceId(sourceId); List projects = getProjectListByWsAndUserId(sourceId); if (projects.size() > 0) { @@ -417,15 +414,13 @@ public class UserService { userGroupExample.createCriteria().andUserIdEqualTo(useId); List userGroups = userGroupMapper.selectByExample(userGroupExample); List projectList = new ArrayList<>(); - userGroups.forEach(userGroup -> { - projects.forEach(project -> { - if (StringUtils.equals(userGroup.getSourceId(), project.getId())) { - if (!projectList.contains(project)) { - projectList.add(project); - } + userGroups.forEach(userGroup -> projects.forEach(project -> { + if (StringUtils.equals(userGroup.getSourceId(), project.getId())) { + if (!projectList.contains(project)) { + projectList.add(project); } - }); - }); + } + })); return projectList; } @@ -632,7 +627,7 @@ public class UserService { .collect(Collectors.toList()); if (workspaces.size() > 0) { String wsId = workspaces.get(0).getSourceId(); - switchUserRole("workspace", wsId); + switchUserResource("workspace", wsId); } } else { UserGroup userGroup = project.stream().filter(p -> StringUtils.isNotBlank(p.getSourceId())) @@ -952,7 +947,6 @@ public class UserService { if (CollectionUtils.isEmpty(userGroups)) { return userGroupPermissionDTO; } - // 设置 user_role userGroupPermissionDTO.setUserGroups(userGroups); List groupId = userGroups.stream().map(UserGroup::getGroupId).collect(Collectors.toList()); @@ -1088,24 +1082,27 @@ public class UserService { } public void addProjectMember(AddMemberRequest request) { - if (!CollectionUtils.isEmpty(request.getUserIds())) { - for (String userId : request.getUserIds()) { - UserGroupExample userGroupExample = new UserGroupExample(); - userGroupExample.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(request.getProjectId()); - List userGroups = userGroupMapper.selectByExample(userGroupExample); - if (userGroups.size() > 0) { - MSException.throwException(Translator.get("user_already_exists")); - } else { - for (String groupId : request.getGroupIds()) { - UserGroup userGroup = new UserGroup(); - userGroup.setGroupId(groupId); - userGroup.setSourceId(request.getProjectId()); - userGroup.setUserId(userId); - userGroup.setId(UUID.randomUUID().toString()); - userGroup.setUpdateTime(System.currentTimeMillis()); - userGroup.setCreateTime(System.currentTimeMillis()); - userGroupMapper.insertSelective(userGroup); - } + if (CollectionUtils.isEmpty(request.getUserIds())) { + LogUtil.info("add project member warning, request param user id list empty!"); + return; + } + + for (String userId : request.getUserIds()) { + UserGroupExample userGroupExample = new UserGroupExample(); + userGroupExample.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(request.getProjectId()); + List userGroups = userGroupMapper.selectByExample(userGroupExample); + if (userGroups.size() > 0) { + MSException.throwException(Translator.get("user_already_exists")); + } else { + for (String groupId : request.getGroupIds()) { + UserGroup userGroup = new UserGroup(); + userGroup.setGroupId(groupId); + userGroup.setSourceId(request.getProjectId()); + userGroup.setUserId(userId); + userGroup.setId(UUID.randomUUID().toString()); + userGroup.setUpdateTime(System.currentTimeMillis()); + userGroup.setCreateTime(System.currentTimeMillis()); + userGroupMapper.insertSelective(userGroup); } } }