From fc23684ffbfded76da2aa2c5de40929ddf3419f0 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 4 Jun 2024 15:34:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1=E4=B8=AD=E5=BF=83?= =?UTF-8?q?=E6=9D=83=E9=99=90=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiTaskCenterController.java | 69 +++++++---------- .../api/service/ApiTaskCenterService.java | 39 +++++++++- .../controller/TaskCenterController.java | 74 ++++++------------- .../system/service/TaskCenterService.java | 39 +++++++++- 4 files changed, 124 insertions(+), 97 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTaskCenterController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTaskCenterController.java index 379a889069..59d8796d60 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTaskCenterController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTaskCenterController.java @@ -2,9 +2,6 @@ package io.metersphere.api.controller; import io.metersphere.api.service.ApiTaskCenterService; import io.metersphere.sdk.constants.PermissionConstants; -import io.metersphere.sdk.constants.TaskCenterResourceType; -import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.util.Translator; import io.metersphere.system.dto.taskcenter.TaskCenterDTO; import io.metersphere.system.dto.taskcenter.request.TaskCenterBatchRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterPageRequest; @@ -18,9 +15,7 @@ import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * @author: LAN @@ -63,70 +58,60 @@ public class ApiTaskCenterController { @PostMapping("/api/system/stop") @Operation(summary = "系统-任务中心-接口用例/场景-停止任务") public void systemStop(@Validated @RequestBody TaskCenterBatchRequest request) { - hasPermission(SYSTEM, request.getModuleType()); + apiTaskCenterService.hasPermission(SYSTEM, request.getModuleType(), + SessionUtils.getCurrentOrganizationId(), + SessionUtils.getCurrentProjectId()); + apiTaskCenterService.systemStop(request, SessionUtils.getUserId()); } @PostMapping("/api/org/stop") @Operation(summary = "组织-任务中心-接口用例/场景-停止任务") public void orgStop(@Validated @RequestBody TaskCenterBatchRequest request) { - hasPermission(ORG, request.getModuleType()); + apiTaskCenterService.hasPermission(ORG, request.getModuleType(), + SessionUtils.getCurrentOrganizationId(), + SessionUtils.getCurrentProjectId()); + apiTaskCenterService.orgStop(request, SessionUtils.getCurrentOrganizationId(), SessionUtils.getUserId()); } @PostMapping("/api/project/stop") @Operation(summary = "项目-任务中心-接口用例/场景-停止任务") public void projectStop(@Validated @RequestBody TaskCenterBatchRequest request) { - hasPermission(PROJECT, request.getModuleType()); + apiTaskCenterService.hasPermission(PROJECT, request.getModuleType(), + SessionUtils.getCurrentOrganizationId(), + SessionUtils.getCurrentProjectId()); apiTaskCenterService.projectStop(request, SessionUtils.getCurrentProjectId(), SessionUtils.getUserId()); } @GetMapping("/api/project/stop/{moduleType}/{id}") @Operation(summary = "项目-任务中心-接口用例/场景-停止任务") public void stopById(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(PROJECT, moduleType); - apiTaskCenterService.stopById(moduleType, id, SessionUtils.getUserId(), OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, "/task/center/api/project/stop"); + apiTaskCenterService.hasPermission(PROJECT, moduleType, + SessionUtils.getCurrentOrganizationId(), + SessionUtils.getCurrentProjectId()); + apiTaskCenterService.stopById(moduleType, id, SessionUtils.getUserId(), + OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, "/task/center/api/project/stop"); } @GetMapping("/api/org/stop/{moduleType}/{id}") @Operation(summary = "组织-任务中心-接口用例/场景-停止任务") public void stopOrgById(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(ORG, moduleType); - apiTaskCenterService.stopById(moduleType, id, SessionUtils.getUserId(), OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, "/task/center/api/org/stop"); + apiTaskCenterService.hasPermission(ORG, moduleType, + SessionUtils.getCurrentOrganizationId(), + SessionUtils.getCurrentProjectId()); + apiTaskCenterService.stopById(moduleType, id, SessionUtils.getUserId(), + OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, "/task/center/api/org/stop"); } @GetMapping("/api/system/stop/{moduleType}/{id}") @Operation(summary = "系统-任务中心-接口用例/场景-停止任务") @RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_READ) - public void stopSystemById(@PathVariable String moduleType,@PathVariable String id) { - hasPermission(SYSTEM, moduleType); - apiTaskCenterService.stopById(moduleType ,id, SessionUtils.getUserId(), OperationLogModule.SETTING_SYSTEM_TASK_CENTER, "/task/center/api/system/stop"); + public void stopSystemById(@PathVariable String moduleType, @PathVariable String id) { + apiTaskCenterService.hasPermission(SYSTEM, moduleType, + SessionUtils.getCurrentOrganizationId(), + SessionUtils.getCurrentProjectId()); + apiTaskCenterService.stopById(moduleType, id, SessionUtils.getUserId(), + OperationLogModule.SETTING_SYSTEM_TASK_CENTER, "/task/center/api/system/stop"); } - - private void hasPermission(String type, String moduleType) { - Map> orgPermission = new HashMap<>(2); - orgPermission.put(TaskCenterResourceType.API_CASE.toString(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)); - orgPermission.put(TaskCenterResourceType.API_SCENARIO.toString(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE)); - Map> projectPermission = new HashMap<>(2); - projectPermission.put(TaskCenterResourceType.API_CASE.toString(), List.of(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)); - projectPermission.put(TaskCenterResourceType.API_SCENARIO.toString(), List.of(PermissionConstants.PROJECT_API_SCENARIO_EXECUTE)); - Map> systemPermission = new HashMap<>(2); - systemPermission.put(TaskCenterResourceType.API_CASE.toString(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)); - systemPermission.put(TaskCenterResourceType.API_SCENARIO.toString(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE)); - - boolean hasPermission = switch (type) { - case ORG -> - orgPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId(), item)); - case PROJECT -> - projectPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId(), item)); - case SYSTEM -> - systemPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId(), item)); - default -> false; - }; - if (!hasPermission) { - throw new MSException(Translator.get("no_permission_to_resource")); - } - } - - } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java index 1b6325b29e..382476a4a2 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTaskCenterService.java @@ -8,10 +8,7 @@ import io.metersphere.api.mapper.ExtApiReportMapper; import io.metersphere.api.mapper.ExtApiScenarioReportMapper; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; -import io.metersphere.sdk.constants.ExecStatus; -import io.metersphere.sdk.constants.HttpMethodConstants; -import io.metersphere.sdk.constants.KafkaTopicConstants; -import io.metersphere.sdk.constants.TaskCenterResourceType; +import io.metersphere.sdk.constants.*; import io.metersphere.sdk.dto.api.result.ProcessResultDTO; import io.metersphere.sdk.dto.api.result.TaskResultDTO; import io.metersphere.sdk.dto.api.task.TaskRequestDTO; @@ -36,6 +33,7 @@ import io.metersphere.system.service.TestResourcePoolService; import io.metersphere.system.service.UserLoginService; import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; +import io.metersphere.system.utils.SessionUtils; import io.metersphere.system.utils.TaskRunnerClient; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; @@ -368,4 +366,37 @@ public class ApiTaskCenterService { request.setModuleType(moduleType); stopApiTask(request, null, userId, path, HttpMethodConstants.GET.name(), module); } + + + public void hasPermission(String type, String moduleType, String orgId, String projectId) { + Map> orgPermission = Map.of( + TaskCenterResourceType.API_CASE.name(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE), + TaskCenterResourceType.API_SCENARIO.name(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE) + ); + + Map> projectPermission = Map.of( + TaskCenterResourceType.API_CASE.name(), List.of(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE), + TaskCenterResourceType.API_SCENARIO.name(), List.of(PermissionConstants.PROJECT_API_SCENARIO_EXECUTE) + ); + + Map> systemPermission = Map.of( + TaskCenterResourceType.API_CASE.name(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE), + TaskCenterResourceType.API_SCENARIO.name(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE) + ); + + boolean hasPermission = switch (type) { + case "org" -> + orgPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(orgId, projectId, item)); + case "project" -> + projectPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(orgId, projectId, item)); + case "system" -> + systemPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(orgId, projectId, item)); + default -> false; + }; + + if (!hasPermission) { + throw new MSException(Translator.get("no_permission_to_resource")); + } + } + } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java index 5c8d3e7fe6..a04f22aa63 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TaskCenterController.java @@ -1,11 +1,7 @@ package io.metersphere.system.controller; import io.metersphere.sdk.constants.PermissionConstants; -import io.metersphere.sdk.constants.TaskCenterResourceType; -import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.util.Translator; import io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO; -import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType; import io.metersphere.system.dto.taskcenter.request.TaskCenterScheduleBatchRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; import io.metersphere.system.log.constants.OperationLogModule; @@ -20,9 +16,7 @@ import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * @author: LAN @@ -65,7 +59,7 @@ public class TaskCenterController { @Operation(summary = "系统-任务中心-删除定时任务") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void delete(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(SYSTEM, moduleType); + taskCenterService.hasPermission(SYSTEM, moduleType, SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.delete(id, moduleType, SessionUtils.getUserId(), "/task/center/system/schedule/delete/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER); } @@ -73,7 +67,8 @@ public class TaskCenterController { @Operation(summary = "组织-任务中心-删除定时任务") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void deleteOrg(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(ORG, moduleType); + taskCenterService.hasPermission(ORG, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.delete(id, moduleType, SessionUtils.getUserId(), "/task/center/org/schedule/delete/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER); } @@ -81,7 +76,8 @@ public class TaskCenterController { @Operation(summary = "项目-任务中心-删除定时任务") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void deleteProject(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(PROJECT, moduleType); + taskCenterService.hasPermission(PROJECT, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.delete(id, moduleType, SessionUtils.getUserId(), "/task/center/project/schedule/delete/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); } @@ -89,7 +85,8 @@ public class TaskCenterController { @Operation(summary = "系统-任务中心-定时任务开启关闭") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void enable(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(SYSTEM, moduleType); + taskCenterService.hasPermission(SYSTEM, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.enable(id, moduleType, SessionUtils.getUserId(), "/task/center/system/schedule/switch/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER); } @@ -98,7 +95,8 @@ public class TaskCenterController { @Operation(summary = "组织-任务中心-定时任务开启关闭") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void enableOrg(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(ORG, moduleType); + taskCenterService.hasPermission(ORG, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.enable(id, moduleType, SessionUtils.getUserId(), "/task/center/org/schedule/switch/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER); } @@ -106,7 +104,8 @@ public class TaskCenterController { @Operation(summary = "项目-任务中心-定时任务开启关闭") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void enableProject(@PathVariable String moduleType, @PathVariable String id) { - hasPermission(PROJECT, moduleType); + taskCenterService.hasPermission(PROJECT, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.enable(id, moduleType, SessionUtils.getUserId(), "/task/center/project/schedule/switch/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); } @@ -114,7 +113,8 @@ public class TaskCenterController { @Operation(summary = "系统-任务中心-修改定时任务") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void update(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) { - hasPermission(SYSTEM, moduleType); + taskCenterService.hasPermission(SYSTEM, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.update(id, moduleType, cron.toString(), SessionUtils.getUserId(), "/task/center/system/schedule/update/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER); } @@ -122,7 +122,8 @@ public class TaskCenterController { @Operation(summary = "组织-任务中心-修改定时任务") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void updateOrg(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) { - hasPermission(ORG, moduleType); + taskCenterService.hasPermission(ORG, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.update(id, moduleType, cron.toString(), SessionUtils.getUserId(), "/task/center/org/schedule/update/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER); } @@ -130,79 +131,52 @@ public class TaskCenterController { @Operation(summary = "项目-任务中心-修改定时任务") @CheckOwner(resourceId = "#id", resourceType = "schedule") public void updateProject(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) { - hasPermission(PROJECT, moduleType); + taskCenterService.hasPermission(PROJECT, moduleType, + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.update(id, moduleType, cron.toString(), SessionUtils.getUserId(), "/task/center/project/schedule/update/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); } @PostMapping("/system/schedule/batch-enable") @Operation(summary = "系统-任务中心-定时任务批量开启") public void batchEnable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { - hasPermission(SYSTEM, request.getScheduleTagType()); + taskCenterService.hasPermission(SYSTEM, request.getScheduleTagType(), SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.batchEnable(request, SessionUtils.getUserId(), "/task/center/system/schedule/batch-enable", OperationLogModule.SETTING_SYSTEM_TASK_CENTER, true, SessionUtils.getCurrentProjectId()); } @PostMapping("/org/schedule/batch-enable") @Operation(summary = "组织-任务中心-定时任务批量开启") public void batchOrgEnable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { - hasPermission(ORG, request.getScheduleTagType()); + taskCenterService.hasPermission(ORG, request.getScheduleTagType(), SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.batchEnableOrg(request, SessionUtils.getUserId(), SessionUtils.getCurrentOrganizationId(), "/task/center/org/schedule/batch-enable", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, true, SessionUtils.getCurrentProjectId()); } @PostMapping("/project/schedule/batch-enable") @Operation(summary = "项目-任务中心-定时任务批量开启") public void batchProjectEnable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { - hasPermission(PROJECT, request.getScheduleTagType()); + taskCenterService.hasPermission(PROJECT, request.getScheduleTagType(), SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.batchEnableProject(request, SessionUtils.getUserId(), SessionUtils.getCurrentProjectId(), "/task/center/project/schedule/batch-enable", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, true); } @PostMapping("/system/schedule/batch-disable") @Operation(summary = "系统-任务中心-定时任务批量关闭") public void batchDisable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { - hasPermission(SYSTEM, request.getScheduleTagType()); + taskCenterService.hasPermission(SYSTEM, request.getScheduleTagType(), SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.batchEnable(request, SessionUtils.getUserId(), "/task/center/system/schedule/batch-disable", OperationLogModule.SETTING_SYSTEM_TASK_CENTER, false, SessionUtils.getCurrentProjectId()); } @PostMapping("/org/schedule/batch-disable") @Operation(summary = "组织-任务中心-定时任务批量关闭") public void batchOrgDisable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { - hasPermission(ORG, request.getScheduleTagType()); + taskCenterService.hasPermission(ORG, request.getScheduleTagType(), SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.batchEnableOrg(request, SessionUtils.getUserId(), SessionUtils.getCurrentOrganizationId(), "/task/center/org/schedule/batch-disable", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, false, SessionUtils.getCurrentProjectId()); } @PostMapping("/project/schedule/batch-disable") @Operation(summary = "项目-任务中心-定时任务批量关闭") public void batchProjectDisable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { - hasPermission(PROJECT, request.getScheduleTagType()); + taskCenterService.hasPermission(PROJECT, request.getScheduleTagType(), + SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId()); taskCenterService.batchEnableProject(request, SessionUtils.getUserId(), SessionUtils.getCurrentProjectId(), "/task/center/project/schedule/batch-disable", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, false); } - private void hasPermission(String type, String moduleType) { - Map> projectPermission = new HashMap<>(2); - projectPermission.put(ScheduleTagType.API_IMPORT.toString(), List.of(PermissionConstants.PROJECT_API_DEFINITION_IMPORT)); - projectPermission.put(TaskCenterResourceType.API_SCENARIO.toString(), List.of(PermissionConstants.PROJECT_API_SCENARIO_EXECUTE)); - projectPermission.put(TaskCenterResourceType.TEST_PLAN.toString(), List.of(PermissionConstants.TEST_PLAN_READ_EXECUTE)); - Map> orgPermission = new HashMap<>(2); - orgPermission.put(ScheduleTagType.API_IMPORT.toString(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_IMPORT)); - orgPermission.put(TaskCenterResourceType.API_SCENARIO.toString(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE)); - orgPermission.put(TaskCenterResourceType.TEST_PLAN.toString(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.TEST_PLAN_READ_EXECUTE)); - Map> systemPermission = new HashMap<>(2); - systemPermission.put(ScheduleTagType.API_IMPORT.toString(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_IMPORT)); - systemPermission.put(TaskCenterResourceType.API_SCENARIO.toString(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE)); - systemPermission.put(TaskCenterResourceType.TEST_PLAN.toString(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.TEST_PLAN_READ_EXECUTE)); - - boolean hasPermission = switch (type) { - case ORG -> - orgPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId(), item)); - case PROJECT -> - projectPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId(), item)); - case SYSTEM -> - systemPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(SessionUtils.getCurrentOrganizationId(), SessionUtils.getCurrentProjectId(), item)); - default -> false; - }; - if (!hasPermission) { - throw new MSException(Translator.get("no_permission_to_resource")); - } - } - - } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java index 7c988bffd0..aa58d28174 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskCenterService.java @@ -5,6 +5,8 @@ import com.github.pagehelper.page.PageMethod; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.constants.HttpMethodConstants; +import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.constants.TaskCenterResourceType; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.SubListUtils; import io.metersphere.sdk.util.Translator; @@ -28,6 +30,7 @@ import io.metersphere.system.schedule.BaseScheduleJob; import io.metersphere.system.schedule.ScheduleService; import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; +import io.metersphere.system.utils.SessionUtils; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -280,7 +283,7 @@ public class TaskCenterService { } private void batchOperation(TaskCenterScheduleBatchRequest request, String userId, String path, String module, List projectList, boolean enable, String projectId) { - List scheduleList = new ArrayList<>(); + List scheduleList; if (request.isSelectAll()) { List projectIds = projectList.stream().map(OptionDTO::getId).toList(); scheduleList = extScheduleMapper.getSchedule(request, projectIds); @@ -316,4 +319,38 @@ public class TaskCenterService { List projectList = getProjectOption(projectId); batchOperation(request, userId, path, module, projectList, enable, projectId); } + + public void hasPermission(String type, String moduleType, String orgId, String projectId) { + Map> orgPermission = Map.of( + ScheduleTagType.API_IMPORT.name(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_IMPORT), + TaskCenterResourceType.API_SCENARIO.name(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE), + TaskCenterResourceType.TEST_PLAN.name(), List.of(PermissionConstants.ORGANIZATION_TASK_CENTER_READ_STOP, PermissionConstants.TEST_PLAN_READ_EXECUTE) + ); + + Map> projectPermission = Map.of( + ScheduleTagType.API_IMPORT.name(), List.of(PermissionConstants.PROJECT_API_DEFINITION_IMPORT), + TaskCenterResourceType.API_SCENARIO.name(), List.of(PermissionConstants.PROJECT_API_SCENARIO_EXECUTE), + TaskCenterResourceType.TEST_PLAN.name(), List.of(PermissionConstants.TEST_PLAN_READ_EXECUTE) + ); + + Map> systemPermission = Map.of( + ScheduleTagType.API_IMPORT.name(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_DEFINITION_IMPORT), + TaskCenterResourceType.API_SCENARIO.name(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.PROJECT_API_SCENARIO_EXECUTE), + TaskCenterResourceType.TEST_PLAN.name(), List.of(PermissionConstants.SYSTEM_TASK_CENTER_READ_STOP, PermissionConstants.TEST_PLAN_READ_EXECUTE) + ); + + boolean hasPermission = switch (type) { + case "org" -> + orgPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(orgId, projectId, item)); + case "project" -> + projectPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(orgId, projectId, item)); + case "system" -> + systemPermission.get(moduleType).stream().anyMatch(item -> SessionUtils.hasPermission(orgId, projectId, item)); + default -> false; + }; + + if (!hasPermission) { + throw new MSException(Translator.get("no_permission_to_resource")); + } + } }