From 58d90c3739758ca103dbbcb2ae48dfe618ccadaf Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Wed, 7 Feb 2024 10:41:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E7=8E=AF=E5=A2=83=E7=BB=84=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E9=A1=B9=E7=9B=AE=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EnvironmentGroupController.java | 6 ++--- .../project/mapper/ExtProjectMapper.java | 5 ++++ .../project/mapper/ExtProjectMapper.xml | 24 +++++++++++++++++++ .../service/EnvironmentGroupService.java | 24 +++++-------------- .../EnvironmentGroupControllerTests.java | 14 +++++++---- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentGroupController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentGroupController.java index 9ca6be0fb9..d478d66615 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentGroupController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentGroupController.java @@ -72,11 +72,11 @@ public class EnvironmentGroupController { return environmentGroupService.get(id); } - @GetMapping("/get-project") + @GetMapping("/get-project/{organizationId}") @Operation(summary = "项目管理-环境组-获取项目") @RequiresPermissions(PermissionConstants.PROJECT_ENVIRONMENT_READ) - public List getProject() { - return environmentGroupService.getProject(SessionUtils.getUserId()); + public List getProject(@PathVariable String organizationId) { + return environmentGroupService.getProject(SessionUtils.getUserId(), organizationId); } @PostMapping("/edit/pos") diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.java b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.java index 6d13067aef..d6f446068f 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.java @@ -12,4 +12,9 @@ public interface ExtProjectMapper { String getModuleSetting(@Param("projectId") String projectId); List getProject(@Param("userId") String userId); + + List getAllProjectByOrgId(@Param("organizationId") String organizationId); + + List getProjectByOrgId(@Param("userId") String userId, @Param("organizationId") String organizationId); + } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.xml b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.xml index fd86cee33b..61a4a3eabf 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.xml +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectMapper.xml @@ -28,4 +28,28 @@ and p.enable = 1 and user.deleted = 0 + + \ No newline at end of file diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java index 189345df83..0c4c0cbce9 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java @@ -15,8 +15,6 @@ import io.metersphere.sdk.mapper.EnvironmentGroupRelationMapper; import io.metersphere.sdk.mapper.EnvironmentMapper; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; -import io.metersphere.system.domain.Organization; -import io.metersphere.system.domain.OrganizationExample; import io.metersphere.system.domain.UserRoleRelationExample; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.request.PosRequest; @@ -206,30 +204,20 @@ public class EnvironmentGroupService { return result; } - public List getProject(String userId) { + public List getProject(String userId, String organizationId) { //判断用户是否是系统管理员 List result = new ArrayList<>(); List projects = new ArrayList<>(); UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample(); userRoleRelationExample.createCriteria().andUserIdEqualTo(userId).andRoleIdEqualTo(InternalUserRole.ADMIN.name()); if (userRoleRelationMapper.countByExample(userRoleRelationExample) > 0) { - projects = projectMapper.selectByExample(new ProjectExample()); + projects = extProjectMapper.getAllProjectByOrgId(organizationId); } else { - projects = extProjectMapper.getProject(userId); + projects = extProjectMapper.getProjectByOrgId(userId, organizationId); + } + if (CollectionUtils.isNotEmpty(projects)) { + result = projects.stream().map(e -> new OptionDTO(e.getId(), e.getName())).collect(Collectors.toList()); } - //提取所有的组织id - List organizationIds = projects.stream().map(Project::getOrganizationId).distinct().toList(); - //查询组织名称 - OrganizationExample organizationExample = new OrganizationExample(); - organizationExample.createCriteria().andIdIn(organizationIds); - List organizations = organizationMapper.selectByExample(organizationExample); - Map organizationMap = organizations.stream().collect(Collectors.toMap(Organization::getId, Organization::getName)); - projects.forEach(e -> { - OptionDTO dto = new OptionDTO(); - dto.setId(e.getId()); - dto.setName(e.getName() + " (" + organizationMap.get(e.getOrganizationId()) + ")"); - result.add(dto); - }); return result; } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java index 203fb1045d..0055926fba 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java @@ -1,9 +1,10 @@ package io.metersphere.project.controller; -import io.metersphere.project.dto.environment.*; import io.metersphere.project.api.KeyValueEnableParam; +import io.metersphere.project.dto.environment.*; import io.metersphere.project.dto.environment.http.HttpConfig; +import io.metersphere.project.service.EnvironmentGroupService; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.domain.Environment; @@ -49,7 +50,7 @@ public class EnvironmentGroupControllerTests extends BaseTest { private static final String update = prefix + "/update"; private static final String delete = prefix + "/delete/"; private static final String list = prefix + "/list"; - private static final String getProject = prefix + "/get-project"; + private static final String getProject = prefix + "/get-project/"; private static final String POS_URL = prefix + "/edit/pos"; private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest(); private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); @@ -61,6 +62,8 @@ public class EnvironmentGroupControllerTests extends BaseTest { @Resource private EnvironmentGroupMapper environmentGroupMapper; private EnvironmentGroup environmentGroup; + @Resource + private EnvironmentGroupService environmentGroupService; public static T parseObjectFromMvcResult(MvcResult mvcResult, Class parseClass) { try { @@ -154,6 +157,8 @@ public class EnvironmentGroupControllerTests extends BaseTest { Assertions.assertEquals(groupResponse.getId(), environmentGroup.getId()); Assertions.assertEquals(groupResponse.getName(), environmentGroup.getName()); checkLog(environmentGroup.getId(), OperationLogType.ADD); + environmentGroupService.getEnvironmentGroupRelations(List.of(environmentGroup.getId())); + environmentGroupService.getEnvironmentGroupRelations(new ArrayList<>()); //校验权限 groupRequest.setName("校验权限"); requestPostPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ_ADD, add, groupRequest); @@ -335,11 +340,12 @@ public class EnvironmentGroupControllerTests extends BaseTest { @Test @Order(13) public void testGetProject() throws Exception { - MvcResult mvcResult = this.responseGet(getProject); + MvcResult mvcResult = this.responseGet(getProject + DEFAULT_ORGANIZATION_ID); List response = parseObjectFromMvcResult(mvcResult, List.class); Assertions.assertNotNull(response); + //校验权限 - requestGetPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ, getProject); + requestGetPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ, getProject + "/" + DEFAULT_ORGANIZATION_ID); }