refactor(系统设置): 优化任务中心权限验证

This commit is contained in:
fit2-zhao 2024-06-04 15:34:47 +08:00 committed by Craftsman
parent 2019042649
commit fc23684ffb
4 changed files with 124 additions and 97 deletions

View File

@ -2,9 +2,6 @@ package io.metersphere.api.controller;
import io.metersphere.api.service.ApiTaskCenterService; import io.metersphere.api.service.ApiTaskCenterService;
import io.metersphere.sdk.constants.PermissionConstants; 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.TaskCenterDTO;
import io.metersphere.system.dto.taskcenter.request.TaskCenterBatchRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterBatchRequest;
import io.metersphere.system.dto.taskcenter.request.TaskCenterPageRequest; 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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author: LAN * @author: LAN
@ -63,70 +58,60 @@ public class ApiTaskCenterController {
@PostMapping("/api/system/stop") @PostMapping("/api/system/stop")
@Operation(summary = "系统-任务中心-接口用例/场景-停止任务") @Operation(summary = "系统-任务中心-接口用例/场景-停止任务")
public void systemStop(@Validated @RequestBody TaskCenterBatchRequest request) { 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()); apiTaskCenterService.systemStop(request, SessionUtils.getUserId());
} }
@PostMapping("/api/org/stop") @PostMapping("/api/org/stop")
@Operation(summary = "组织-任务中心-接口用例/场景-停止任务") @Operation(summary = "组织-任务中心-接口用例/场景-停止任务")
public void orgStop(@Validated @RequestBody TaskCenterBatchRequest request) { 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()); apiTaskCenterService.orgStop(request, SessionUtils.getCurrentOrganizationId(), SessionUtils.getUserId());
} }
@PostMapping("/api/project/stop") @PostMapping("/api/project/stop")
@Operation(summary = "项目-任务中心-接口用例/场景-停止任务") @Operation(summary = "项目-任务中心-接口用例/场景-停止任务")
public void projectStop(@Validated @RequestBody TaskCenterBatchRequest request) { 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()); apiTaskCenterService.projectStop(request, SessionUtils.getCurrentProjectId(), SessionUtils.getUserId());
} }
@GetMapping("/api/project/stop/{moduleType}/{id}") @GetMapping("/api/project/stop/{moduleType}/{id}")
@Operation(summary = "项目-任务中心-接口用例/场景-停止任务") @Operation(summary = "项目-任务中心-接口用例/场景-停止任务")
public void stopById(@PathVariable String moduleType, @PathVariable String id) { public void stopById(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(PROJECT, moduleType); apiTaskCenterService.hasPermission(PROJECT, moduleType,
apiTaskCenterService.stopById(moduleType, id, SessionUtils.getUserId(), OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, "/task/center/api/project/stop"); 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}") @GetMapping("/api/org/stop/{moduleType}/{id}")
@Operation(summary = "组织-任务中心-接口用例/场景-停止任务") @Operation(summary = "组织-任务中心-接口用例/场景-停止任务")
public void stopOrgById(@PathVariable String moduleType, @PathVariable String id) { public void stopOrgById(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(ORG, moduleType); apiTaskCenterService.hasPermission(ORG, moduleType,
apiTaskCenterService.stopById(moduleType, id, SessionUtils.getUserId(), OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, "/task/center/api/org/stop"); 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}") @GetMapping("/api/system/stop/{moduleType}/{id}")
@Operation(summary = "系统-任务中心-接口用例/场景-停止任务") @Operation(summary = "系统-任务中心-接口用例/场景-停止任务")
@RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_READ) @RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_READ)
public void stopSystemById(@PathVariable String moduleType,@PathVariable String id) { public void stopSystemById(@PathVariable String moduleType, @PathVariable String id) {
hasPermission(SYSTEM, moduleType); apiTaskCenterService.hasPermission(SYSTEM, moduleType,
apiTaskCenterService.stopById(moduleType ,id, SessionUtils.getUserId(), OperationLogModule.SETTING_SYSTEM_TASK_CENTER, "/task/center/api/system/stop"); 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<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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"));
}
}
} }

View File

@ -8,10 +8,7 @@ import io.metersphere.api.mapper.ExtApiReportMapper;
import io.metersphere.api.mapper.ExtApiScenarioReportMapper; import io.metersphere.api.mapper.ExtApiScenarioReportMapper;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.ExecStatus; import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.KafkaTopicConstants;
import io.metersphere.sdk.constants.TaskCenterResourceType;
import io.metersphere.sdk.dto.api.result.ProcessResultDTO; import io.metersphere.sdk.dto.api.result.ProcessResultDTO;
import io.metersphere.sdk.dto.api.result.TaskResultDTO; import io.metersphere.sdk.dto.api.result.TaskResultDTO;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO; 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.service.UserLoginService;
import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import io.metersphere.system.utils.SessionUtils;
import io.metersphere.system.utils.TaskRunnerClient; import io.metersphere.system.utils.TaskRunnerClient;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -368,4 +366,37 @@ public class ApiTaskCenterService {
request.setModuleType(moduleType); request.setModuleType(moduleType);
stopApiTask(request, null, userId, path, HttpMethodConstants.GET.name(), module); stopApiTask(request, null, userId, path, HttpMethodConstants.GET.name(), module);
} }
public void hasPermission(String type, String moduleType, String orgId, String projectId) {
Map<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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"));
}
}
} }

View File

@ -1,11 +1,7 @@
package io.metersphere.system.controller; package io.metersphere.system.controller;
import io.metersphere.sdk.constants.PermissionConstants; 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.TaskCenterScheduleDTO;
import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType;
import io.metersphere.system.dto.taskcenter.request.TaskCenterScheduleBatchRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterScheduleBatchRequest;
import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest;
import io.metersphere.system.log.constants.OperationLogModule; 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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author: LAN * @author: LAN
@ -65,7 +59,7 @@ public class TaskCenterController {
@Operation(summary = "系统-任务中心-删除定时任务") @Operation(summary = "系统-任务中心-删除定时任务")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void delete(@PathVariable String moduleType, @PathVariable String id) { 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); 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 = "组织-任务中心-删除定时任务") @Operation(summary = "组织-任务中心-删除定时任务")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void deleteOrg(@PathVariable String moduleType, @PathVariable String id) { 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); 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 = "项目-任务中心-删除定时任务") @Operation(summary = "项目-任务中心-删除定时任务")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void deleteProject(@PathVariable String moduleType, @PathVariable String id) { 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); 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 = "系统-任务中心-定时任务开启关闭") @Operation(summary = "系统-任务中心-定时任务开启关闭")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void enable(@PathVariable String moduleType, @PathVariable String id) { 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); 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 = "组织-任务中心-定时任务开启关闭") @Operation(summary = "组织-任务中心-定时任务开启关闭")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void enableOrg(@PathVariable String moduleType, @PathVariable String id) { 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); 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 = "项目-任务中心-定时任务开启关闭") @Operation(summary = "项目-任务中心-定时任务开启关闭")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void enableProject(@PathVariable String moduleType, @PathVariable String id) { 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); 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 = "系统-任务中心-修改定时任务") @Operation(summary = "系统-任务中心-修改定时任务")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void update(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) { 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); 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 = "组织-任务中心-修改定时任务") @Operation(summary = "组织-任务中心-修改定时任务")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void updateOrg(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) { 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); 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 = "项目-任务中心-修改定时任务") @Operation(summary = "项目-任务中心-修改定时任务")
@CheckOwner(resourceId = "#id", resourceType = "schedule") @CheckOwner(resourceId = "#id", resourceType = "schedule")
public void updateProject(@PathVariable String moduleType, @PathVariable String id, @RequestBody Object cron) { 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); taskCenterService.update(id, moduleType, cron.toString(), SessionUtils.getUserId(), "/task/center/project/schedule/update/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
} }
@PostMapping("/system/schedule/batch-enable") @PostMapping("/system/schedule/batch-enable")
@Operation(summary = "系统-任务中心-定时任务批量开启") @Operation(summary = "系统-任务中心-定时任务批量开启")
public void batchEnable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { 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()); taskCenterService.batchEnable(request, SessionUtils.getUserId(), "/task/center/system/schedule/batch-enable", OperationLogModule.SETTING_SYSTEM_TASK_CENTER, true, SessionUtils.getCurrentProjectId());
} }
@PostMapping("/org/schedule/batch-enable") @PostMapping("/org/schedule/batch-enable")
@Operation(summary = "组织-任务中心-定时任务批量开启") @Operation(summary = "组织-任务中心-定时任务批量开启")
public void batchOrgEnable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { 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()); 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") @PostMapping("/project/schedule/batch-enable")
@Operation(summary = "项目-任务中心-定时任务批量开启") @Operation(summary = "项目-任务中心-定时任务批量开启")
public void batchProjectEnable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { 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); taskCenterService.batchEnableProject(request, SessionUtils.getUserId(), SessionUtils.getCurrentProjectId(), "/task/center/project/schedule/batch-enable", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, true);
} }
@PostMapping("/system/schedule/batch-disable") @PostMapping("/system/schedule/batch-disable")
@Operation(summary = "系统-任务中心-定时任务批量关闭") @Operation(summary = "系统-任务中心-定时任务批量关闭")
public void batchDisable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { 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()); taskCenterService.batchEnable(request, SessionUtils.getUserId(), "/task/center/system/schedule/batch-disable", OperationLogModule.SETTING_SYSTEM_TASK_CENTER, false, SessionUtils.getCurrentProjectId());
} }
@PostMapping("/org/schedule/batch-disable") @PostMapping("/org/schedule/batch-disable")
@Operation(summary = "组织-任务中心-定时任务批量关闭") @Operation(summary = "组织-任务中心-定时任务批量关闭")
public void batchOrgDisable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { 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()); 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") @PostMapping("/project/schedule/batch-disable")
@Operation(summary = "项目-任务中心-定时任务批量关闭") @Operation(summary = "项目-任务中心-定时任务批量关闭")
public void batchProjectDisable(@Validated @RequestBody TaskCenterScheduleBatchRequest request) { 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); 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<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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"));
}
}
} }

View File

@ -5,6 +5,8 @@ import com.github.pagehelper.page.PageMethod;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.HttpMethodConstants; 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.exception.MSException;
import io.metersphere.sdk.util.SubListUtils; import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.sdk.util.Translator; 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.schedule.ScheduleService;
import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import io.metersphere.system.utils.SessionUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; 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<OptionDTO> projectList, boolean enable, String projectId) { private void batchOperation(TaskCenterScheduleBatchRequest request, String userId, String path, String module, List<OptionDTO> projectList, boolean enable, String projectId) {
List<Schedule> scheduleList = new ArrayList<>(); List<Schedule> scheduleList;
if (request.isSelectAll()) { if (request.isSelectAll()) {
List<String> projectIds = projectList.stream().map(OptionDTO::getId).toList(); List<String> projectIds = projectList.stream().map(OptionDTO::getId).toList();
scheduleList = extScheduleMapper.getSchedule(request, projectIds); scheduleList = extScheduleMapper.getSchedule(request, projectIds);
@ -316,4 +319,38 @@ public class TaskCenterService {
List<OptionDTO> projectList = getProjectOption(projectId); List<OptionDTO> projectList = getProjectOption(projectId);
batchOperation(request, userId, path, module, projectList, enable, projectId); batchOperation(request, userId, path, module, projectList, enable, projectId);
} }
public void hasPermission(String type, String moduleType, String orgId, String projectId) {
Map<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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"));
}
}
} }