Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, List<FlowRecord>> recordsByProcess = this.loadRunningInstances();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand All @@ -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), "校验失败时不应改写实例引用");
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,4 +41,22 @@ public MultiResponse<WorkflowVersionOption> versions(IdRequest request) {
return MultiResponse.of(workflowVersionEntityRepository.versions(request.getStringId()));
}

/**
* 当前用户是否可执行「强制同步运行中实例」(仅流程管理员可操作)
*/
@GetMapping("/permission/forceSync")
public SingleResponse<Boolean> 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;
}
}

}
Loading