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); }