diff --git a/docs/Integration/operator-identity.md b/docs/Integration/operator-identity.md index 62901d3e..316487d0 100644 --- a/docs/Integration/operator-identity.md +++ b/docs/Integration/operator-identity.md @@ -249,9 +249,11 @@ def run(request) { | 表 | 新增列 | |----|--------| -| `t_flow_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity`、`forward_operator_identity`、`interfered_operator_identity` | +| `t_flow_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity`、`forward_operator_identity`、`interfered_operator_identity`、`handle_operator_identity` | | `t_flow_todo_record` | `create_operator_identity`、`submit_operator_identity`、`current_operator_identity` | +「办理人」另有两列非身份字段(issue #306):`handle_operator_id`、`handle_operator_name`,与 `handle_operator_identity` 一起记录**谁以什么身份办理了这条记录**。待办表不加这三列——待办尚未办理,没有办理人语义。 + 只存身份标识本身,**不落库任何附加属性**(避免为 5 组操作者各加一个 JSON 快照列,也避免身份名称被冻结在历史数据里)。展示名称仍取操作者姓名;需要展示身份名称时由宿主按 `identity` 自行映射。 升级脚本见 [sql/0.3.0-operator-identity.sql](./sql/0.3.0-operator-identity.sql)。 @@ -311,7 +313,23 @@ def run(request) { `OperatorManager`、操作者线程缓存、网关缓存的 `(userId, identity)` 键仍保持严格匹配——它们解决的是「取到哪一个带身份的操作者实例」,不是权限问题。 -留痕口径:被办理记录保留其归属身份(`currentOperatorIdentity`,由取人脚本决定),本次操作携带的身份写入下游新生成记录的 `submitOperatorIdentity`;`USER_ONLY` 下同人换身份不算「被干预」,因此不会写 `interferedOperator*`。 +留痕口径(`USER_ONLY` 下同人换身份不算「被干预」,因此不会写 `interferedOperator*`): + +| 字段 | 语义 | 写入时机 | +|------|------|----------| +| `currentOperatorId/Name/Identity` | 这条待办的**归属**人(由取人脚本决定) | 记录生成时;办结后**不变** | +| `submitOperatorId/Name/Identity` | **创建这条记录时**的上游提交人 | 记录生成时 | +| `handleOperatorId/Name/Identity` | **本次办理人**——谁、以什么身份办的 | 办结动作(通过 / 拒绝 / 退回 / 转办 / 委派);暂存与抄送刷新不写 | + +因此「办结」不会改写归属身份,只额外留下办理人三元组。 + +> **issue #306 之前的行为**:办理动作不写任何身份字段,最后一步办理人身份**无处落地**——没有下游记录可以承载 +> `submitOperator*`,于是「张三以 `department2` 审批通过」在库里只能看到 `department1`(待办归属)或默认身份。 +> 中间节点只能跳一条到下游记录的 `submitOperatorIdentity` 间接推断,且要求下游是同一个人。 +> 升级后请以 `handleOperator*` 作为「已办是谁办的」的唯一口径。 + +`FlowRecord#markHandleOperator(IFlowOperator)` 由办结动作显式调用,**不在 `update()` 内隐式写入**:`update()` 同时服务于 +暂存(`SaveAction`,`pass=false`)与抄送刷新(`notifyRecord`),隐式写入会把暂存人、抄送接收人误记成办理人。 ### 4.5 身份写入约束 @@ -353,6 +371,7 @@ codingapi.flow.identity.match-mode=USER_ONLY | 身份长度 | ≤ 64 字符(复合 key 用 `:` 拼接),入口与落库双处校验 | | 待办索引 | 实体未声明 `@Index`,大表请按 DDL 脚本手工加索引 | | 默认取人脚本 | issue #303 起新建节点的默认脚本返回 `OperatorKey`(创建者以发起身份参与);存量流程定义中的旧脚本不受影响,不迁移 | +| 办理人留痕 | issue #306 起 `handle_operator_*` 记录办理人与办理身份;存量记录为空,前端回退到 `current_operator_identity` 展示,行为与升级前一致;如需回填,按 `finish_time` 与下游 `submit_operator_*` 推断,引擎不做自动迁移 | | 不做的事 | 引擎不建组织模型、不做身份授权校验(「user1 能否以 department1 身份操作」由业务侧保证)、不冻结身份名称 | > 身份是一个**不透明字符串**,不要尝试在引擎里比较它的业务含义;需要「部门名称」等展示信息,由宿主维护 `identity → 名称` 的映射自行渲染(框架不提供附加属性字段)。 diff --git a/docs/Integration/sql/0.3.0-operator-identity.sql b/docs/Integration/sql/0.3.0-operator-identity.sql index 7a207227..e0b4b3ac 100644 --- a/docs/Integration/sql/0.3.0-operator-identity.sql +++ b/docs/Integration/sql/0.3.0-operator-identity.sql @@ -27,6 +27,18 @@ ALTER TABLE t_flow_record ADD COLUMN forward_operator_identity VARCHAR(64) NU ALTER TABLE t_flow_record ADD COLUMN interfered_operator_identity VARCHAR(64) NULL; +-- ----------------------------------------------------------------------------- +-- 1.1 流程记录表:办理人三列(issue #306) +-- ----------------------------------------------------------------------------- +-- 背景:此前记录被办结时不写任何身份字段,current_operator_identity 保留的是「待办归属身份」, +-- submit_operator_identity 只表示「创建该记录时的上游提交人」,最后一步办理人身份因此无处可落。 +-- 语义:handle_operator_* = 本次实际办理人的 (id, 名称, 身份),暂存与抄送刷新不写入。 +-- 兼容:与 current_operator_identity 并存、互不覆盖;历史数据为 NULL,前端可回退到 current_operator_identity。 +ALTER TABLE t_flow_record ADD COLUMN handle_operator_id BIGINT NULL; +ALTER TABLE t_flow_record ADD COLUMN handle_operator_name VARCHAR(255) NULL; +ALTER TABLE t_flow_record ADD COLUMN handle_operator_identity VARCHAR(64) NULL; + + -- ----------------------------------------------------------------------------- -- 2. 待办表:发起 / 提交 / 当前 三组操作者的身份 -- ----------------------------------------------------------------------------- diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java index ccbd99f2..ecd87247 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/DelegateAction.java @@ -73,6 +73,7 @@ public void run(FlowSession flowSession) { FlowRecord currentRecord = flowSession.getCurrentRecord(); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(currentRecord); flowEvents.add(new FlowRecordDoneEvent(currentRecord, flowSession.isMock())); 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 bca7e0c9..e39830c4 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 @@ -141,6 +141,7 @@ public void run(FlowSession flowSession) { boolean isFinish = currentNode.isFinish(flowSession); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); // 添加流程结束事件 flowEvents.add(new FlowRecordDoneEvent(currentRecord, flowSession.isMock())); recordList.add(currentRecord); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java index ca7db511..5c06bfd7 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/RejectAction.java @@ -100,6 +100,7 @@ public void run(FlowSession flowSession) { FlowRecord flowRecord = flowSession.getCurrentRecord(); flowRecord.update(flowSession, true); + flowRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(flowRecord); // 当前审批为已办状态 diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java index 268cda80..14195421 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReturnAction.java @@ -54,6 +54,7 @@ public void run(FlowSession flowSession) { FlowRecord currentRecord = flowSession.getCurrentRecord(); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(currentRecord); flowEvents.add(new FlowRecordDoneEvent(currentRecord,flowSession.isMock())); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java index 8d2a085b..0eec025e 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/TransferAction.java @@ -82,6 +82,7 @@ public void run(FlowSession flowSession) { FlowRecord currentRecord = flowSession.getCurrentRecord(); currentRecord.update(flowSession, true); + currentRecord.markHandleOperator(flowSession.getCurrentOperator()); recordList.add(currentRecord); flowEvents.add(new FlowRecordDoneEvent(currentRecord,flowSession.isMock())); diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java index c0e85522..38c5b5e4 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowRecordContent.java @@ -100,6 +100,21 @@ public class FlowRecordContent { */ private String createOperatorIdentity; + /** + * 办理人Id(issue #306),记录办结时的实际办理人 + */ + private Long handleOperatorId; + + /** + * 办理人名称(issue #306) + */ + private String handleOperatorName; + + /** + * 办理人身份标识(issue #306),空串表示默认身份 + */ + private String handleOperatorIdentity; + /** * 合并记录id,当存在合并记录数据时待办记录数量不会增加,内容会更新至最新的待办数据信息。 * {@link FlowRecord#getTodoKey()} @@ -161,6 +176,9 @@ public static FlowRecordContent convert(FlowRecord record){ content.setCreateOperatorId(record.getCreateOperatorId()); content.setCreateOperatorName(record.getCreateOperatorName()); content.setCreateOperatorIdentity(OperatorIdentity.normalize(record.getCreateOperatorIdentity())); + content.setHandleOperatorId(record.getHandleOperatorId()); + content.setHandleOperatorName(record.getHandleOperatorName()); + content.setHandleOperatorIdentity(OperatorIdentity.normalize(record.getHandleOperatorIdentity())); content.setMergeable(record.isMergeable()); content.setRecordId(record.getId()); content.setTimeoutTime(record.getTimeoutTime()); 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 ebd6cd91..785886a3 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 @@ -303,6 +303,25 @@ public class FlowRecord { */ private String interferedOperatorIdentity; + /** + * 办理人Id(issue #306) + *
记录被办结时,本次实际办理人的用户 id;暂存与抄送刷新不写入。 + */ + private long handleOperatorId; + + /** + * 办理人名称(issue #306) + */ + private String handleOperatorName; + + /** + * 办理人身份标识(issue #306) + *
本次办理时使用的身份,空串表示默认身份。与「待办归属身份」 + * {@link FlowRecord#currentOperatorIdentity} 互不覆盖:办结不改变待办归属, + * 只额外留下「谁以什么身份办的」。 + */ + private String handleOperatorIdentity; + /** * 数据合并的依据,当开启时值为固定值,否则为随机数据 *
合并依据由 {@link RecordMergeStrategy.MergeType} 决定: @@ -401,6 +420,7 @@ public FlowRecord(FlowSession flowSession, int nodeOrder) { // 身份字段统一以空串兜底,避免出现 null 与空串两种「默认身份」表达 this.forwardOperatorIdentity = OperatorIdentity.DEFAULT; this.interferedOperatorIdentity = OperatorIdentity.DEFAULT; + this.handleOperatorIdentity = OperatorIdentity.DEFAULT; // 是否转交之后的人来审批的判断 if (currentOperator.equals(sourceOperator)) { this.forwardOperatorId = 0; @@ -648,6 +668,28 @@ public void update(FlowSession flowSession, boolean pass) { } } + /** + * 记录本次办理人(issue #306)。 + * + *
必须由「真正办结」的动作显式调用(通过 / 拒绝 / 退回 / 转办 / 委派)。 + * 暂存({@link com.codingapi.flow.action.actions.SaveAction})与抄送刷新 + * ({@link #notifyRecord(com.codingapi.flow.session.FlowSession)})只是刷新记录内容、 + * 并未产生办理结果,因此不能写入,否则会把暂存人、抄送接收人误记为办理人。 + * + *
不修改 {@link #currentOperatorId} / {@link #currentOperatorIdentity}: + * 待办归属身份在办结后仍需保留(见 docs/Integration/operator-identity.md §4.4)。 + * + * @param operator 本次实际办理人 + */ + public void markHandleOperator(IFlowOperator operator) { + if (operator == null) { + return; + } + this.handleOperatorId = operator.getUserId(); + this.handleOperatorName = operator.getName(); + this.handleOperatorIdentity = OperatorIdentity.of(operator); + } + public void clearNotify(){ this.readable = false; this.readTime = 0; @@ -818,6 +860,18 @@ public void newRecord() { this.setAdvice(null); this.setSignKey(null); this.setActionId(null); + // 新记录尚未被办理,不能继承上游记录的办理人信息(issue #306) + this.clearHandleOperator(); + } + + /** + * 清空办理人信息(issue #306)。 + *
记录回到「未办理」状态时调用,避免复用记录对象时把上一位办理人带到新的待办上。 + */ + public void clearHandleOperator() { + this.handleOperatorId = 0; + this.handleOperatorName = null; + this.handleOperatorIdentity = OperatorIdentity.DEFAULT; } /** 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 2ee38d02..0d527738 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 @@ -147,6 +147,9 @@ private static FlowRecord copy(FlowRecord record) { record.getSubmitOperatorIdentity(), record.getCurrentOperatorIdentity(), record.getForwardOperatorIdentity(), - record.getInterferedOperatorIdentity()); + record.getInterferedOperatorIdentity(), + record.getHandleOperatorId(), + record.getHandleOperatorName(), + record.getHandleOperatorIdentity()); } } \ No newline at end of file diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/service/HandleOperatorIdentityFlowTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/service/HandleOperatorIdentityFlowTest.java new file mode 100644 index 00000000..72649123 --- /dev/null +++ b/flow-engine-framework/src/test/java/com/codingapi/flow/service/HandleOperatorIdentityFlowTest.java @@ -0,0 +1,290 @@ +package com.codingapi.flow.service; + +import com.codingapi.flow.action.ActionType; +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.operator.OperatorIdentity; +import com.codingapi.flow.operator.OperatorIdentityMatchMode; +import com.codingapi.flow.operator.OperatorIdentityPolicy; +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.script.factory.FlowGroovyScriptFactory; +import com.codingapi.flow.strategy.node.FormFieldPermissionStrategy; +import com.codingapi.flow.strategy.node.OperatorLoadStrategy; +import com.codingapi.flow.strategy.node.RevokeStrategy; +import com.codingapi.flow.user.User; +import com.codingapi.flow.workflow.Workflow; +import com.codingapi.flow.workflow.WorkflowBuilder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * 办理人身份留痕的流程级测试(issue #306)。 + * + *
核心回归:记录被办结后必须留下「本次办理人」的 {@code handleOperatorId/Name/Identity},
+ * 与「待办归属身份」{@code currentOperatorIdentity} 互不覆盖。此前 {@code update()} 不写任何身份字段,
+ * 导致最后一步办理身份(没有下游记录可承载)彻底丢失。
+ */
+class HandleOperatorIdentityFlowTest {
+
+ private static final long INITIATOR = 1;
+ private static final long APPROVER = 2;
+
+ private static final String DEPARTMENT_1 = "department1";
+ private static final String DEPARTMENT_2 = "department2";
+
+ private MyFlowServiceFactory factory;
+ private final Map 查询层({@code FlowRecordQueryServiceImpl})把 JPA 实体直接交给
+ * {@link FlowRecordContentConvertor#convert(FlowRecordEntity)} 组装列表数据,
+ * 不走领域对象链路。因此「办理人」三字段必须在该转换器里显式映射,
+ * 否则已办列表拿到的永远是 null,前端只能回退到待办归属人。
+ */
+class FlowRecordContentConvertorTest {
+
+ /**
+ * 构造字段完整的流程记录实体,避免转换时的拆箱 NPE 干扰断言。
+ */
+ private FlowRecordEntity newRecordEntity() {
+ FlowRecordEntity entity = new FlowRecordEntity();
+ entity.setId(2L);
+ entity.setWorkRuntimeId(1L);
+ entity.setWorkTitle("身份标识验证流程");
+ entity.setWorkCode("identity-demo");
+ entity.setNodeId("approval");
+ entity.setNodeType("APPROVAL");
+ entity.setNodeName("审批节点");
+ entity.setTitle("你有一条待办");
+ entity.setFromId(0L);
+ entity.setParentId(0L);
+ entity.setReadTime(0L);
+ entity.setCurrentOperatorId(2L);
+ entity.setCurrentOperatorName("张三");
+ entity.setSubmitOperatorId(1L);
+ entity.setForwardOperatorId(0L);
+ entity.setNodeOrder(0);
+ entity.setHidden(false);
+ entity.setRevoked(false);
+ entity.setNotify(false);
+ entity.setRecordState(FlowRecord.SATE_RECORD_DONE);
+ entity.setFlowState(FlowRecord.SATE_FLOW_FINISH);
+ 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;
+ }
+
+ /**
+ * 测试目标:已办记录的办理人(人 + 身份)必须出现在列表响应里。
+ * 前置条件:实体带有本次办理人信息(张三以 department1 身份办理)。
+ * 执行步骤:实体 → 列表响应对象。
+ * 期望断言:办理人 id / 名称 / 身份与实体一致,且不与待办归属人混淆。
+ */
+ @Test
+ void should_expose_handle_operator_for_done_record() {
+ FlowRecordEntity entity = newRecordEntity();
+ entity.setCurrentOperatorIdentity("");
+ entity.setCreateOperatorIdentity("");
+ entity.setHandleOperatorId(2L);
+ entity.setHandleOperatorName("张三");
+ entity.setHandleOperatorIdentity("department1");
+
+ FlowRecordContent content = FlowRecordContentConvertor.convert(entity);
+
+ assertAll(
+ () -> assertEquals(2L, content.getHandleOperatorId()),
+ () -> assertEquals("张三", content.getHandleOperatorName()),
+ () -> assertEquals("department1", content.getHandleOperatorIdentity()),
+ // 待办归属身份保持原样,办理身份不覆盖它
+ () -> assertEquals("", content.getCurrentOperatorIdentity())
+ );
+ }
+
+ /**
+ * 测试目标:升级前的存量已办记录(办理人列为空)不报错、不伪造数据。
+ * 前置条件:实体 handle 三字段均为 null。
+ * 执行步骤:实体 → 列表响应对象。
+ * 期望断言:id / 名称保持 null,身份归一化为空串,前端据此回退待办归属人。
+ */
+ @Test
+ void should_keep_handle_operator_empty_for_legacy_done_record() {
+ FlowRecordEntity entity = newRecordEntity();
+
+ FlowRecordContent content = FlowRecordContentConvertor.convert(entity);
+
+ assertAll(
+ () -> assertNull(content.getHandleOperatorId()),
+ () -> assertNull(content.getHandleOperatorName()),
+ () -> assertEquals("", content.getHandleOperatorIdentity())
+ );
+ }
+}