From a5199a0385dc397eee1121cbcf6c40586f9b0bc0 Mon Sep 17 00:00:00 2001
From: lorne <1991wangliang@gmail.com>
Date: Mon, 23 Nov 2026 21:30:04 +0800
Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=93=8D?=
=?UTF-8?q?=E4=BD=9C=E8=80=85=E8=BA=AB=E4=BB=BD=E6=A0=87=E8=AF=86=E7=9A=84?=
=?UTF-8?q?=E5=AD=98=E5=82=A8=E4=B8=8E=E8=84=9A=E6=9C=AC=E8=AF=BB=E5=8F=96?=
=?UTF-8?q?=EF=BC=88issue=20#288=20P1=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- IFlowOperator 新增 default getIdentity()/getIdentityAttributes(),下游存量实现零改动即可编译
- 新增 OperatorIdentity:null 与空串统一归一化为默认身份,供全链路比较与存储复用
- FlowRecord 增加发起/提交/当前/转交/干预五组身份字段,FlowTodoRecord 增加三组身份字段
- FlowRecordEntity/FlowTodoRecordEntity 与 Convertor 双向映射,历史数据 identity 为 null 时读取归一化为空串
- GroovyScriptRequest 暴露 getCreatedOperatorIdentity/getCurrentOperatorIdentity/getSubmitOperatorIdentity
- FlowOperator 响应体携带身份,保留 (id, name) 双参构造器保证下游源码兼容
- P1 不改变任何判定逻辑与待办合并键格式,identity 为空时行为与历史一致
Closes #288
---
.../flow/operator/IFlowOperator.java | 36 ++++
.../flow/operator/OperatorIdentity.java | 66 ++++++
.../flow/pojo/response/FlowContent.java | 15 +-
.../flow/pojo/response/FlowOperator.java | 25 ++-
.../com/codingapi/flow/record/FlowRecord.java | 38 ++++
.../codingapi/flow/record/FlowTodoRecord.java | 22 ++
.../script/request/GroovyScriptRequest.java | 34 +++
.../flow/operator/OperatorIdentityTest.java | 131 ++++++++++++
.../flow/pojo/response/FlowOperatorTest.java | 60 ++++++
.../CopyOnReadFlowRecordRepository.java | 7 +-
.../GroovyScriptRequestIdentityTest.java | 94 +++++++++
.../FlowOperatorIdentityRecordTest.java | 194 ++++++++++++++++++
.../java/com/codingapi/flow/user/User.java | 34 ++-
.../infra/convert/FlowRecordConvertor.java | 14 +-
.../convert/FlowTodoRecordConvertor.java | 10 +-
.../flow/infra/entity/FlowRecordEntity.java | 25 +++
.../infra/entity/FlowTodoRecordEntity.java | 15 ++
.../FlowRecordIdentityConvertorTest.java | 152 ++++++++++++++
18 files changed, 959 insertions(+), 13 deletions(-)
create mode 100644 flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java
create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityTest.java
create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/pojo/response/FlowOperatorTest.java
create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/script/request/GroovyScriptRequestIdentityTest.java
create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowOperatorIdentityRecordTest.java
create mode 100644 flow-engine-starter-infra/src/test/java/com/codingapi/flow/infra/convert/FlowRecordIdentityConvertorTest.java
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/operator/IFlowOperator.java b/flow-engine-framework/src/main/java/com/codingapi/flow/operator/IFlowOperator.java
index b868fb69..a52d75e0 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/operator/IFlowOperator.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/operator/IFlowOperator.java
@@ -3,8 +3,13 @@
import com.codingapi.flow.script.request.GroovyScriptRequest;
import com.codingapi.springboot.framework.user.IUser;
+import java.util.Map;
+
/**
* 流程参与用户
+ *
+ *
支持同一用户以多种身份参与流程(issue #288):身份标识由下游业务自定义,
+ * 框架不解释其含义,仅做透传、存储与比较。
*/
public interface IFlowOperator extends IUser {
@@ -24,6 +29,37 @@ public interface IFlowOperator extends IUser {
String getName();
+ /**
+ * 获取身份标识
+ *
+ *
身份标识的取值语义由下游自定义(部门 id、岗位编码或复合标识均可),
+ * 框架只做透传、存储与比较,不做任何解释。
+ *
+ * - 返回空串或 null 表示「默认身份」,与未启用多身份的历史行为完全一致;
+ * - 同一用户的不同身份在流程中被视为不同的参与人。
+ *
+ *
+ * @return 身份标识,默认返回空串
+ * @see OperatorIdentity
+ */
+ default String getIdentity() {
+ return OperatorIdentity.DEFAULT;
+ }
+
+
+ /**
+ * 获取身份附加属性
+ *
+ * 供 Groovy 脚本与前端展示使用的动态字段(如部门名称、岗位名称),
+ * 下游可自由定义 key/value。框架只透传,不落库、不参与任何引擎判定。
+ *
+ * @return 身份附加属性,默认返回空集合
+ */
+ default Map getIdentityAttributes() {
+ return Map.of();
+ }
+
+
/**
* 是否流程管理员
* 流程管理员可以强制干预流程
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java b/flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java
new file mode 100644
index 00000000..b31beb23
--- /dev/null
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java
@@ -0,0 +1,66 @@
+package com.codingapi.flow.operator;
+
+/**
+ * 身份标识归一化工具。
+ *
+ * 流程引擎不解释身份标识的业务语义,只做透传、存储与比较。为了让「存量数据」与
+ * 「未启用多身份的业务」保持完全一致的行为,统一约定:
+ *
+ * - {@code null} 与空串等价,均视为「默认身份」;
+ * - 所有身份读取、写入、比较前都必须经过 {@link #normalize(String)},禁止直接使用原始值比较;
+ * - 默认身份的存取不改变既有数据格式(例如待办合并键在默认身份下保持历史格式)。
+ *
+ *
+ * @see IFlowOperator#getIdentity()
+ */
+public final class OperatorIdentity {
+
+ /**
+ * 默认身份标识
+ */
+ public static final String DEFAULT = "";
+
+ private OperatorIdentity() {
+ }
+
+ /**
+ * 归一化身份标识:null 转为空串,其余原样返回。
+ *
+ * @param identity 原始身份标识
+ * @return 归一化后的身份标识,不为 null
+ */
+ public static String normalize(String identity) {
+ return identity == null ? DEFAULT : identity;
+ }
+
+ /**
+ * 是否为默认身份(null 或空串)。
+ *
+ * @param identity 身份标识
+ * @return true 表示默认身份
+ */
+ public static boolean isDefault(String identity) {
+ return normalize(identity).isEmpty();
+ }
+
+ /**
+ * 读取操作者的身份标识,操作者为 null 时返回默认身份。
+ *
+ * @param operator 流程参与用户
+ * @return 归一化后的身份标识
+ */
+ public static String of(IFlowOperator operator) {
+ return operator == null ? DEFAULT : normalize(operator.getIdentity());
+ }
+
+ /**
+ * 判断两个身份标识是否相同(null 安全)。
+ *
+ * @param left 身份标识
+ * @param right 身份标识
+ * @return true 表示同一身份
+ */
+ public static boolean same(String left, String right) {
+ return normalize(left).equals(normalize(right));
+ }
+}
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowContent.java b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowContent.java
index 8a935f29..055c70a5 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowContent.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowContent.java
@@ -319,7 +319,8 @@ private List loadCurrentNodeOperators(FlowSession flowSession) {
continue;
}
operators.putIfAbsent(record.getCurrentOperatorId(),
- new FlowOperator(record.getCurrentOperatorId(), record.getCurrentOperatorName()));
+ new FlowOperator(record.getCurrentOperatorId(), record.getCurrentOperatorName(),
+ record.getCurrentOperatorIdentity()));
}
return List.copyOf(operators.values());
}
@@ -336,7 +337,8 @@ public void pushWorkflow(Workflow workflow) {
public void pushRecords(FlowRecord record, List mergeRecords) {
this.recordId = record.getId();
this.processId = record.getProcessId();
- this.createOperator = new FlowOperator(record.getCreateOperatorId(), record.getCreateOperatorName());
+ this.createOperator = new FlowOperator(record.getCreateOperatorId(), record.getCreateOperatorName(),
+ record.getCreateOperatorIdentity());
this.mergeable = record.isTodo() && record.isMergeable();
this.flowState = record.getFlowState();
this.recordState = record.getRecordState();
@@ -352,8 +354,10 @@ public void pushRecords(FlowRecord record, List mergeRecords) {
body.setNodeId(item.getNodeId());
body.setNodeName(item.getNodeName());
body.setNodeType(item.getNodeType());
- body.setSubmitOperator(new FlowOperator(item.getSubmitOperatorId(), item.getSubmitOperatorName()));
- body.setCreatedOperator(new FlowOperator(item.getCreateOperatorId(), item.getCreateOperatorName()));
+ body.setSubmitOperator(new FlowOperator(item.getSubmitOperatorId(), item.getSubmitOperatorName(),
+ item.getSubmitOperatorIdentity()));
+ body.setCreatedOperator(new FlowOperator(item.getCreateOperatorId(), item.getCreateOperatorName(),
+ item.getCreateOperatorIdentity()));
body.setTitle(item.getTitle());
body.setData(item.getFormData());
body.setRecordState(item.getRecordState());
@@ -376,7 +380,8 @@ public void pushHistory(Workflow workflow, List historyRecords) {
history.setNodeId(item.getNodeId());
history.setNodeType(item.getNodeType());
history.setUpdateTime(item.getUpdateTime());
- history.setCurrentOperator(new FlowOperator(item.getCurrentOperatorId(), item.getCurrentOperatorName()));
+ history.setCurrentOperator(new FlowOperator(item.getCurrentOperatorId(), item.getCurrentOperatorName(),
+ item.getCurrentOperatorIdentity()));
this.histories.add(history);
}
}
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowOperator.java b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowOperator.java
index 82a61b1c..fa499a29 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowOperator.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowOperator.java
@@ -1,21 +1,42 @@
package com.codingapi.flow.pojo.response;
import com.codingapi.flow.operator.IFlowOperator;
-import lombok.AllArgsConstructor;
+import com.codingapi.flow.operator.OperatorIdentity;
import lombok.Data;
/**
* 流程审批人
+ *
+ * 响应体为公开契约:保留 (id, name) 双参构造器保证下游源码兼容,
+ * 身份标识通过新增构造器或 setter 补充(issue #288)。
*/
@Data
-@AllArgsConstructor
public class FlowOperator {
private long id;
private String name;
+ /**
+ * 身份标识,空串表示默认身份
+ */
+ private String identity;
+
+ /**
+ * 兼容历史调用方的双参构造器,身份为默认身份
+ */
+ public FlowOperator(long id, String name) {
+ this(id, name, OperatorIdentity.DEFAULT);
+ }
+
+ public FlowOperator(long id, String name, String identity) {
+ this.id = id;
+ this.name = name;
+ this.identity = OperatorIdentity.normalize(identity);
+ }
+
public FlowOperator(IFlowOperator flowOperator) {
this.id = flowOperator.getUserId();
this.name = flowOperator.getName();
+ this.identity = OperatorIdentity.of(flowOperator);
}
}
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java b/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java
index bd0fa18e..8471cf13 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java
@@ -9,6 +9,7 @@
import com.codingapi.flow.node.nodes.EndNode;
import com.codingapi.flow.node.nodes.NotifyNode;
import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.operator.OperatorIdentity;
import com.codingapi.flow.session.FlowAdvice;
import com.codingapi.flow.session.FlowSession;
import com.codingapi.flow.session.IRepositoryHolder;
@@ -266,6 +267,34 @@ public class FlowRecord {
*/
private int parallelBranchTotal;
+ /**
+ * 发起者身份标识(issue #288)
+ *
框架不解释其语义,空串表示默认身份,与未启用多身份的历史数据完全一致。
+ *
+ * @see com.codingapi.flow.operator.OperatorIdentity
+ */
+ private String createOperatorIdentity;
+
+ /**
+ * 提交审批人身份标识(issue #288)
+ */
+ private String submitOperatorIdentity;
+
+ /**
+ * 当前审批人身份标识(issue #288)
+ */
+ private String currentOperatorIdentity;
+
+ /**
+ * 代替的审批人身份标识(issue #288)
+ */
+ private String forwardOperatorIdentity;
+
+ /**
+ * 被干预的用户身份标识(issue #288)
+ */
+ private String interferedOperatorIdentity;
+
/**
* 数据合并的依据,当开启时值为固定值,否则为随机数据
*
合并依据由 {@link RecordMergeStrategy.MergeType} 决定:
@@ -326,12 +355,17 @@ public FlowRecord(FlowSession flowSession, int nodeOrder) {
this.processId = FlowIDGeneratorGatewayContext.getInstance().generateProcessId();
this.createOperatorId = flowSession.getCreatedOperator().getUserId();
this.createOperatorName = flowSession.getCreatedOperator().getName();
+ this.createOperatorIdentity = OperatorIdentity.of(flowSession.getCreatedOperator());
+ // 身份字段统一以空串兜底,避免出现 null 与空串两种「默认身份」表达
+ this.forwardOperatorIdentity = OperatorIdentity.DEFAULT;
+ this.interferedOperatorIdentity = OperatorIdentity.DEFAULT;
// 是否转交之后的人来审批的判断
if (currentOperator.equals(sourceOperator)) {
this.forwardOperatorId = 0;
} else {
this.forwardOperatorId = sourceOperator.getUserId();
this.forwardOperatorName = sourceOperator.getName();
+ this.forwardOperatorIdentity = OperatorIdentity.of(sourceOperator);
}
this.recordState = SATE_RECORD_TODO;
this.actionId = action.id();
@@ -340,9 +374,11 @@ public FlowRecord(FlowSession flowSession, int nodeOrder) {
this.currentOperatorId = currentOperator.getUserId();
this.currentOperatorName = currentOperator.getName();
+ this.currentOperatorIdentity = OperatorIdentity.of(currentOperator);
this.submitOperatorId = flowSession.getSubmitOperatorId();
this.submitOperatorName = flowSession.getSubmitOperatorName();
+ this.submitOperatorIdentity = OperatorIdentity.of(flowSession.getSubmitOperator());
this.advice = flowSession.getAdvice().getAdvice();
this.signKey = flowSession.getAdvice().getSignKey();
@@ -559,6 +595,7 @@ public void update(FlowSession flowSession, boolean pass) {
if (flowSession.getCurrentOperator().getUserId() != this.currentOperatorId) {
this.interferedOperatorId = flowSession.getCurrentOperator().getUserId();
this.interferedOperatorName = flowSession.getCurrentOperator().getName();
+ this.interferedOperatorIdentity = OperatorIdentity.of(flowSession.getCurrentOperator());
}
if (flowAdvice != null) {
@@ -694,6 +731,7 @@ public FlowRecord create(FlowSession flowSession) {
FlowRecord flowRecord = new FlowRecord(flowSession, 0);
flowRecord.currentOperatorId = flowSession.getCurrentOperator().getUserId();
flowRecord.currentOperatorName = flowSession.getCurrentOperator().getName();
+ flowRecord.currentOperatorIdentity = OperatorIdentity.of(flowSession.getCurrentOperator());
return flowRecord;
}
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowTodoRecord.java b/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowTodoRecord.java
index 09206513..1ef55639 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowTodoRecord.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowTodoRecord.java
@@ -1,5 +1,6 @@
package com.codingapi.flow.record;
+import com.codingapi.flow.operator.OperatorIdentity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@@ -120,6 +121,24 @@ public class FlowTodoRecord {
*/
private long timeoutTime;
+ /**
+ * 当前审批人身份标识(issue #288)
+ *
框架不解释其语义,空串表示默认身份,与未启用多身份的历史数据完全一致。
+ *
+ * @see com.codingapi.flow.operator.OperatorIdentity
+ */
+ private String currentOperatorIdentity;
+
+ /**
+ * 提交审批人身份标识(issue #288)
+ */
+ private String submitOperatorIdentity;
+
+ /**
+ * 发起者身份标识(issue #288)
+ */
+ private String createOperatorIdentity;
+
public FlowTodoRecord(FlowRecord flowRecord) {
this.update(flowRecord);
@@ -144,6 +163,9 @@ public void update(FlowRecord flowRecord) {
this.createTime = flowRecord.getCreateTime();
this.createOperatorId = flowRecord.getCreateOperatorId();
this.createOperatorName = flowRecord.getCreateOperatorName();
+ this.currentOperatorIdentity = OperatorIdentity.normalize(flowRecord.getCurrentOperatorIdentity());
+ this.submitOperatorIdentity = OperatorIdentity.normalize(flowRecord.getSubmitOperatorIdentity());
+ this.createOperatorIdentity = OperatorIdentity.normalize(flowRecord.getCreateOperatorIdentity());
this.todoKey = flowRecord.getTodoKey();
this.mergeable = flowRecord.isMergeable();
this.recordId = flowRecord.getId();
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/script/request/GroovyScriptRequest.java b/flow-engine-framework/src/main/java/com/codingapi/flow/script/request/GroovyScriptRequest.java
index d030ffaf..84bb2e02 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/script/request/GroovyScriptRequest.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/script/request/GroovyScriptRequest.java
@@ -4,6 +4,7 @@
import com.codingapi.flow.form.FormData;
import com.codingapi.flow.node.IFlowNode;
import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.operator.OperatorIdentity;
import com.codingapi.flow.pojo.request.FlowCreateRequest;
import com.codingapi.flow.record.FlowRecord;
import com.codingapi.flow.session.FlowSession;
@@ -281,6 +282,39 @@ public String getSubmitOperatorName() {
}
+ /**
+ * 流程创建者身份标识(issue #288)
+ *
身份标识由下游自定义,框架不做解释;空串表示默认身份。
+ * 可用于按部门、岗位等身份差异决定节点分支。
+ *
+ * @return 创建者身份标识
+ */
+ @ScriptFunction(name = "getCreatedOperatorIdentity", description = "获取流程创建者身份标识")
+ public String getCreatedOperatorIdentity() {
+ return OperatorIdentity.of(this.createdOperator);
+ }
+
+ /**
+ * 流程审批者身份标识(issue #288)
+ *
+ * @return 当前审批者身份标识
+ */
+ @ScriptFunction(name = "getCurrentOperatorIdentity", description = "获取流程审批者身份标识")
+ public String getCurrentOperatorIdentity() {
+ return OperatorIdentity.of(this.currentOperator);
+ }
+
+ /**
+ * 流程提交人身份标识(issue #288)
+ *
+ * @return 提交人身份标识
+ */
+ @ScriptFunction(name = "getSubmitOperatorIdentity", description = "获取流程提交人身份标识")
+ public String getSubmitOperatorIdentity() {
+ return OperatorIdentity.of(this.submitOperator);
+ }
+
+
/**
* 获取开始节点
*
diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityTest.java
new file mode 100644
index 00000000..73265cb6
--- /dev/null
+++ b/flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityTest.java
@@ -0,0 +1,131 @@
+package com.codingapi.flow.operator;
+
+import com.codingapi.flow.script.request.GroovyScriptRequest;
+import com.codingapi.flow.user.User;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * 身份标识归一化规则测试。
+ *
+ *
约定的核心规则:null 与空串等价,统一视为「默认身份」(issue #288)。
+ */
+class OperatorIdentityTest {
+
+ @Test
+ void should_normalize_null_to_empty() {
+ // given
+ String identity = null;
+
+ // when
+ String normalized = OperatorIdentity.normalize(identity);
+
+ // then
+ assertEquals("", normalized);
+ }
+
+ @Test
+ void should_keep_identity_value() {
+ // given
+ String identity = "department1";
+
+ // when
+ String normalized = OperatorIdentity.normalize(identity);
+
+ // then
+ assertEquals("department1", normalized);
+ }
+
+ @Test
+ void should_treat_null_and_empty_as_default() {
+ assertTrue(OperatorIdentity.isDefault(null));
+ assertTrue(OperatorIdentity.isDefault(""));
+ assertFalse(OperatorIdentity.isDefault("department1"));
+ }
+
+ @Test
+ void should_compare_identities_null_safe() {
+ assertTrue(OperatorIdentity.same(null, ""));
+ assertTrue(OperatorIdentity.same("department1", "department1"));
+ assertFalse(OperatorIdentity.same("department1", "department2"));
+ assertFalse(OperatorIdentity.same(null, "department1"));
+ }
+
+ @Test
+ void should_read_identity_from_operator_and_handle_null_operator() {
+ // given
+ IFlowOperator operator = new User(1, "张三");
+
+ // when & then
+ assertEquals("", OperatorIdentity.of(operator));
+ assertEquals("", OperatorIdentity.of(null));
+ }
+
+ @Test
+ void should_provide_default_identity_on_operator_interface() {
+ // given:未实现身份的存量操作者(测试 User 未覆盖 default 方法)
+ IFlowOperator operator = new IFlowOperator() {
+ @Override
+ public long getUserId() {
+ return 1;
+ }
+
+ @Override
+ public String getName() {
+ return "张三";
+ }
+
+ @Override
+ public boolean isFlowManager() {
+ return false;
+ }
+
+ @Override
+ public IFlowOperator forwardOperator(GroovyScriptRequest request) {
+ return null;
+ }
+ };
+
+ // when & then:默认身份为空串,附加属性为空集合,保证存量实现零改动可用
+ assertEquals("", operator.getIdentity());
+ assertTrue(operator.getIdentityAttributes().isEmpty());
+ }
+
+ @Test
+ void should_allow_downstream_override_identity() {
+ // given
+ IFlowOperator operator = new IdentityUser(1, "张三", "department1");
+
+ // when & then
+ assertEquals("department1", OperatorIdentity.of(operator));
+ assertEquals("部门一", operator.getIdentityAttributes().get("deptName"));
+ }
+
+ /**
+ * 模拟下游自定义身份的操作者实现。
+ */
+ private static class IdentityUser extends User {
+
+ private final String identity;
+
+ IdentityUser(long userId, String name, String identity) {
+ super(userId, name);
+ this.identity = identity;
+ }
+
+ @Override
+ public String getIdentity() {
+ return identity;
+ }
+
+ @Override
+ public Map getIdentityAttributes() {
+ return Map.of("deptName", "部门一");
+ }
+ }
+}
diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/pojo/response/FlowOperatorTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/pojo/response/FlowOperatorTest.java
new file mode 100644
index 00000000..35a0aba8
--- /dev/null
+++ b/flow-engine-framework/src/test/java/com/codingapi/flow/pojo/response/FlowOperatorTest.java
@@ -0,0 +1,60 @@
+package com.codingapi.flow.pojo.response;
+
+import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.user.User;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * 响应体操作者身份字段测试(issue #288 P1)。
+ *
+ * 响应体为公开契约,必须保留双参构造器以保证下游实现源码兼容。
+ */
+class FlowOperatorTest {
+
+ /**
+ * 测试目标:存量双参构造器仍然可用,身份默认空串。
+ * 前置条件:使用 (id, name) 构造。
+ * 执行步骤:读取身份。
+ * 期望断言:身份为空串,不出现 null。
+ */
+ @Test
+ void should_keep_two_args_constructor_and_default_identity() {
+ FlowOperator operator = new FlowOperator(1L, "张三");
+
+ assertEquals(1L, operator.getId());
+ assertEquals("张三", operator.getName());
+ assertEquals("", operator.getIdentity());
+ }
+
+ /**
+ * 测试目标:由流程操作者构建响应体时携带身份。
+ * 前置条件:操作者身份为 department1。
+ * 执行步骤:构造响应体。
+ * 期望断言:id、名称、身份均正确。
+ */
+ @Test
+ void should_take_identity_from_flow_operator() {
+ IFlowOperator user = User.withIdentity(2, "boss", "department2");
+
+ FlowOperator operator = new FlowOperator(user);
+
+ assertEquals(2L, operator.getId());
+ assertEquals("boss", operator.getName());
+ assertEquals("department2", operator.getIdentity());
+ }
+
+ /**
+ * 测试目标:三参构造器支持从记录直接还原带身份的响应体。
+ * 前置条件:记录中保存了审批人 id、名称与身份。
+ * 执行步骤:构造响应体。
+ * 期望断言:身份被正确设置。
+ */
+ @Test
+ void should_support_three_args_constructor() {
+ FlowOperator operator = new FlowOperator(3L, "lorne", "department3");
+
+ assertEquals("department3", operator.getIdentity());
+ }
+}
diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java b/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java
index 13f43cb0..2ee38d02 100644
--- a/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java
+++ b/flow-engine-framework/src/test/java/com/codingapi/flow/repository/CopyOnReadFlowRecordRepository.java
@@ -142,6 +142,11 @@ private static FlowRecord copy(FlowRecord record) {
record.getDelegateId(),
record.getParallelId(),
record.getParallelBranchNodeId(),
- record.getParallelBranchTotal());
+ record.getParallelBranchTotal(),
+ record.getCreateOperatorIdentity(),
+ record.getSubmitOperatorIdentity(),
+ record.getCurrentOperatorIdentity(),
+ record.getForwardOperatorIdentity(),
+ record.getInterferedOperatorIdentity());
}
}
\ No newline at end of file
diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/script/request/GroovyScriptRequestIdentityTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/script/request/GroovyScriptRequestIdentityTest.java
new file mode 100644
index 00000000..eceaf2c8
--- /dev/null
+++ b/flow-engine-framework/src/test/java/com/codingapi/flow/script/request/GroovyScriptRequestIdentityTest.java
@@ -0,0 +1,94 @@
+package com.codingapi.flow.script.request;
+
+import com.codingapi.flow.factory.MyFlowServiceFactory;
+import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.script.factory.FlowGroovyScriptFactory;
+import com.codingapi.flow.script.node.NodeTitleScript;
+import com.codingapi.flow.session.FlowSession;
+import com.codingapi.flow.user.User;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * 脚本请求对象的身份标识透传测试(issue #288 P1)。
+ *
+ *
验证下游可通过 Groovy 脚本读取发起人 / 提交人 / 当前审批人的身份标识,
+ * 用于实现节点分支差异与人员差异。
+ */
+class GroovyScriptRequestIdentityTest {
+
+ private final MyFlowServiceFactory factory = new MyFlowServiceFactory();
+
+ private FlowSession session(IFlowOperator current, IFlowOperator created, IFlowOperator submit) {
+ return new FlowSession(
+ factory.repositoryHolder,
+ current,
+ created,
+ submit,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null,
+ 0,
+ null
+ );
+ }
+
+ /**
+ * 测试目标:请求对象直接读取三类操作者的身份标识。
+ * 前置条件:当前审批人、发起人、提交人分别带不同身份。
+ * 执行步骤:由会话构建 GroovyScriptRequest 并读取身份。
+ * 期望断言:三个身份方法分别返回各自身份。
+ */
+ @Test
+ void should_expose_identity_getters() {
+ IFlowOperator current = User.withIdentity(2, "boss", "department2");
+ IFlowOperator created = User.withIdentity(1, "user1", "department1");
+
+ GroovyScriptRequest request = new GroovyScriptRequest(session(current, created, created));
+
+ assertEquals("department2", request.getCurrentOperatorIdentity());
+ assertEquals("department1", request.getCreatedOperatorIdentity());
+ assertEquals("department1", request.getSubmitOperatorIdentity());
+ }
+
+ /**
+ * 测试目标:存量用户未实现身份时,脚本读取到默认身份(空串)而非 null。
+ * 前置条件:三类操作者均未覆盖 getIdentity()。
+ * 执行步骤:构建请求并读取身份。
+ * 期望断言:三个身份方法均返回空串。
+ */
+ @Test
+ void should_return_default_identity_when_operator_has_no_identity() {
+ IFlowOperator user = new User(1, "user1");
+
+ GroovyScriptRequest request = new GroovyScriptRequest(session(user, user, user));
+
+ assertEquals("", request.getCurrentOperatorIdentity());
+ assertEquals("", request.getCreatedOperatorIdentity());
+ assertEquals("", request.getSubmitOperatorIdentity());
+ }
+
+ /**
+ * 测试目标:Groovy 脚本内可通过 request 读取当前审批人身份(节点分支差异的基础能力)。
+ * 前置条件:当前审批人身份为 department2。
+ * 执行步骤:执行节点标题脚本返回身份标识。
+ * 期望断言:脚本返回 department2。
+ */
+ @Test
+ void should_read_identity_in_groovy_script() {
+ IFlowOperator current = User.withIdentity(2, "boss", "department2");
+ IFlowOperator created = User.withIdentity(1, "user1", "department1");
+
+ NodeTitleScript script = new NodeTitleScript(
+ FlowGroovyScriptFactory.createNodeTitleScript("def run(request){return request.getCurrentOperatorIdentity()}")
+ .getKey());
+
+ String identity = script.execute(session(current, created, created));
+
+ assertEquals("department2", identity);
+ }
+}
diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowOperatorIdentityRecordTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowOperatorIdentityRecordTest.java
new file mode 100644
index 00000000..34c6174c
--- /dev/null
+++ b/flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowOperatorIdentityRecordTest.java
@@ -0,0 +1,194 @@
+package com.codingapi.flow.service;
+
+import com.codingapi.flow.action.IFlowAction;
+import com.codingapi.flow.action.actions.CustomAction;
+import com.codingapi.flow.builder.ActionBuilder;
+import com.codingapi.flow.builder.FormFieldPermissionsBuilder;
+import com.codingapi.flow.builder.NodeStrategyBuilder;
+import com.codingapi.flow.context.GatewayContext;
+import com.codingapi.flow.factory.MyFlowServiceFactory;
+import com.codingapi.flow.form.DataType;
+import com.codingapi.flow.form.FlowForm;
+import com.codingapi.flow.form.FlowFormBuilder;
+import com.codingapi.flow.form.permission.PermissionType;
+import com.codingapi.flow.node.nodes.ApprovalNode;
+import com.codingapi.flow.node.nodes.EndNode;
+import com.codingapi.flow.node.nodes.StartNode;
+import com.codingapi.flow.pojo.body.FlowAdviceBody;
+import com.codingapi.flow.pojo.request.FlowActionRequest;
+import com.codingapi.flow.pojo.request.FlowCreateRequest;
+import com.codingapi.flow.record.FlowRecord;
+import com.codingapi.flow.record.FlowTodoRecord;
+import com.codingapi.flow.script.factory.FlowGroovyScriptFactory;
+import com.codingapi.flow.strategy.node.FormFieldPermissionStrategy;
+import com.codingapi.flow.strategy.node.OperatorLoadStrategy;
+import com.codingapi.flow.user.User;
+import com.codingapi.flow.workflow.Workflow;
+import com.codingapi.flow.workflow.WorkflowBuilder;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * 流程记录身份标识落库测试(issue #288 P1)。
+ *
+ *
P1 只做身份透传与存储,不改变任何判定逻辑:身份为空时记录行为与历史完全一致。
+ */
+class FlowOperatorIdentityRecordTest {
+
+ private MyFlowServiceFactory newFactory(User... users) {
+ MyFlowServiceFactory factory = new MyFlowServiceFactory();
+ for (User user : users) {
+ factory.userGateway.save(user);
+ }
+ GatewayContext.getInstance().setFlowOperatorGateway(factory.userGateway);
+ return factory;
+ }
+
+ private Workflow buildLeaveWorkflow(MyFlowServiceFactory factory, User creator) {
+ FlowForm form = FlowFormBuilder.builder()
+ .name("请假流程")
+ .code("leave")
+ .addField("请假人", "name", DataType.STRING)
+ .addField("请假天数", "days", DataType.INTEGER)
+ .build();
+
+ StartNode startNode = StartNode.builder()
+ .strategies(NodeStrategyBuilder.builder()
+ .addStrategy(new FormFieldPermissionStrategy(FormFieldPermissionsBuilder.builder()
+ .addPermission("leave", "name", PermissionType.WRITE)
+ .addPermission("leave", "days", PermissionType.WRITE)
+ .build()))
+ .build())
+ .actions(ActionBuilder.builder()
+ .addAction(CustomAction.defaultAction())
+ .build())
+ .build();
+
+ ApprovalNode bossNode = ApprovalNode.builder()
+ .name("经理审批")
+ .strategies(NodeStrategyBuilder.builder()
+ .addStrategy(new FormFieldPermissionStrategy(FormFieldPermissionsBuilder.builder()
+ .addPermission("leave", "name", PermissionType.WRITE)
+ .addPermission("leave", "days", PermissionType.WRITE)
+ .build()))
+ .addStrategy(new OperatorLoadStrategy(
+ FlowGroovyScriptFactory.createOperatorLoadScript("def run(request){return [2]}").getKey()))
+ .build())
+ .build();
+
+ EndNode endNode = EndNode.builder().build();
+
+ Workflow workflow = WorkflowBuilder.builder()
+ .title("请假流程")
+ .code("leave")
+ .createdOperator(creator)
+ .form(form)
+ .addNode(startNode)
+ .addNode(bossNode)
+ .addNode(endNode)
+ .build();
+
+ factory.workflowService.saveWorkflow(workflow);
+ return workflow;
+ }
+
+ private Map formData() {
+ return Map.of("name", "lorne", "days", 1);
+ }
+
+ private List startActions(Workflow workflow) {
+ return workflow.getStartNode().actionManager().getActions();
+ }
+
+ private long createAtStartNode(MyFlowServiceFactory factory, Workflow workflow, User user) {
+ FlowCreateRequest createRequest = new FlowCreateRequest();
+ createRequest.setWorkCode(workflow.getCode());
+ createRequest.setFormData(formData());
+ createRequest.setActionId(startActions(workflow).get(0).id());
+ createRequest.setOperatorId(user.getUserId());
+ return factory.flowService.create(createRequest);
+ }
+
+ /**
+ * 场景:发起人带身份发起流程 → 开始节点记录与待办记录都落发起人身份。
+ */
+ @Test
+ void should_store_creator_identity_on_start_record_and_todo() {
+ User creator = User.withIdentity(1, "user1", "department1");
+ User boss = User.withIdentity(2, "boss", "department2");
+ MyFlowServiceFactory factory = newFactory(creator, boss);
+
+ Workflow workflow = buildLeaveWorkflow(factory, creator);
+ long recordId = createAtStartNode(factory, workflow, creator);
+
+ FlowRecord record = factory.flowRecordRepository.get(recordId);
+ assertNotNull(record);
+ assertEquals("department1", record.getCreateOperatorIdentity());
+ assertEquals("department1", record.getSubmitOperatorIdentity());
+ assertEquals("department1", record.getCurrentOperatorIdentity());
+ assertEquals("", record.getForwardOperatorIdentity());
+ assertEquals("", record.getInterferedOperatorIdentity());
+
+ List todos = factory.flowTodoRecordRepository.findByOperatorId(creator.getUserId());
+ assertEquals(1, todos.size());
+ FlowTodoRecord todo = todos.get(0);
+ assertEquals("department1", todo.getCreateOperatorIdentity());
+ assertEquals("department1", todo.getSubmitOperatorIdentity());
+ assertEquals("department1", todo.getCurrentOperatorIdentity());
+ }
+
+ /**
+ * 场景:存量用户未实现身份 → 全部落默认身份(空串),与历史行为一致。
+ */
+ @Test
+ void should_store_default_identity_when_operator_has_no_identity() {
+ User creator = new User(1, "user1");
+ User boss = new User(2, "boss");
+ MyFlowServiceFactory factory = newFactory(creator, boss);
+
+ Workflow workflow = buildLeaveWorkflow(factory, creator);
+ long recordId = createAtStartNode(factory, workflow, creator);
+
+ FlowRecord record = factory.flowRecordRepository.get(recordId);
+ assertNotNull(record);
+ assertTrue(record.getCreateOperatorIdentity().isEmpty());
+ assertTrue(record.getSubmitOperatorIdentity().isEmpty());
+ assertTrue(record.getCurrentOperatorIdentity().isEmpty());
+ }
+
+ /**
+ * 场景:发起人办理后流转到审批人 → 新记录的当前审批人身份来自审批人自身,发起/提交身份保持不变。
+ */
+ @Test
+ void should_store_approver_identity_after_pass() {
+ User creator = User.withIdentity(1, "user1", "department1");
+ User boss = User.withIdentity(2, "boss", "department2");
+ MyFlowServiceFactory factory = newFactory(creator, boss);
+
+ Workflow workflow = buildLeaveWorkflow(factory, creator);
+ long recordId = createAtStartNode(factory, workflow, creator);
+
+ FlowActionRequest actionRequest = new FlowActionRequest();
+ actionRequest.setFormData(formData());
+ actionRequest.setRecordId(recordId);
+ actionRequest.setAdvice(new FlowAdviceBody(startActions(workflow).get(0).id(), "同意", creator.getUserId()));
+ factory.flowService.action(actionRequest);
+
+ List bossTodos = factory.flowRecordRepository.findTodoByOperator(boss.getUserId());
+ assertEquals(1, bossTodos.size());
+ FlowRecord bossRecord = bossTodos.get(0);
+ assertEquals("department2", bossRecord.getCurrentOperatorIdentity());
+ assertEquals("department1", bossRecord.getCreateOperatorIdentity());
+ assertEquals("department1", bossRecord.getSubmitOperatorIdentity());
+
+ List bossTodoRecords = factory.flowTodoRecordRepository.findByOperatorId(boss.getUserId());
+ assertEquals(1, bossTodoRecords.size());
+ assertEquals("department2", bossTodoRecords.get(0).getCurrentOperatorIdentity());
+ }
+}
diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/user/User.java b/flow-engine-framework/src/test/java/com/codingapi/flow/user/User.java
index 19ce3952..727a43f1 100644
--- a/flow-engine-framework/src/test/java/com/codingapi/flow/user/User.java
+++ b/flow-engine-framework/src/test/java/com/codingapi/flow/user/User.java
@@ -3,30 +3,48 @@
import com.codingapi.flow.operator.IFlowOperator;
import com.codingapi.flow.script.request.GroovyScriptRequest;
+import java.util.Map;
+
public class User implements IFlowOperator {
private final long userId;
private final String name;
private final boolean manager;
private final User forwardOperator;
+ /**
+ * 身份标识(issue #288),为空表示默认身份
+ */
+ private final String identity;
public User(long userId, String name) {
- this(userId, name, false, null);
+ this(userId, name, false, null, "");
}
public User(long userId, String name, boolean manager) {
- this(userId, name, manager, null);
+ this(userId, name, manager, null, "");
}
public User(long userId, String name, User forwardOperator) {
- this(userId, name, false, forwardOperator);
+ this(userId, name, false, forwardOperator, "");
}
public User(long userId, String name, boolean manager, User forwardOperator) {
+ this(userId, name, manager, forwardOperator, "");
+ }
+
+ protected User(long userId, String name, boolean manager, User forwardOperator, String identity) {
this.userId = userId;
this.name = name;
this.manager = manager;
this.forwardOperator = forwardOperator;
+ this.identity = identity;
+ }
+
+ /**
+ * 创建带身份标识的用户(测试多身份场景)
+ */
+ public static User withIdentity(long userId, String name, String identity) {
+ return new User(userId, name, false, null, identity);
}
@Override
@@ -44,6 +62,16 @@ public boolean isFlowManager() {
return manager;
}
+ @Override
+ public String getIdentity() {
+ return identity;
+ }
+
+ @Override
+ public Map getIdentityAttributes() {
+ return identity == null || identity.isEmpty() ? Map.of() : Map.of("identityName", identity);
+ }
+
@Override
public IFlowOperator forwardOperator(GroovyScriptRequest request) {
return forwardOperator;
diff --git a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java
index 9e34a9cf..07448756 100644
--- a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java
+++ b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowRecordConvertor.java
@@ -2,6 +2,7 @@
import com.codingapi.flow.infra.entity.FlowRecordEntity;
import com.codingapi.flow.infra.entity.convert.MapConvertor;
+import com.codingapi.flow.operator.OperatorIdentity;
import com.codingapi.flow.record.FlowRecord;
import com.codingapi.flow.strategy.node.RecordMergeStrategy;
@@ -61,7 +62,13 @@ public static FlowRecord convert(FlowRecordEntity entity) {
entity.getDelegateId(),
entity.getParallelId(),
entity.getParallelBranchNodeId(),
- entity.getParallelBranchTotal());
+ entity.getParallelBranchTotal(),
+ // issue #288:历史数据 identity 列为 null,读取时统一归一化为默认身份
+ OperatorIdentity.normalize(entity.getCreateOperatorIdentity()),
+ OperatorIdentity.normalize(entity.getSubmitOperatorIdentity()),
+ OperatorIdentity.normalize(entity.getCurrentOperatorIdentity()),
+ OperatorIdentity.normalize(entity.getForwardOperatorIdentity()),
+ OperatorIdentity.normalize(entity.getInterferedOperatorIdentity()));
}
public static FlowRecordEntity convert(FlowRecord record) {
@@ -120,6 +127,11 @@ public static FlowRecordEntity convert(FlowRecord record) {
entity.setParallelId(record.getParallelId());
entity.setParallelBranchNodeId(record.getParallelBranchNodeId());
entity.setParallelBranchTotal(record.getParallelBranchTotal());
+ entity.setCreateOperatorIdentity(OperatorIdentity.normalize(record.getCreateOperatorIdentity()));
+ entity.setSubmitOperatorIdentity(OperatorIdentity.normalize(record.getSubmitOperatorIdentity()));
+ entity.setCurrentOperatorIdentity(OperatorIdentity.normalize(record.getCurrentOperatorIdentity()));
+ entity.setForwardOperatorIdentity(OperatorIdentity.normalize(record.getForwardOperatorIdentity()));
+ entity.setInterferedOperatorIdentity(OperatorIdentity.normalize(record.getInterferedOperatorIdentity()));
return entity;
}
diff --git a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowTodoRecordConvertor.java b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowTodoRecordConvertor.java
index 7de8f9c9..8339d93f 100644
--- a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowTodoRecordConvertor.java
+++ b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/convert/FlowTodoRecordConvertor.java
@@ -1,6 +1,7 @@
package com.codingapi.flow.infra.convert;
import com.codingapi.flow.infra.entity.FlowTodoRecordEntity;
+import com.codingapi.flow.operator.OperatorIdentity;
import com.codingapi.flow.record.FlowTodoRecord;
public class FlowTodoRecordConvertor {
@@ -31,7 +32,11 @@ public static FlowTodoRecord convert(FlowTodoRecordEntity entity){
entity.getMargeCount(),
entity.getMergeable(),
entity.getRecordId(),
- entity.getTimeoutTime()
+ entity.getTimeoutTime(),
+ // issue #288:历史数据 identity 列为 null,读取时统一归一化为默认身份
+ OperatorIdentity.normalize(entity.getCurrentOperatorIdentity()),
+ OperatorIdentity.normalize(entity.getSubmitOperatorIdentity()),
+ OperatorIdentity.normalize(entity.getCreateOperatorIdentity())
);
}
@@ -66,6 +71,9 @@ public static FlowTodoRecordEntity convert(FlowTodoRecord record){
entity.setMergeable(record.isMergeable());
entity.setRecordId(record.getRecordId());
entity.setTimeoutTime(record.getTimeoutTime());
+ entity.setCurrentOperatorIdentity(OperatorIdentity.normalize(record.getCurrentOperatorIdentity()));
+ entity.setSubmitOperatorIdentity(OperatorIdentity.normalize(record.getSubmitOperatorIdentity()));
+ entity.setCreateOperatorIdentity(OperatorIdentity.normalize(record.getCreateOperatorIdentity()));
return entity;
}
}
diff --git a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java
index 9e8c8764..498f54cd 100644
--- a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java
+++ b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowRecordEntity.java
@@ -100,6 +100,11 @@ public class FlowRecordEntity {
*/
private String currentOperatorName;
+ /**
+ * 当前审批人身份标识(issue #288),空值等价默认身份
+ */
+ private String currentOperatorIdentity;
+
/**
* 提交审批人Id
*/
@@ -110,6 +115,11 @@ public class FlowRecordEntity {
*/
private String submitOperatorName;
+ /**
+ * 提交审批人身份标识(issue #288),空值等价默认身份
+ */
+ private String submitOperatorIdentity;
+
/**
* 代替的审批人
*/
@@ -120,6 +130,11 @@ public class FlowRecordEntity {
*/
private String forwardOperatorName;
+ /**
+ * 代替的审批人身份标识(issue #288),空值等价默认身份
+ */
+ private String forwardOperatorIdentity;
+
/**
* 有那个节点退回的
*/
@@ -180,6 +195,11 @@ public class FlowRecordEntity {
*/
private String createOperatorName;
+ /**
+ * 发起者身份标识(issue #288),空值等价默认身份
+ */
+ private String createOperatorIdentity;
+
/**
* 异常信息
*/
@@ -208,6 +228,11 @@ public class FlowRecordEntity {
*/
private String interferedOperatorName;
+ /**
+ * 被干预的用户身份标识(issue #288),空值等价默认身份
+ */
+ private String interferedOperatorIdentity;
+
/**
* 委托记录id
*/
diff --git a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowTodoRecordEntity.java b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowTodoRecordEntity.java
index 69387f30..e29cc601 100644
--- a/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowTodoRecordEntity.java
+++ b/flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/entity/FlowTodoRecordEntity.java
@@ -68,6 +68,11 @@ public class FlowTodoRecordEntity {
*/
private String currentOperatorName;
+ /**
+ * 当前审批人身份标识(issue #288),空值等价默认身份
+ */
+ private String currentOperatorIdentity;
+
/**
* 提交审批人Id
*/
@@ -78,6 +83,11 @@ public class FlowTodoRecordEntity {
*/
private String submitOperatorName;
+ /**
+ * 提交审批人身份标识(issue #288),空值等价默认身份
+ */
+ private String submitOperatorIdentity;
+
/**
* 创建时间
*/
@@ -92,6 +102,11 @@ public class FlowTodoRecordEntity {
*/
private String createOperatorName;
+ /**
+ * 发起者身份标识(issue #288),空值等价默认身份
+ */
+ private String createOperatorIdentity;
+
/**
* 合并记录id,当存在合并记录数据时待办记录数量不会增加,内容会更新至最新的待办数据信息。
* {@link FlowRecord#getTodoKey()}
diff --git a/flow-engine-starter-infra/src/test/java/com/codingapi/flow/infra/convert/FlowRecordIdentityConvertorTest.java b/flow-engine-starter-infra/src/test/java/com/codingapi/flow/infra/convert/FlowRecordIdentityConvertorTest.java
new file mode 100644
index 00000000..c229f53f
--- /dev/null
+++ b/flow-engine-starter-infra/src/test/java/com/codingapi/flow/infra/convert/FlowRecordIdentityConvertorTest.java
@@ -0,0 +1,152 @@
+package com.codingapi.flow.infra.convert;
+
+import com.codingapi.flow.infra.entity.FlowRecordEntity;
+import com.codingapi.flow.infra.entity.FlowTodoRecordEntity;
+import com.codingapi.flow.record.FlowRecord;
+import com.codingapi.flow.record.FlowTodoRecord;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertAll;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * 流程记录身份字段的持久化映射测试(issue #288 P1)。
+ *
+ * 覆盖两点:历史数据(identity 列为 null)读取为默认身份;身份在实体与领域对象之间不丢失。
+ */
+class FlowRecordIdentityConvertorTest {
+
+ /**
+ * 构造字段完整的流程记录实体,避免转换时的拆箱 NPE 干扰身份断言。
+ */
+ private FlowRecordEntity newRecordEntity() {
+ FlowRecordEntity entity = new FlowRecordEntity();
+ entity.setId(1L);
+ entity.setWorkRuntimeId(1L);
+ entity.setWorkTitle("请假流程");
+ entity.setWorkCode("leave");
+ entity.setNodeId("start");
+ entity.setNodeType("START");
+ entity.setNodeName("开始");
+ entity.setTitle("请假流程");
+ entity.setFromId(0L);
+ entity.setParentId(0L);
+ entity.setReadTime(0L);
+ entity.setCurrentOperatorId(1L);
+ entity.setSubmitOperatorId(1L);
+ entity.setForwardOperatorId(0L);
+ entity.setNodeOrder(0);
+ entity.setHidden(false);
+ entity.setRevoked(false);
+ entity.setNotify(false);
+ entity.setRecordState(0);
+ entity.setFlowState(0);
+ entity.setUpdateTime(0L);
+ entity.setCreateTime(0L);
+ entity.setFinishTime(0L);
+ entity.setReadable(false);
+ entity.setCreateOperatorId(1L);
+ entity.setTimeoutTime(0L);
+ entity.setMergeable(false);
+ entity.setInterferedOperatorId(0L);
+ entity.setDelegateId(0L);
+ entity.setParallelBranchTotal(0);
+ return entity;
+ }
+
+ /**
+ * 构造字段完整的待办记录实体。
+ */
+ private FlowTodoRecordEntity newTodoEntity() {
+ FlowTodoRecordEntity entity = new FlowTodoRecordEntity();
+ entity.setId(1L);
+ entity.setWorkflowRuntimeId(1L);
+ entity.setReadTime(0L);
+ entity.setCurrentOperatorId(1L);
+ entity.setSubmitOperatorId(1L);
+ entity.setCreateTime(0L);
+ entity.setCreateOperatorId(1L);
+ entity.setMargeCount(0);
+ entity.setMergeable(false);
+ entity.setRecordId(1L);
+ entity.setTimeoutTime(0L);
+ return entity;
+ }
+
+ /**
+ * 测试目标:历史数据没有 identity 列值时,读取后统一为默认身份(空串)。
+ * 前置条件:实体 identity 字段全部为 null(升级前的存量数据)。
+ * 执行步骤:实体 → 领域对象。
+ * 期望断言:五组操作者身份均为空串,不出现 null。
+ */
+ @Test
+ void should_read_null_identity_as_default_for_legacy_record() {
+ FlowRecordEntity entity = newRecordEntity();
+
+ FlowRecord record = FlowRecordConvertor.convert(entity);
+
+ assertAll(
+ () -> assertEquals("", record.getCreateOperatorIdentity()),
+ () -> assertEquals("", record.getSubmitOperatorIdentity()),
+ () -> assertEquals("", record.getCurrentOperatorIdentity()),
+ () -> assertEquals("", record.getForwardOperatorIdentity()),
+ () -> assertEquals("", record.getInterferedOperatorIdentity())
+ );
+ }
+
+ /**
+ * 测试目标:身份标识在实体与领域对象之间双向转换不丢失。
+ * 前置条件:实体带有五组不同的身份标识。
+ * 执行步骤:实体 → 领域对象 → 实体。
+ * 期望断言:两组身份值完全一致。
+ */
+ @Test
+ void should_round_trip_identity() {
+ FlowRecordEntity entity = newRecordEntity();
+ entity.setCreateOperatorIdentity("department1");
+ entity.setSubmitOperatorIdentity("department1");
+ entity.setCurrentOperatorIdentity("department2");
+ entity.setForwardOperatorIdentity("department3");
+ entity.setInterferedOperatorIdentity("department4");
+
+ FlowRecord record = FlowRecordConvertor.convert(entity);
+ FlowRecordEntity roundTrip = FlowRecordConvertor.convert(record);
+
+ assertAll(
+ () -> assertEquals("department1", record.getCreateOperatorIdentity()),
+ () -> assertEquals("department2", roundTrip.getCurrentOperatorIdentity()),
+ () -> assertEquals("department3", roundTrip.getForwardOperatorIdentity()),
+ () -> assertEquals("department4", roundTrip.getInterferedOperatorIdentity())
+ );
+ }
+
+ /**
+ * 测试目标:待办记录的历史数据同样读取为默认身份,且身份可往返。
+ * 前置条件:待办实体 identity 为 null 与有值两种情况。
+ * 执行步骤:实体 → 领域对象 → 实体。
+ * 期望断言:null 归一化为空串,有值身份保持。
+ */
+ @Test
+ void should_map_todo_record_identity() {
+ FlowTodoRecordEntity legacyEntity = newTodoEntity();
+ FlowTodoRecord legacyTodo = FlowTodoRecordConvertor.convert(legacyEntity);
+ assertEquals("", legacyTodo.getCurrentOperatorIdentity());
+ assertEquals("", legacyTodo.getSubmitOperatorIdentity());
+ assertEquals("", legacyTodo.getCreateOperatorIdentity());
+
+ FlowTodoRecordEntity entity = newTodoEntity();
+ entity.setId(2L);
+ entity.setCurrentOperatorIdentity("department2");
+ entity.setSubmitOperatorIdentity("department1");
+ entity.setCreateOperatorIdentity("department1");
+
+ FlowTodoRecord todo = FlowTodoRecordConvertor.convert(entity);
+ FlowTodoRecordEntity roundTrip = FlowTodoRecordConvertor.convert(todo);
+
+ assertAll(
+ () -> assertEquals("department2", todo.getCurrentOperatorIdentity()),
+ () -> assertEquals("department1", roundTrip.getSubmitOperatorIdentity()),
+ () -> assertEquals("department1", roundTrip.getCreateOperatorIdentity())
+ );
+ }
+}
From 6e71220c798fd5a951739452c9953a450e74670c Mon Sep 17 00:00:00 2001
From: lorne <1991wangliang@gmail.com>
Date: Mon, 23 Nov 2026 21:30:38 +0800
Subject: [PATCH 2/5] =?UTF-8?q?chore(release):=20=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E5=8F=B7=E5=8D=87=E7=BA=A7=E8=87=B3=200.3.0-SNAPSHOT=EF=BC=88?=
=?UTF-8?q?=E4=B8=AD=E7=89=88=E6=9C=AC=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
issue #288 引入身份标识并将在 P2/P3 改变待办查询与审批判定语义,
属于行为变更,按中版本升级:0.2.x → 0.3.0。
- pom.xml:revision 默认值 0.1.0-SNAPSHOT(陈旧)→ 0.3.0-SNAPSHOT
- AGENTS.md:同步当前版本描述
- 前端 flow-frontend 将在前端改造完成时一并升级至 0.3.0 并锁定版本号
Closes #288
---
AGENTS.md | 2 +-
pom.xml | 5 +++--
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/AGENTS.md b/AGENTS.md
index 2f249ec5..79843b7a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -4,7 +4,7 @@
## 项目概述
-Flow Engine 是一个企业级工作流引擎,基于 Java 17 和 Spring Boot 3.5.9 构建(当前版本 0.1.0-SNAPSHOT)。
+Flow Engine 是一个企业级工作流引擎,基于 Java 17 和 Spring Boot 3.5.9 构建(当前版本 0.3.0-SNAPSHOT)。
### 核心模块
diff --git a/pom.xml b/pom.xml
index 03803276..3cc5c7b7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,8 +20,9 @@
-
- 0.1.0-SNAPSHOT
+
+
+ 0.3.0-SNAPSHOT
yyyy-MM-dd HH:mm:ss
From 07425ab2d1de04870f2eb763cb4cc701a992dd46 Mon Sep 17 00:00:00 2001
From: lorne <1991wangliang@gmail.com>
Date: Mon, 23 Nov 2026 21:51:19 +0800
Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=E6=93=8D=E4=BD=9C=E8=80=85?=
=?UTF-8?q?=E8=BA=AB=E4=BB=BD=E6=A0=87=E8=AF=86=E7=9A=84=E8=AF=B7=E6=B1=82?=
=?UTF-8?q?=E9=80=8F=E4=BC=A0=E3=80=81=E5=BE=85=E5=8A=9E=E9=9A=94=E7=A6=BB?=
=?UTF-8?q?=E4=B8=8E=E5=AE=A1=E6=89=B9=E5=88=A4=E5=AE=9A=EF=BC=88issue=20#?=
=?UTF-8?q?288=20P2/P3=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
P2 透传与隔离:
- FlowOperatorGateway 新增 default get(id, identity),下游存量实现零改动;IRepositoryHolder 同步新增 getOperatorById(id, identity)
- FlowOperatorLocalThreadCache 缓存键由 userId 升级为 (userId, identity),同一人两个身份不再串味
- 请求体统一支持 operatorIdentity:create/action/revoke/delete/urge/detail/processNodes/reissue/forceSync/subProcessReset
- REST 层新增 operatorIdentity 参数(与 operatorId 对称),未传时取当前登录操作者身份
- getTodoKey 采用兼容追加策略:默认身份保持历史格式,非空身份追加 #identity,升级中的存量待办合并关系不断裂
- 待办/已办/抄送查询按 (operatorId, identity) 过滤,默认身份用 coalesce(identity,'') 覆盖历史 NULL 行
- FlowRecordContent 列表响应补充发起/提交/当前三组身份,供前端列表展示
P3 判定升级为 (userId, identity):
- WorkflowStrategyManager.verifyOperator 增加身份比较(审批参与人校验)
- 撤销/删除/催办/子流程重置的记录归属校验加入身份
- OperatorManager(加签/减签/转交/委托)按人员+身份匹配
- 加签待办队列查重、减签目标解析按 (userId, identity) 去重
- 提交人==审批人的自动通过判定、流程干预人识别加入身份
- 详情/流程树/延迟触发/子流程会话/转交/delegate 等按记录身份回查操作者
文档与升级:
- 新增 docs/Integration/operator-identity.md(模型、SPI、请求参数、脚本用法、兼容性与限制)
- 新增 docs/Integration/sql/0.3.0-operator-identity.sql(加列 + 索引建议 + 达梦说明 + 自检 SQL)
- 示例工程 FlowOperatorGatewayImpl/User 提供身份参考实现(返回带身份的副本,避免污染持久化上下文)
验证:./mvnw clean install 全量通过,453 个用例 0 失败(framework 430 / infra 6 / api 2 / starter 8 / example 7)。
新增用例 13 个:待办合并键身份维度 4、缓存身份隔离 4、审批判定身份 3、待办查询身份隔离(含历史 NULL)2。
Closes #288
---
docs/Integration/README.md | 1 +
docs/Integration/operator-identity.md | 188 +++++++++++++++++
.../sql/0.3.0-operator-identity.sql | 75 +++++++
docs/Integration/user-integration.md | 8 +
.../com/codingapi/example/entity/User.java | 43 ++++
.../gateway/impl/FlowOperatorGatewayImpl.java | 18 ++
.../example/FlowTodoIdentityQueryTest.java | 185 +++++++++++++++++
.../flow/action/actions/AddAuditAction.java | 20 +-
.../flow/action/actions/PassAction.java | 6 +-
.../action/actions/ReduceAuditAction.java | 10 +-
.../cache/FlowOperatorLocalThreadCache.java | 74 +++++--
.../flow/context/GatewayContext.java | 24 ++-
.../flow/context/RepositoryHolderContext.java | 10 +
.../flow/gateway/FlowOperatorGateway.java | 27 +++
.../flow/manager/OperatorManager.java | 36 +++-
.../flow/manager/WorkflowStrategyManager.java | 24 ++-
.../flow/mock/MockRepositoryHolder.java | 10 +
.../codingapi/flow/node/BaseAuditNode.java | 7 +-
.../codingapi/flow/node/nodes/StartNode.java | 5 +-
.../flow/pojo/body/FlowAdviceBody.java | 10 +
.../flow/pojo/request/FlowActionRequest.java | 11 +
.../flow/pojo/request/FlowCreateRequest.java | 7 +
.../flow/pojo/request/FlowDeleteRequest.java | 13 ++
.../flow/pojo/request/FlowDetailRequest.java | 17 +-
.../pojo/request/FlowForceSyncRequest.java | 5 +
.../pojo/request/FlowProcessNodeRequest.java | 19 +-
.../flow/pojo/request/FlowReissueRequest.java | 5 +
.../flow/pojo/request/FlowRevokeRequest.java | 13 ++
.../request/FlowSubProcessResetRequest.java | 5 +
.../flow/pojo/request/FlowUrgeRequest.java | 13 ++
.../flow/pojo/response/FlowRecordContent.java | 22 ++
.../flow/query/FlowRecordQueryService.java | 29 +++
.../com/codingapi/flow/record/FlowRecord.java | 44 +++-
.../flow/service/impl/FlowActionService.java | 11 +-
.../flow/service/impl/FlowCreateService.java | 4 +-
.../service/impl/FlowDelayTriggerService.java | 9 +-
.../flow/service/impl/FlowDeleteService.java | 6 +-
.../flow/service/impl/FlowDetailService.java | 8 +-
.../service/impl/FlowForceSyncService.java | 2 +-
.../service/impl/FlowProcessNodeService.java | 57 +++--
.../flow/service/impl/FlowReissueService.java | 6 +-
.../flow/service/impl/FlowRevokeService.java | 5 +-
.../impl/FlowSubProcessResetService.java | 6 +-
.../flow/service/impl/FlowUrgeService.java | 6 +-
.../impl/SubProcessSessionFactory.java | 9 +-
.../flow/session/IRepositoryHolder.java | 17 +-
.../FlowOperatorLocalThreadCacheTest.java | 85 ++++++++
.../flow/gateway/impl/UserGateway.java | 14 ++
.../flow/service/FlowTodoKeyIdentityTest.java | 196 ++++++++++++++++++
.../OperatorIdentityPermissionTest.java | 182 ++++++++++++++++
.../java/com/codingapi/flow/user/User.java | 8 +
.../api/controller/FlowRecordController.java | 51 +++--
.../convert/FlowRecordContentConvertor.java | 7 +
.../infra/jpa/FlowRecordEntityRepository.java | 34 +++
.../jpa/FlowTodoRecordEntityRepository.java | 19 ++
.../controller/FlowRecordQueryController.java | 27 ++-
.../service/FlowRecordQueryServiceImpl.java | 22 ++
57 files changed, 1661 insertions(+), 114 deletions(-)
create mode 100644 docs/Integration/operator-identity.md
create mode 100644 docs/Integration/sql/0.3.0-operator-identity.sql
create mode 100644 flow-engine-example/src/test/java/com/codingapi/example/FlowTodoIdentityQueryTest.java
create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/cache/FlowOperatorLocalThreadCacheTest.java
create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/service/FlowTodoKeyIdentityTest.java
create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/service/OperatorIdentityPermissionTest.java
diff --git a/docs/Integration/README.md b/docs/Integration/README.md
index 7d770cc8..e1be4345 100644
--- a/docs/Integration/README.md
+++ b/docs/Integration/README.md
@@ -31,6 +31,7 @@
|------|------|
| [快速开始](./quick-start.md) | 最小集成步骤:引入依赖 → 提供 Bean → 编写流程 → 发起流程 |
| [流程用户体系](./user-integration.md) | `FlowOperatorGateway` / `IFlowOperator` / `GatewayContext` / 线程缓存 / 当前登录人 |
+| [多身份参与](./operator-identity.md) | 同一人员多身份:身份 SPI、请求声明、待办隔离、判定规则、升级 DDL |
| [流程默认脚本](./script-integration.md) | 12 种脚本类型、`GroovyScriptRequest` 完整 API、`$bind`、默认脚本替换、脚本生命周期 |
| [流程事件机制](./event-integration.md) | 9 种事件、`EventPusher`、`IHandler` 订阅、异步分发管道、事务变体、推送时机 |
| [仓储抽象与持久化](./repository-integration.md) | `IRepositoryHolder`、12 个仓储接口(含 11 个 JPA 实现 + 需自行实现的 `NodeViewJavaScriptRepository`)、脚本仓储、锁机制 |
diff --git a/docs/Integration/operator-identity.md b/docs/Integration/operator-identity.md
new file mode 100644
index 00000000..9f5ee36d
--- /dev/null
+++ b/docs/Integration/operator-identity.md
@@ -0,0 +1,188 @@
+# 多身份参与(Operator Identity)
+
+> 面向业务项目的集成说明,对应 issue #288。引擎只负责**透传、存储与比较**身份标识,不解释其业务语义,也不内建组织模型。
+
+## 1. 解决什么问题
+
+一个用户可能同时隶属多个部门(或身兼多个岗位),在不同事项上以不同身份参与流程:
+
+| 差异类型 | 例子 |
+|----------|------|
+| 节点 / 分支差异 | user1 在 department1 发起需要「组长审核」,在 department2 发起则直接「部门经理审核」 |
+| 人员差异 | department1 的经理是 boss1,department2 的经理是 boss2 |
+| 待办差异 | user1 在 department1 / department2 各有一条待办,不能合并成一条 |
+
+0.2.x 的引擎只认 `userId`:记录、待办、权限判定全部按 userId 比较,上述场景无法表达。
+
+## 2. 模型
+
+身份标识是一个**不透明字符串**,由下游定义取值,框架只做四件事:
+
+1. 从 `IFlowOperator#getIdentity()` 读取;
+2. 落库到流程记录与待办;
+3. 在判定时按 `(userId, identity)` 比较;
+4. 透传给 Groovy 脚本与 REST 响应。
+
+约定:`null` 与空串等价,均表示**默认身份**(`OperatorIdentity.DEFAULT`)。未启用多身份的业务、以及 0.3.0 之前的历史数据,全部落在默认身份上,行为与历史完全一致。
+
+```java
+public interface IFlowOperator extends IUser {
+
+ long getUserId();
+
+ String getName();
+
+ /**
+ * 身份标识,空串表示默认身份。框架不解释语义。
+ */
+ default String getIdentity() {
+ return OperatorIdentity.DEFAULT;
+ }
+
+ /**
+ * 身份的附加属性(部门名、岗位名等),只透传给脚本与前端,不参与引擎判定,不落库。
+ */
+ default Map getIdentityAttributes() {
+ return Map.of();
+ }
+}
+```
+
+两个方法都是 `default`,**下游存量实现类无需改动即可编译**。
+
+## 3. 下游需要做什么
+
+### 3.1 实现身份(可选)
+
+```java
+public class User implements IFlowOperator {
+
+ private String identity; // 运行期选择,通常不落用户主表
+
+ @Override
+ public String getIdentity() {
+ return identity;
+ }
+}
+```
+
+### 3.2 让网关按身份返回操作者(关键)
+
+```java
+@Override
+public IFlowOperator get(long id, String identity) {
+ User user = userRepository.getUserById(id);
+ if (user == null || OperatorIdentity.isDefault(identity)) {
+ return user;
+ }
+ // 必须复制:直接改写实体会污染持久化上下文 / 线程缓存,导致同一 id 的两个身份互相覆盖
+ return user.withIdentity(identity);
+}
+```
+
+> `get(long, String)` 在接口上是 `default` 方法,默认退化为 `get(id)`(忽略身份)。**不实现它时,请求里声明的身份不会被写入记录** —— 引擎的身份来源是操作者对象本身,而不是请求参数。多身份场景必须实现此方法。
+
+### 3.3 请求声明身份
+
+REST 接口支持 `operatorIdentity` 参数(与 `operatorId` 对称):未传时取当前登录操作者的身份。
+
+```bash
+# 以 department1 的身份发起
+curl -X POST 'http://host/api/cmd/record/create?operatorIdentity=department1' \
+ -H 'Content-Type: application/json' -d '{...}'
+
+# 以 department1 的身份查看待办
+curl 'http://host/api/query/record/todo?operatorId=1&operatorIdentity=department1'
+```
+
+编程式 API 中对应各请求对象的 `operatorIdentity` 字段:
+
+| 请求对象 | 字段 |
+|----------|------|
+| `FlowCreateRequest` | `operatorIdentity` |
+| `FlowAdviceBody`(`FlowActionRequest.advice`) | `operatorIdentity` |
+| `FlowRevokeRequest` / `FlowDeleteRequest` / `FlowUrgeRequest` | `operatorIdentity` |
+| `FlowDetailRequest` / `FlowProcessNodeRequest` | `operatorIdentity` |
+| `FlowReissueRequest` / `FlowForceSyncRequest` / `FlowSubProcessResetRequest` | `operatorIdentity` |
+
+### 3.4 在脚本中使用身份
+
+`GroovyScriptRequest` 新增三个函数:
+
+| 脚本函数 | 含义 |
+|----------|------|
+| `getCurrentOperatorIdentity()` | 当前操作者身份 |
+| `getCreatedOperatorIdentity()` | 流程发起人身份 |
+| `getSubmitOperatorIdentity()` | 流程提交人身份 |
+
+节点/分支差异(条件脚本):
+
+```groovy
+def run(request) {
+ // department1 需要组长审核,其他身份直接进入部门经理审批
+ return request.getCurrentOperatorIdentity() == 'department1'
+}
+```
+
+人员差异(审批人脚本):
+
+```groovy
+def run(request) {
+ def identity = request.getCreatedOperatorIdentity()
+ if (identity == 'department1') return [boss1Id]
+ if (identity == 'department2') return [boss2Id]
+ return [defaultBossId]
+}
+```
+
+## 4. 引擎侧行为
+
+### 4.1 记录与待办落库
+
+| 表 | 新增列 |
+|----|--------|
+| `t_flow_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity`、`forward_operator_identity`、`interfered_operator_identity` |
+| `t_flow_todo_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity` |
+
+只存身份标识本身,`getIdentityAttributes()` 的附加属性**不落库**(避免 5 个 JSON 快照列,也避免身份名称被冻结在历史数据里)。展示名称仍取操作者姓名。
+
+升级脚本见 [sql/0.3.0-operator-identity.sql](./sql/0.3.0-operator-identity.sql)。
+
+### 4.2 待办合并键(兼容追加)
+
+`FlowRecord#getTodoKey()` 的格式:
+
+| 身份 | 合并键 |
+|------|--------|
+| 默认身份 | `{operatorId}-{workRuntimeId}-{nodeId}`(与 0.2.x 完全一致) |
+| 非空身份 | `{operatorId}-{workRuntimeId}-{nodeId}#{identity}` |
+
+默认身份保持历史格式是刻意的:升级瞬间仍在运行的流程,其存量待办行是旧格式键,若统一改格式会导致新流转算出的键查不到旧行 → 重复生成待办、合并关系断裂。
+
+### 4.3 查询隔离
+
+待办 / 已办 / 抄送查询按 `(currentOperatorId, currentOperatorIdentity)` 过滤,默认身份的条件写成 `coalesce(identity, '') = ''`,保证历史 NULL 行仍能被查到(否则升级后存量待办会从列表中消失)。
+
+### 4.4 判定升级为 `(userId, identity)`
+
+以下判定统一由「只比 userId」升级为「比 userId + identity」,身份为空时与历史等价:
+
+- 审批动作的参与人校验(`WorkflowStrategyManager#verifyOperator`)
+- 撤销、删除、催办、子流程重置的记录归属校验
+- 加签 / 减签 / 转交 / 委托的人员匹配(`OperatorManager`)
+- 提交人 == 审批人的自动通过判定
+- 流程干预人识别
+- 操作者线程缓存与网关缓存的键
+
+## 5. 兼容性与限制
+
+| 项 | 说明 |
+|----|------|
+| 存量数据 | 新列为 NULL,按默认身份读取,**无需回填** |
+| 回滚 | 仅新增 nullable 列,旧版本不映射新列,可直接回滚 |
+| 工作流定义 | 与身份无关,导入导出不受影响 |
+| 身份长度 | 建议 ≤ 64 字符(复合 key 用 `:` 拼接) |
+| 待办索引 | 实体未声明 `@Index`,大表请按 DDL 脚本手工加索引 |
+| 不做的事 | 引擎不建组织模型、不做身份授权校验(「user1 能否以 department1 身份操作」由业务侧保证)、不冻结身份名称 |
+
+> 身份是一个**不透明字符串**,不要尝试在引擎里比较它的业务含义;需要「部门名称」等展示信息,请用 `getIdentityAttributes()` 或按身份回查组织数据。
diff --git a/docs/Integration/sql/0.3.0-operator-identity.sql b/docs/Integration/sql/0.3.0-operator-identity.sql
new file mode 100644
index 00000000..afa11162
--- /dev/null
+++ b/docs/Integration/sql/0.3.0-operator-identity.sql
@@ -0,0 +1,75 @@
+-- =============================================================================
+-- flow-engine 0.3.0 升级脚本:操作者身份标识(issue #288)
+-- =============================================================================
+-- 背景:
+-- 同一人员可同时隶属多个部门/岗位,以不同「身份」参与同一个流程。
+-- 0.3.0 起框架在记录与待办上保存身份标识(不解释语义,由下游定义取值)。
+--
+-- 兼容性:
+-- 1. 仅新增 nullable 列,不修改、不删除任何既有列,旧版本程序不映射新列,可安全回滚。
+-- 2. 历史数据的新列为 NULL,引擎按「null 与空串等价 = 默认身份」读取,无需数据回填。
+-- 3. 待办合并键在默认身份下保持历史格式,仅非空身份追加 "#identity" 后缀,
+-- 升级瞬间仍在运行的流程不会出现待办重复或合并关系断裂。
+--
+-- 使用方式:
+-- - 使用 ddl-auto=update 的环境会自动加列,本脚本用于「手工管理表结构」的生产环境;
+-- - 索引请按实际数据量与慢查询情况选择性执行(见文末)。
+-- =============================================================================
+
+
+-- -----------------------------------------------------------------------------
+-- 1. 流程记录表:发起 / 提交 / 当前 / 转交 / 干预 五组操作者的身份
+-- -----------------------------------------------------------------------------
+ALTER TABLE t_flow_record ADD COLUMN create_operator_identity VARCHAR(64) NULL;
+ALTER TABLE t_flow_record ADD COLUMN submit_operator_identity VARCHAR(64) NULL;
+ALTER TABLE t_flow_record ADD COLUMN current_operator_identity VARCHAR(64) NULL;
+ALTER TABLE t_flow_record ADD COLUMN forward_operator_identity VARCHAR(64) NULL;
+ALTER TABLE t_flow_record ADD COLUMN interfered_operator_identity VARCHAR(64) NULL;
+
+
+-- -----------------------------------------------------------------------------
+-- 2. 待办表:发起 / 提交 / 当前 三组操作者的身份
+-- -----------------------------------------------------------------------------
+ALTER TABLE t_flow_todo_record ADD COLUMN create_operator_identity VARCHAR(64) NULL;
+ALTER TABLE t_flow_todo_record ADD COLUMN submit_operator_identity VARCHAR(64) NULL;
+ALTER TABLE t_flow_todo_record ADD COLUMN current_operator_identity VARCHAR(64) NULL;
+
+
+-- -----------------------------------------------------------------------------
+-- 3. 索引(可选,按需执行)
+-- -----------------------------------------------------------------------------
+-- 3.1 待办列表查询:where current_operator_id = ? and coalesce(current_operator_identity,'') = ?
+-- 数据量超过 100 万的待办表建议加此联合索引;小表可暂不加。
+-- 注意:coalesce 会导致该列无法命中普通索引,因此索引列顺序把 operator_id 放前面,
+-- 由 operator_id 做范围收敛,身份列做回表过滤。
+-- CREATE INDEX idx_todo_operator_identity
+-- ON t_flow_todo_record (current_operator_id, current_operator_identity);
+
+-- 3.2 已办 / 抄送列表查询(同上,命中 t_flow_record)
+-- CREATE INDEX idx_record_operator_identity
+-- ON t_flow_record (current_operator_id, current_operator_identity, record_state);
+
+
+-- -----------------------------------------------------------------------------
+-- 4. 达梦(DM)环境补充说明
+-- -----------------------------------------------------------------------------
+-- 达梦上 Hibernate 的 ddl-auto=update 对【加列】是可靠的(仅新增 nullable 列,不涉及序列),
+-- 但生产环境仍建议按本脚本手工执行。达梦语法与上文一致,字段类型可用 VARCHAR(64);
+-- 若字符集配置为 UTF-8,VARCHAR(64) 即 64 个字符,足以容纳「部门id」「部门id:岗位编码」等复合身份。
+-- 达梦不支持 `coalesce` 走索引,索引策略同上(operator_id 在前)。
+
+
+-- -----------------------------------------------------------------------------
+-- 5. 升级后自检(可选)
+-- -----------------------------------------------------------------------------
+-- 4.1 身份列是否都已创建(不同数据库查询 information_schema 的写法略有差异)
+-- SELECT table_name, column_name
+-- FROM information_schema.columns
+-- WHERE column_name LIKE '%operator_identity'
+-- ORDER BY table_name, column_name;
+
+-- 4.2 待办合并键格式抽查:默认身份应为 "id-runtime-node",非空身份应带 "#identity" 后缀
+-- SELECT current_operator_id, current_operator_identity, todo_key
+-- FROM t_flow_todo_record
+-- ORDER BY id DESC
+-- LIMIT 20;
diff --git a/docs/Integration/user-integration.md b/docs/Integration/user-integration.md
index 8e5a0cb6..ae1b1e72 100644
--- a/docs/Integration/user-integration.md
+++ b/docs/Integration/user-integration.md
@@ -14,12 +14,20 @@ package com.codingapi.flow.gateway;
/** 流程操作者防腐层 */
public interface FlowOperatorGateway {
IFlowOperator get(long id);
+
+ /** 按 (id, identity) 取用户,default 退化为 get(id);多身份场景需覆盖(见「多身份参与」) */
+ default IFlowOperator get(long id, String identity) {
+ return get(id);
+ }
+
List findByIds(List ids);
}
```
引擎所有「按 id 取用户 / 批量取用户」的请求都汇聚到这个接口。**框架不提供默认实现**(`flow-engine-starter-infra` 也不提供),必须由业务方提供一个 Spring Bean。
+> 同一人员以多个身份参与流程时(例如同时隶属两个部门),需要覆盖 `get(long, String)`,详见 [多身份参与](./operator-identity.md)。
+
### 1.2 `IFlowOperator`(流程操作人)
文件:`flow-engine-framework/src/main/java/com/codingapi/flow/operator/IFlowOperator.java`
diff --git a/flow-engine-example/src/main/java/com/codingapi/example/entity/User.java b/flow-engine-example/src/main/java/com/codingapi/example/entity/User.java
index 7e8eab6f..dce7554d 100644
--- a/flow-engine-example/src/main/java/com/codingapi/example/entity/User.java
+++ b/flow-engine-example/src/main/java/com/codingapi/example/entity/User.java
@@ -8,6 +8,7 @@
import com.codingapi.springboot.script.annotation.ScriptType;
import jakarta.persistence.*;
import lombok.Data;
+import lombok.EqualsAndHashCode;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.List;
@@ -37,6 +38,18 @@ public class User implements IFlowOperator {
private String account;
private String password;
+ /**
+ * 当前身份标识(issue #288)。
+ *
+ * 同一个人员可能隶属多个部门/岗位,以不同身份参与流程。身份是**运行期选择**而非用户主数据,
+ * 因此不落库;由 {@code FlowOperatorGateway#get(id, identity)} 按身份返回携带该标识的用户副本。
+ * 不参与 {@code equals/hashCode},避免同一 id 的两个身份副本被判定为不同用户。
+ */
+ @Transient
+ @EqualsAndHashCode.Exclude
+ @ScriptField(name = "identity", description = "当前身份标识")
+ private String identity;
+
public static User admin(PasswordEncoder passwordEncoder) {
User user = new User();
@@ -75,6 +88,36 @@ public boolean isFlowManager() {
return flowManager!=null && flowManager ;
}
+ /**
+ * 当前身份标识,空串表示默认身份(issue #288)。
+ */
+ @Override
+ @ScriptFunction(name = "getIdentity", description = "获取当前身份标识")
+ public String getIdentity() {
+ return identity;
+ }
+
+ /**
+ * 返回携带指定身份的副本。
+ *
+ *
必须复制而不是直接改写实体:JPA 返回的是持久化上下文中的共享实例,
+ * 直接写入 {@code identity} 会让同一 id 的两个身份互相覆盖(缓存串味)。
+ *
+ * @param identity 身份标识
+ * @return 带身份的副本
+ */
+ public User withIdentity(String identity) {
+ User copy = new User();
+ copy.id = this.id;
+ copy.name = this.name;
+ copy.flowManager = this.flowManager;
+ copy.flowOperatorId = this.flowOperatorId;
+ copy.account = this.account;
+ copy.password = this.password;
+ copy.identity = identity;
+ return copy;
+ }
+
@Override
public IFlowOperator forwardOperator(GroovyScriptRequest request) {
if (flowOperatorId != null && flowOperatorId > 0) {
diff --git a/flow-engine-example/src/main/java/com/codingapi/example/gateway/impl/FlowOperatorGatewayImpl.java b/flow-engine-example/src/main/java/com/codingapi/example/gateway/impl/FlowOperatorGatewayImpl.java
index ca79b81f..d133ca0d 100644
--- a/flow-engine-example/src/main/java/com/codingapi/example/gateway/impl/FlowOperatorGatewayImpl.java
+++ b/flow-engine-example/src/main/java/com/codingapi/example/gateway/impl/FlowOperatorGatewayImpl.java
@@ -1,8 +1,10 @@
package com.codingapi.example.gateway.impl;
+import com.codingapi.example.entity.User;
import com.codingapi.example.repository.UserRepository;
import com.codingapi.flow.gateway.FlowOperatorGateway;
import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.operator.OperatorIdentity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;
@@ -19,6 +21,22 @@ public IFlowOperator get(long id) {
return userRepository.getUserById(id);
}
+ /**
+ * 按身份获取操作者(issue #288)。
+ *
+ *
示例组织模型里身份就是「以哪个部门/岗位的身份参与流程」,不属于用户主数据,
+ * 因此这里返回携带身份标识的**用户副本**,而不改写持久化上下文中的实体实例。
+ * 真实项目应把身份替换为自己的组织模型(例如按部门 id 查询岗位任职关系)。
+ */
+ @Override
+ public IFlowOperator get(long id, String identity) {
+ if (OperatorIdentity.isDefault(identity)) {
+ return get(id);
+ }
+ User user = userRepository.getUserById(id);
+ return user == null ? null : user.withIdentity(OperatorIdentity.normalize(identity));
+ }
+
@Override
public List findByIds(List ids) {
return userRepository.findUserByIdIn(ids)
diff --git a/flow-engine-example/src/test/java/com/codingapi/example/FlowTodoIdentityQueryTest.java b/flow-engine-example/src/test/java/com/codingapi/example/FlowTodoIdentityQueryTest.java
new file mode 100644
index 00000000..50e96393
--- /dev/null
+++ b/flow-engine-example/src/test/java/com/codingapi/example/FlowTodoIdentityQueryTest.java
@@ -0,0 +1,185 @@
+package com.codingapi.example;
+
+import com.codingapi.example.entity.User;
+import com.codingapi.example.repository.UserRepository;
+import com.codingapi.flow.builder.FormFieldPermissionsBuilder;
+import com.codingapi.flow.builder.NodeStrategyBuilder;
+import com.codingapi.flow.form.DataType;
+import com.codingapi.flow.form.FlowForm;
+import com.codingapi.flow.form.FlowFormBuilder;
+import com.codingapi.flow.form.permission.PermissionType;
+import com.codingapi.flow.infra.entity.FlowTodoRecordEntity;
+import com.codingapi.flow.infra.jpa.FlowTodoRecordEntityRepository;
+import com.codingapi.flow.node.nodes.ApprovalNode;
+import com.codingapi.flow.node.nodes.EndNode;
+import com.codingapi.flow.node.nodes.StartNode;
+import com.codingapi.flow.pojo.request.FlowCreateRequest;
+import com.codingapi.flow.service.FlowService;
+import com.codingapi.flow.service.WorkflowService;
+import com.codingapi.flow.strategy.node.FormFieldPermissionStrategy;
+import com.codingapi.flow.strategy.node.OperatorLoadStrategy;
+import com.codingapi.flow.workflow.Workflow;
+import com.codingapi.flow.workflow.WorkflowBuilder;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.test.context.TestPropertySource;
+
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * 待办查询的身份隔离测试(issue #288 P2)。
+ *
+ * 校验两件事:
+ *
+ * - 身份条件真正落到 SQL 上(不是内存过滤);
+ * - 历史数据身份列为 {@code NULL} 时,按默认身份查询仍然能查到——否则升级瞬间
+ * 所有存量待办都会从用户列表里消失。
+ *
+ */
+@SpringBootTest
+@TestPropertySource(properties = "spring.datasource.url=jdbc:h2:mem:flow-todo-identity-test;DB_CLOSE_DELAY=-1")
+class FlowTodoIdentityQueryTest {
+
+ @Autowired
+ private FlowService flowService;
+
+ @Autowired
+ private WorkflowService workflowService;
+
+ @Autowired
+ private UserRepository userRepository;
+
+ @Autowired
+ private FlowTodoRecordEntityRepository flowTodoRecordEntityRepository;
+
+ /**
+ * 历史数据(身份为 NULL)必须能被默认身份查到,且不会串到非默认身份的查询结果里。
+ */
+ @Test
+ void should_match_null_identity_as_default_for_legacy_rows() {
+ long operatorId = 880001L;
+
+ FlowTodoRecordEntity legacy = new FlowTodoRecordEntity();
+ legacy.setProcessId("legacy-process");
+ legacy.setWorkflowRuntimeId(1L);
+ legacy.setWorkCode("legacy");
+ legacy.setNodeId("start");
+ legacy.setNodeType("START");
+ legacy.setNodeName("开始");
+ legacy.setTitle("历史待办");
+ legacy.setCurrentOperatorId(operatorId);
+ legacy.setCurrentOperatorName("user1");
+ legacy.setCurrentOperatorIdentity(null);
+ legacy.setTodoKey(operatorId + "-1-start");
+ legacy.setMargeCount(0);
+ legacy.setMergeable(false);
+ legacy.setRecordId(1L);
+ legacy.setCreateTime(System.currentTimeMillis());
+ flowTodoRecordEntityRepository.save(legacy);
+
+ FlowTodoRecordEntity department1 = new FlowTodoRecordEntity();
+ department1.setProcessId("department1-process");
+ department1.setWorkflowRuntimeId(1L);
+ department1.setWorkCode("leave");
+ department1.setNodeId("start");
+ department1.setNodeType("START");
+ department1.setNodeName("开始");
+ department1.setTitle("部门1待办");
+ department1.setCurrentOperatorId(operatorId);
+ department1.setCurrentOperatorName("user1");
+ department1.setCurrentOperatorIdentity("department1");
+ department1.setTodoKey(operatorId + "-1-start#department1");
+ department1.setMargeCount(0);
+ department1.setMergeable(false);
+ department1.setRecordId(2L);
+ department1.setCreateTime(System.currentTimeMillis());
+ flowTodoRecordEntityRepository.save(department1);
+
+ PageRequest page = PageRequest.of(0, 10);
+
+ // 默认身份:只命中身份为 NULL 的历史行
+ assertEquals(1, flowTodoRecordEntityRepository
+ .findTodoRecordPageByIdentity(operatorId, "", page).getTotalElements());
+ // 空串与 NULL 等价,不能把非默认身份的行也带出来
+ assertEquals(0, flowTodoRecordEntityRepository
+ .findTodoRecordPageByIdentity(operatorId, "department2", page).getTotalElements());
+ // 非默认身份:只命中自己的行
+ assertEquals(1, flowTodoRecordEntityRepository
+ .findTodoRecordPageByIdentity(operatorId, "department1", page).getTotalElements());
+ }
+
+ /**
+ * 端到端:以指定身份发起流程 → 待办按身份落库 → 按身份可查、按默认身份查不到。
+ */
+ @Test
+ void should_persist_identity_and_query_todo_by_identity() {
+ User admin = userRepository.getUserByAccount(User.ADMIN_ACCOUNT);
+ String code = "identity-" + System.nanoTime();
+
+ FlowForm form = FlowFormBuilder.builder()
+ .name("身份测试流程")
+ .code(code)
+ .addField("事由", "reason", DataType.STRING)
+ .build();
+
+ FormFieldPermissionsBuilder permissions = FormFieldPermissionsBuilder.builder()
+ .addPermission(code, "reason", PermissionType.WRITE);
+
+ StartNode startNode = StartNode.builder()
+ .strategies(NodeStrategyBuilder.builder()
+ .addStrategy(new FormFieldPermissionStrategy(permissions.build()))
+ .build())
+ .build();
+
+ ApprovalNode approvalNode = ApprovalNode.builder()
+ .name("经理审批")
+ .strategies(NodeStrategyBuilder.builder()
+ .addStrategy(new FormFieldPermissionStrategy(permissions.build()))
+ .addStrategy(OperatorLoadStrategy.initiatorSelectStrategy())
+ .build())
+ .build();
+
+ EndNode endNode = EndNode.builder().build();
+
+ Workflow workflow = WorkflowBuilder.builder()
+ .title("身份测试流程")
+ .code(code)
+ .createdOperator(admin)
+ .form(form)
+ .addNode(startNode)
+ .addNode(approvalNode)
+ .addNode(endNode)
+ .build();
+ workflowService.saveWorkflow(workflow);
+
+ FlowCreateRequest createRequest = new FlowCreateRequest();
+ createRequest.setWorkCode(workflow.getCode());
+ createRequest.setFormData(Map.of("reason", "测试身份"));
+ createRequest.setActionId(startNode.actionManager().getActions().get(0).id());
+ createRequest.setOperatorId(admin.getUserId());
+ createRequest.setOperatorIdentity("department1");
+
+ PageRequest page = PageRequest.of(0, 50);
+ // 用增量断言,避免同一上下文中其他用例的待办影响计数
+ long identityBefore = flowTodoRecordEntityRepository
+ .findTodoRecordPageByIdentity(admin.getUserId(), "department1", page).getTotalElements();
+ long defaultBefore = flowTodoRecordEntityRepository
+ .findTodoRecordPageByIdentity(admin.getUserId(), "", page).getTotalElements();
+
+ long recordId = flowService.create(createRequest);
+ assertTrue(recordId > 0, "流程创建应成功并返回记录id");
+
+ long identityAfter = flowTodoRecordEntityRepository
+ .findTodoRecordPageByIdentity(admin.getUserId(), "department1", page).getTotalElements();
+ long defaultAfter = flowTodoRecordEntityRepository
+ .findTodoRecordPageByIdentity(admin.getUserId(), "", page).getTotalElements();
+
+ assertEquals(identityBefore + 1, identityAfter, "带身份的待办应能按身份查到");
+ assertEquals(defaultBefore, defaultAfter, "带身份的待办不应出现在默认身份列表里");
+ }
+}
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/AddAuditAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/AddAuditAction.java
index 60232cd9..12fa25c1 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/AddAuditAction.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/AddAuditAction.java
@@ -12,6 +12,7 @@
import com.codingapi.flow.manager.OperatorManager;
import com.codingapi.flow.node.IFlowNode;
import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.operator.OperatorIdentity;
import com.codingapi.flow.record.FlowRecord;
import com.codingapi.flow.script.node.OperatorLoadScript;
import com.codingapi.flow.session.FlowSession;
@@ -153,24 +154,31 @@ private void verifyNotInTodoQueue(FlowSession flowSession, List a
if (auditOperators == null || auditOperators.isEmpty()) {
return;
}
- Set pendingOperatorIds = pendingOperatorIds(flowSession);
- if (pendingOperatorIds.isEmpty()) {
+ Set pendingOperators = pendingOperatorKeys(flowSession);
+ if (pendingOperators.isEmpty()) {
return;
}
for (IFlowOperator auditOperator : auditOperators) {
- if (pendingOperatorIds.contains(auditOperator.getUserId())) {
+ if (pendingOperators.contains(operatorKey(auditOperator.getUserId(), OperatorIdentity.of(auditOperator)))) {
throw FlowExecutionException.addAuditOperatorAlreadyInTodo(auditOperator.getUserId());
}
}
}
/**
- * 当前节点实例中仍有待办的人员id集合。
+ * 参与人键:{@code userId + identity},同一人员的不同身份是不同参与人(issue #288)
+ */
+ private static String operatorKey(long userId, String identity) {
+ return userId + "\u0001" + OperatorIdentity.normalize(identity);
+ }
+
+ /**
+ * 当前节点实例中仍有待办的参与人键集合(userId + identity)。
*
* 仓储按 {@code (fromId, nodeId)} 查询节点记录,开始节点({@code fromId = 0})等场景可能
* 命中其他流程实例的记录,必须按 {@code processId} 限定,避免误判其他流程的待办人员。
*/
- private Set pendingOperatorIds(FlowSession flowSession) {
+ private Set pendingOperatorKeys(FlowSession flowSession) {
FlowRecord currentRecord = flowSession.getCurrentRecord();
List currentNodeRecords = flowSession.getCurrentNodeRecords();
if (currentRecord == null || currentNodeRecords == null) {
@@ -180,7 +188,7 @@ private Set pendingOperatorIds(FlowSession flowSession) {
.filter(Objects::nonNull)
.filter(record -> currentRecord.getProcessId().equals(record.getProcessId()))
.filter(FlowRecord::isPendingApproval)
- .map(FlowRecord::getCurrentOperatorId)
+ .map(record -> operatorKey(record.getCurrentOperatorId(), record.getCurrentOperatorIdentity()))
.collect(Collectors.toSet());
}
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java
index ed069947..4dea88cb 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/PassAction.java
@@ -203,7 +203,8 @@ public void run(FlowSession flowSession) {
// 是否转交审批人的流程
if (isFinish && currentRecord.isForward()) {
- IFlowOperator forwardOperator = repositoryHolder.getOperatorById(currentRecord.getForwardOperatorId());
+ IFlowOperator forwardOperator = repositoryHolder.getOperatorById(
+ currentRecord.getForwardOperatorId(), currentRecord.getForwardOperatorIdentity());
FlowRecord notifyRecord = currentRecord.create(flowSession.updateSession(forwardOperator));
notifyRecord.notifyRecord(flowSession.updateSession(forwardOperator));
// 如果不存储这个记录,若下一流程是结束流程时,无法更新流程状态为结束状态。
@@ -215,7 +216,8 @@ public void run(FlowSession flowSession) {
// 多人审批时其他待办可能尚未完成,若只在 isFinish 时返回,委派记录会永久滞留(issue #239)
if (currentRecord.isDelegate()) {
FlowRecord delegateRecord = repositoryHolder.getRecordById(currentRecord.getDelegateId());
- IFlowOperator delegateOperator = repositoryHolder.getOperatorById(delegateRecord.getCurrentOperatorId());
+ IFlowOperator delegateOperator = repositoryHolder.getOperatorById(
+ delegateRecord.getCurrentOperatorId(), delegateRecord.getCurrentOperatorIdentity());
FlowRecord rebackRecord = delegateRecord.create(flowSession.updateSession(delegateOperator));
// 返回记录同样归属原委托记录的节点实例与审批槽位(issue #239)
rebackRecord.inheritNodeInstance(delegateRecord);
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReduceAuditAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReduceAuditAction.java
index 76cb04f2..7783e755 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReduceAuditAction.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReduceAuditAction.java
@@ -13,6 +13,7 @@
import com.codingapi.flow.node.BaseAuditNode;
import com.codingapi.flow.node.IFlowNode;
import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.operator.OperatorIdentity;
import com.codingapi.flow.record.FlowRecord;
import com.codingapi.flow.script.node.OperatorLoadScript;
import com.codingapi.flow.session.FlowSession;
@@ -170,12 +171,17 @@ private List resolveTargetRecords(List targets,
List targetRecords = new ArrayList<>();
for (IFlowOperator target : targets) {
long targetOperatorId = target.getUserId();
- if (targetOperatorId == currentRecord.getCurrentOperatorId()) {
+ String targetOperatorIdentity = OperatorIdentity.of(target);
+ // 自己不能被自己减签:按 (userId, identity) 判断,避免把另一身份的待办当成自己的
+ if (targetOperatorId == currentRecord.getCurrentOperatorId()
+ && OperatorIdentity.same(targetOperatorIdentity, currentRecord.getCurrentOperatorIdentity())) {
throw FlowExecutionException.reduceAuditOwnRecord();
}
+ // 减签按「人 + 身份」生效:同一人员的另一个身份仍然是独立参与人,不能被顺带撤销
List matchedRecords = currentNodeRecords.stream()
.filter(FlowRecord::isPendingApproval)
- .filter(record -> record.getCurrentOperatorId() == targetOperatorId)
+ .filter(record -> record.getCurrentOperatorId() == targetOperatorId
+ && OperatorIdentity.same(record.getCurrentOperatorIdentity(), targetOperatorIdentity))
.toList();
if (matchedRecords.isEmpty()) {
throw FlowExecutionException.reduceAuditTargetNotFound(targetOperatorId);
diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/cache/FlowOperatorLocalThreadCache.java b/flow-engine-framework/src/main/java/com/codingapi/flow/cache/FlowOperatorLocalThreadCache.java
index 57a994ca..333568e8 100644
--- a/flow-engine-framework/src/main/java/com/codingapi/flow/cache/FlowOperatorLocalThreadCache.java
+++ b/flow-engine-framework/src/main/java/com/codingapi/flow/cache/FlowOperatorLocalThreadCache.java
@@ -1,6 +1,7 @@
package com.codingapi.flow.cache;
import com.codingapi.flow.operator.IFlowOperator;
+import com.codingapi.flow.operator.OperatorIdentity;
import lombok.Getter;
import java.util.ArrayList;
@@ -9,9 +10,23 @@
import java.util.Map;
import java.util.function.Supplier;
+/**
+ * 流程操作者线程缓存。
+ *
+ * 同一个人员可以以不同「身份」参与流程(issue #288),因此缓存键必须是
+ * {@code (userId, identity)} 二元组而不是 userId:以身份为维度隔离后,同一 id 的
+ * 两个身份各自缓存各自的 {@link IFlowOperator} 实例,不会互相串味。
+ *
+ *
身份为 {@code null}/{@code ""}(默认身份)时与历史行为等价,等同于只按 userId 缓存。
+ */
public class FlowOperatorLocalThreadCache {
- private final ThreadLocal