From a8e235e880242bbcb95f1e76bc6281c09d2f6d84 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 17 Nov 2026 23:19:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=BA=E5=88=B6=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=BB=85=E5=85=81=E8=AE=B8=E6=B5=81=E7=A8=8B=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E6=93=8D=E4=BD=9C=E5=B9=B6=E5=BC=80=E6=94=BE=E6=9D=83?= =?UTF-8?q?=E9=99=90=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - FlowForceSyncService 校验操作人 isFlowManager(),非流程管理员抛出 FlowPermissionException,避免绕过前端直接调用接口; - 新增 GET /api/query/workflow/permission/forceSync,供前端控制操作入口显隐; - 补充非流程管理员拒绝同步的单元测试。 Refs #281 --- .../service/impl/FlowForceSyncService.java | 9 ++++- .../service/FlowForceSyncServiceTest.java | 33 +++++++++++++++---- .../controller/WorkflowQueryController.java | 21 ++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/service/impl/FlowForceSyncService.java b/flow-engine-framework/src/main/java/com/codingapi/flow/service/impl/FlowForceSyncService.java index fdef480a..8b6d1d7c 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/service/impl/FlowForceSyncService.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/service/impl/FlowForceSyncService.java @@ -3,8 +3,10 @@ import com.codingapi.flow.event.FlowEventPublisher; import com.codingapi.flow.event.FlowRecordForceSyncEvent; import com.codingapi.flow.exception.FlowNotFoundException; +import com.codingapi.flow.exception.FlowPermissionException; import com.codingapi.flow.exception.FlowStateException; import com.codingapi.flow.mock.MockRepositoryHolder; +import com.codingapi.flow.operator.IFlowOperator; import com.codingapi.flow.pojo.request.FlowForceSyncRequest; import com.codingapi.flow.pojo.response.FlowForceSyncResult; import com.codingapi.flow.record.FlowRecord; @@ -60,9 +62,14 @@ public FlowForceSyncResult sync() { // 最新设计必须通过校验,避免把不合法的设计同步给运行实例 latestWorkflow.verify(); - if (repositoryHolder.getOperatorById(request.getOperatorId()) == null) { + IFlowOperator operator = repositoryHolder.getOperatorById(request.getOperatorId()); + if (operator == null) { throw FlowNotFoundException.operator(request.getOperatorId()); } + // 强制同步是流程管理员对运行中实例的高风险干预操作,仅流程管理员可执行 + if (!operator.isFlowManager()) { + throw FlowPermissionException.accessDenied("force sync flow instance"); + } Map> recordsByProcess = this.loadRunningInstances(); diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowForceSyncServiceTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowForceSyncServiceTest.java index 6ff258fa..0d65d9b8 100644 --- a/flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowForceSyncServiceTest.java +++ b/flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowForceSyncServiceTest.java @@ -6,6 +6,7 @@ import com.codingapi.flow.builder.FormFieldPermissionsBuilder; import com.codingapi.flow.builder.NodeStrategyBuilder; import com.codingapi.flow.context.GatewayContext; +import com.codingapi.flow.exception.FlowPermissionException; import com.codingapi.flow.exception.FlowStateException; import com.codingapi.flow.factory.MyFlowServiceFactory; import com.codingapi.flow.form.DataType; @@ -54,6 +55,11 @@ class FlowForceSyncServiceTest { private final MyFlowServiceFactory factory = new MyFlowServiceFactory(); + /** + * 流程管理员,强制同步仅允许流程管理员操作 + */ + private final User admin = new User(99, "admin", true); + private final User a1 = new User(11, "a1"); private final User a2 = new User(12, "a2"); private final User a3 = new User(13, "a3"); @@ -77,7 +83,7 @@ void should_rebind_selected_instance_and_split_merged_todo() { // when 只同步 process1 renameBNode(workflow, "B审批-改名后"); FlowForceSyncResult result = factory.flowService.forceSync( - new FlowForceSyncRequest(WORK_CODE, List.of(process1), a1.getUserId())); + new FlowForceSyncRequest(WORK_CODE, List.of(process1), admin.getUserId())); // then 只有选中实例换绑到新快照 long newRuntimeId = result.getWorkflowRuntimeId(); @@ -121,7 +127,7 @@ void should_rebind_all_selected_instances_and_merge_into_one_group() { // when 同步两条实例 renameBNode(workflow, "B审批-改名后"); FlowForceSyncResult result = factory.flowService.forceSync( - new FlowForceSyncRequest(WORK_CODE, List.of(process1, process2), a1.getUserId())); + new FlowForceSyncRequest(WORK_CODE, List.of(process1, process2), admin.getUserId())); // then 两条实例共享同一份新快照,并合并为一条待办组 long newRuntimeId = result.getWorkflowRuntimeId(); @@ -138,11 +144,11 @@ void should_skip_instances_already_on_latest_runtime() { String process1 = createAndSubmit(a1, workflow); renameBNode(workflow, "B审批-改名后"); FlowForceSyncResult first = factory.flowService.forceSync( - new FlowForceSyncRequest(WORK_CODE, List.of(process1), a1.getUserId())); + new FlowForceSyncRequest(WORK_CODE, List.of(process1), admin.getUserId())); // when 再次同步同一实例 FlowForceSyncResult second = factory.flowService.forceSync( - new FlowForceSyncRequest(WORK_CODE, List.of(process1), a1.getUserId())); + new FlowForceSyncRequest(WORK_CODE, List.of(process1), admin.getUserId())); // then 幂等跳过 assertEquals(first.getWorkflowRuntimeId(), second.getWorkflowRuntimeId()); @@ -164,7 +170,7 @@ void should_reject_when_node_added() { // when / then 拒绝同步,且实例未被改写 assertThrows(FlowStateException.class, () -> factory.flowService.forceSync( - new FlowForceSyncRequest(WORK_CODE, List.of(process1), a1.getUserId()))); + new FlowForceSyncRequest(WORK_CODE, List.of(process1), admin.getUserId()))); assertEquals(oldRuntimeId, runtimeIdOf(process1), "校验失败时不应改写实例引用"); } @@ -178,7 +184,7 @@ void should_reject_when_node_order_changed() { // when / then assertThrows(FlowStateException.class, () -> factory.flowService.forceSync( - new FlowForceSyncRequest(WORK_CODE, List.of(process1), a1.getUserId()))); + new FlowForceSyncRequest(WORK_CODE, List.of(process1), admin.getUserId()))); } @Test @@ -191,7 +197,7 @@ void should_reject_when_action_removed() { // when / then assertThrows(FlowStateException.class, () -> factory.flowService.forceSync( - new FlowForceSyncRequest(WORK_CODE, List.of(process1), a1.getUserId()))); + new FlowForceSyncRequest(WORK_CODE, List.of(process1), admin.getUserId()))); } @Test @@ -208,12 +214,25 @@ void should_reject_when_instance_not_running() { // when / then assertThrows(FlowStateException.class, () -> factory.flowService.forceSync( + new FlowForceSyncRequest(WORK_CODE, List.of(process1), admin.getUserId()))); + } + + @Test + void should_reject_when_operator_not_flow_manager() { + // given 非流程管理员发起同步 + Workflow workflow = prepareWorkflow(); + String process1 = createAndSubmit(a1, workflow); + renameBNode(workflow, "B审批-改名后"); + + // when / then + assertThrows(FlowPermissionException.class, () -> factory.flowService.forceSync( new FlowForceSyncRequest(WORK_CODE, List.of(process1), a1.getUserId()))); } // ─── 辅助方法 ─── private Workflow prepareWorkflow() { + factory.userGateway.save(admin); factory.userGateway.save(a1); factory.userGateway.save(a2); factory.userGateway.save(a3); diff --git a/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/WorkflowQueryController.java b/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/WorkflowQueryController.java index a89ea31f..a0893bb7 100644 --- a/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/WorkflowQueryController.java +++ b/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/WorkflowQueryController.java @@ -5,9 +5,12 @@ import com.codingapi.flow.infra.jpa.WorkflowVersionEntityRepository; import com.codingapi.flow.infra.pojo.WorkflowOption; import com.codingapi.flow.infra.pojo.WorkflowVersionOption; +import com.codingapi.flow.operator.IFlowOperator; import com.codingapi.springboot.framework.dto.request.IdRequest; import com.codingapi.springboot.framework.dto.request.SearchRequest; import com.codingapi.springboot.framework.dto.response.MultiResponse; +import com.codingapi.springboot.framework.dto.response.SingleResponse; +import com.codingapi.springboot.framework.user.UserContext; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -38,4 +41,22 @@ public MultiResponse versions(IdRequest request) { return MultiResponse.of(workflowVersionEntityRepository.versions(request.getStringId())); } + /** + * 当前用户是否可执行「强制同步运行中实例」(仅流程管理员可操作) + */ + @GetMapping("/permission/forceSync") + public SingleResponse forceSyncPermission() { + return SingleResponse.of(isFlowManager()); + } + + private boolean isFlowManager() { + try { + Object current = UserContext.getInstance().current(); + return current instanceof IFlowOperator operator && operator.isFlowManager(); + } catch (Exception e) { + // 未登录或无用户上下文时视为无权限 + return false; + } + } + }