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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions docs/Integration/operator-identity.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,30 @@ public class User implements IFlowOperator {

### 3.2 让网关按身份返回操作者(关键)

`FlowOperatorGateway` 以 `OperatorKey` 作为参与人查询契约,业务方必须实现:

- `get(OperatorKey key)`:按人员与身份获取一个操作者;
- `findByKeys(List<OperatorKey> keys)`:按完整参与人列表批量获取,允许同一人员对应多个身份。

默认身份的 `get(long)` / `findByIds(List<Long>)` 是便捷方法,内部统一转换为 `OperatorKey.ofDefault(...)`。

```java
@Override
public IFlowOperator get(long id, String identity) {
User user = userRepository.getUserById(id);
if (user == null || OperatorIdentity.of(identity).isDefault()) {
public IFlowOperator get(OperatorKey key) {
User user = userRepository.getUserById(key.operatorId());
if (user == null || OperatorIdentity.of(key.identity()).isDefault()) {
return user;
}
// 必须复制:直接改写实体会污染持久化上下文 / 线程缓存,导致同一 id 的两个身份互相覆盖
return user.withIdentity(identity);
return user.withIdentity(key.identity());
}
```

> `get(long, String)` 在接口上是 `default` 方法,默认退化为 `get(id)`(忽略身份)。**不实现它时,请求里声明的身份不会被写入记录** —— 引擎的身份来源是操作者对象本身,而不是请求参数。多身份场景必须实现此方法。

批量取人同样提供了两个 `default` 重载,用于流程树与人员设定场景(建议一并覆盖,避免同一批人按身份回查时退化为默认身份):

| 接口 | 新增方法 | 未覆盖时的行为 |
|------|----------|----------------|
| `FlowOperatorGateway` | `findByIds(List<Long>, String)` | 退化为 `findByIds(ids)` |
| `IRepositoryHolder` | `findOperatorByIds(List<Long>, String)` | 退化为 `findOperatorByIds(ids)` |
| `IRepositoryHolder` | `getOperatorById(long, String)` | 退化为 `getOperatorById(id)` |
批量实现必须返回与请求身份一致的操作者,并按入参顺序返回;查不到的参与人跳过。`IRepositoryHolder` 同步使用 `getOperatorByKey(OperatorKey)` / `findOperatorsByKeys(List<OperatorKey>)`,不再提供拆分身份参数的默认重载。

> 引擎做了两层兜底,保证「没覆盖」不会静默丢身份:
> 身份不一致时不会静默降级:单条读取按 `(人员id, 身份)` 去重输出 WARN;显式非默认身份的批量结果不会登记线程缓存,也不会作为该参与人的查询结果返回。默认身份读取保留历史兼容行为。
>
> 1. **单条路径**:`GatewayContext#getFlowOperator(id, identity)`、发起(`FlowCreateService`)与审批(`FlowActionService`)三个入口都会做运行期检查——请求声明了非默认身份、但回查到的操作者身份为空时,按 `(人员id, 身份)` 去重输出一条 WARN;
> 2. **批量路径**:`GatewayContext#findByIds(ids, identity)` 发现批量结果身份不符时,逐条回退到单条按身份取人(正确实现会返回带身份的副本,未实现则触发上面的 WARN),同时不把身份不符的对象登记进线程缓存,避免污染后续按身份的读取。
> **升级提示**:自行实现 `FlowOperatorGateway` 的下游需要把 `get(long, String)` 迁移为 `get(OperatorKey)`,把 `findByIds(List<Long>, String)` 迁移为 `findByKeys(List<OperatorKey>)`。这是有意的源码不兼容调整,避免“编译通过但身份被丢弃”。

### 3.3 请求声明身份

Expand Down Expand Up @@ -432,8 +430,8 @@ codingapi.flow.identity.match-mode=USER_ONLY
`identityScope=LOCKED`,此时步骤 4 才会变成「只显示当前身份下的记录」,跨身份办理也会被拒绝
(`state.operator.notMatch`)。

示例的 `User.identity` 是 `@Transient`(运行期身份,不落库),由 `FlowOperatorGateway#get(id, identity)` /
`findByIds(ids, identity)` 返回带身份的副本。真实项目把身份选项换成自己的组织模型即可。
示例的 `User.identity` 是 `@Transient`(运行期身份,不落库),由 `FlowOperatorGateway#get(OperatorKey)` /
`findByKeys(List)` 返回带身份的副本。真实项目把身份选项换成自己的组织模型即可。

### 5.3 常见疑问:为什么「审批人是 admin」却在自己的待办里找不到

Expand Down
66 changes: 45 additions & 21 deletions docs/Integration/user-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ package com.codingapi.flow.gateway;

/** 流程操作者防腐层 */
public interface FlowOperatorGateway {
IFlowOperator get(long id);
IFlowOperator get(OperatorKey key);

/** 按 (id, identity) 取用户,default 退化为 get(id);多身份场景需覆盖(见「多身份参与」) */
default IFlowOperator get(long id, String identity) {
return get(id);
}
List<IFlowOperator> findByKeys(List<OperatorKey> keys);

List<IFlowOperator> findByIds(List<Long> ids);
default IFlowOperator get(long id) {
return get(OperatorKey.ofDefault(id));
}

/** 按 (ids, identity) 批量取用户,default 退化为 findByIds(ids);多身份场景需一并覆盖 */
default List<IFlowOperator> findByIds(List<Long> ids, String identity) {
return findByIds(ids);
default List<IFlowOperator> findByIds(List<Long> ids) {
return findByKeys(ids.stream().map(OperatorKey::ofDefault).toList());
}
}
```

引擎所有「按 id 取用户 / 批量取用户」的请求都汇聚到这个接口。**框架不提供默认实现**(`flow-engine-starter-infra` 也不提供),必须由业务方提供一个 Spring Bean。

> 同一人员以多个身份参与流程时(例如同时隶属两个部门),需要覆盖 `get(long, String)`;
> 流程树、人员设定等批量路径还需要覆盖 `findByIds(List<Long>, String)`,只覆盖单条会让批量路径退化为默认身份。
> 详见 [多身份参与](./operator-identity.md) 3.2。
> 同一人员以多个身份参与流程时(例如同时隶属两个部门),`get(OperatorKey)` 与 `findByKeys(List<OperatorKey>)` 必须返回身份匹配的操作者。
> 接口不再提供忽略身份的默认重载,避免下游编译通过但运行期丢失身份。迁移说明见 [多身份参与](./operator-identity.md) 3.2。

### 1.2 `IFlowOperator`(流程操作人)

Expand Down Expand Up @@ -76,17 +73,24 @@ public class GatewayContext {
@Setter @Getter private FlowOperatorGateway flowOperatorGateway;

public IFlowOperator getFlowOperator(long userId) {
return FlowOperatorLocalThreadCache.getInstance().get(userId, () -> flowOperatorGateway.get(userId));
return getFlowOperator(OperatorKey.ofDefault(userId));
}
public IFlowOperator getFlowOperator(OperatorKey key) {
return FlowOperatorLocalThreadCache.getInstance().get(key, () -> flowOperatorGateway.get(key));
}
public List<IFlowOperator> findByIds(List<Long> ids) {
return FlowOperatorLocalThreadCache.getInstance().find(ids, (idList) -> flowOperatorGateway.findByIds(idList));
return findByKeys(ids.stream().map(OperatorKey::ofDefault).toList());
}
public List<IFlowOperator> findByKeys(List<OperatorKey> keys) {
return FlowOperatorLocalThreadCache.getInstance().find(keys,
missingKeys -> flowOperatorGateway.findByKeys(missingKeys));
}
}
```

- **注入方式**:`GatewayContext.getInstance().setFlowOperatorGateway(bean)`,由 starter 的 `GatewayContextRegister`(`InitializingBean`)在启动时执行。
- **线程缓存** `FlowOperatorLocalThreadCache`(`flow-engine-framework/.../cache/FlowOperatorLocalThreadCache.java`):`ThreadLocal<Map<...>>`,未命中时调用网关并回写;`find` 会过滤 null/<=0 的 id、去重、保序返回(保证多人审批顺序稳定)。缓存键由 `OperatorKey#key()` 生成,**含身份**。`FlowService` 每个操作入口都会 `clear()` 该缓存。
- **带身份的读取**:`GatewayContext.getFlowOperator(userId, identity)` / `findByIds(ids, identity)` 是新增重载;批量结果身份不符时会逐条回退单条按身份回查(见 [多身份参与](./operator-identity.md) 3.2)。
- **带身份的读取**:统一使用 `GatewayContext.getFlowOperator(OperatorKey)` / `findByKeys(List<OperatorKey>)`;批量查询支持同一人员多个身份,显式非默认身份不一致的结果不会入库缓存(见 [多身份参与](./operator-identity.md) 3.2)。

## 3. 集成示例(完整可运行)

Expand Down Expand Up @@ -161,10 +165,16 @@ package com.codingapi.example.gateway.impl;
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 com.codingapi.flow.operator.OperatorKey;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Repository
@AllArgsConstructor
Expand All @@ -173,15 +183,29 @@ public class FlowOperatorGatewayImpl implements FlowOperatorGateway {
private final UserRepository userRepository;

@Override
public IFlowOperator get(long id) {
return userRepository.getUserById(id);
public IFlowOperator get(OperatorKey key) {
User user = userRepository.getUserById(key.operatorId());
if (user == null || OperatorIdentity.of(key.identity()).isDefault()) {
return user;
}
return user.withIdentity(key.identity());
}

@Override
public List<IFlowOperator> findByIds(List<Long> ids) {
return userRepository.findUserByIdIn(ids).stream()
.map(user -> (IFlowOperator) user)
.toList();
public List<IFlowOperator> findByKeys(List<OperatorKey> keys) {
List<Long> ids = keys.stream().map(OperatorKey::operatorId).distinct().toList();
Map<Long, User> users = userRepository.findUserByIdIn(ids).stream()
.collect(Collectors.toMap(User::getUserId, Function.identity()));
List<IFlowOperator> operators = new ArrayList<>();
for (OperatorKey key : keys) {
User user = users.get(key.operatorId());
if (user == null) {
continue;
}
operators.add(OperatorIdentity.of(key.identity()).isDefault()
? user : user.withIdentity(key.identity()));
}
return operators;
}
}
```
Expand Down
50 changes: 17 additions & 33 deletions docs/capabilities/flow-engine-framework/gateway-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ content_hash: 9a35fe960766c3a944ad60043ea71ccd5915e14add45650eed22e6771b95473b

| 组件 | 职责 |
|------|------|
| `FlowOperatorGateway` | 网关接口,定义 `get(userId)` 和 `findByIds(ids)` 方法 |
| `FlowOperatorGateway` | 网关接口,定义 `get(OperatorKey)` 和 `findByKeys(keys)` 方法;默认身份便捷方法由它们派生 |
| `GatewayContext` | 单例上下文,持有 `FlowOperatorGateway` 实现 |
| `FlowOperatorLocalThreadCache` | ThreadLocal 缓存,同请求内缓存操作者信息 |

Expand All @@ -37,48 +37,32 @@ content_hash: 9a35fe960766c3a944ad60043ea71ccd5915e14add45650eed22e6771b95473b

```java
public interface FlowOperatorGateway {
IFlowOperator get(long userId);
List<IFlowOperator> findByIds(List<Long> ids);
IFlowOperator get(OperatorKey key);
List<IFlowOperator> findByKeys(List<OperatorKey> keys);
}
```

> 多身份场景还需覆盖两个 `default` 重载 `get(long, String)` 与 `findByIds(List<Long>, String)`;
> 不覆盖时它们退化为忽略身份的上面两个方法,请求里声明的身份不会写入记录。
> 参与人统一由 `OperatorKey` 表达,批量查询允许同一人员对应多个身份。接口不提供忽略身份的重载;
> 默认身份便捷方法 `get(long)` / `findByIds(List<Long>)` 内部转换为 `OperatorKey.ofDefault(...)`。
> 详见 [多身份参与](../../Integration/operator-identity.md) 3.2。

### 缓存机制

- 每次 `FlowService` 操作前调用 `FlowOperatorLocalThreadCache.clear()` 清理缓存
- 同一请求内,通过 `GatewayContext.getFlowOperator(userId)` 获取操作者时自动缓存
- 批量查询通过 `GatewayContext.findByIds(ids)` 获取,同样走缓存
- 同一请求内,通过 `GatewayContext.getFlowOperator(OperatorKey)` 获取操作者时自动缓存
- 批量查询通过 `GatewayContext.findByKeys(keys)` 获取,同样走缓存;显式非默认身份不一致的结果不会缓存

## 使用实例

```java
// 1. 实现网关接口(在业务项目中)
@Component
public class FlowOperatorGatewayImpl implements FlowOperatorGateway {

@Autowired
private UserRepository userRepository;

@Override
public IFlowOperator get(long userId) {
User user = userRepository.findById(userId);
return new FlowOperator(user.getId(), user.getName(), user.getDepartment());
}

@Override
public List<IFlowOperator> findByIds(List<Long> ids) {
return userRepository.findAllById(ids).stream()
.map(u -> new FlowOperator(u.getId(), u.getName(), u.getDepartment()))
.toList();
}
}
完整可运行的网关实现见 `flow-engine-example/src/main/java/com/codingapi/example/gateway/impl/FlowOperatorGatewayImpl.java`。

// 2. 通过 GatewayContext 获取操作者(自动缓存)
IFlowOperator operator = GatewayContext.getInstance().getFlowOperator(userId);

// 3. 批量获取
List<IFlowOperator> operators = GatewayContext.getInstance().findByIds(userIds);
```java
// 按参与人获取(自动缓存)
IFlowOperator operator = GatewayContext.getInstance().getFlowOperator(
OperatorKey.of(userId, "department1"));

// 混合身份批量获取(自动缓存,按入参顺序返回)
List<IFlowOperator> operators = GatewayContext.getInstance().findByKeys(List.of(
OperatorKey.ofDefault(userId),
OperatorKey.of(userId, "department1")));
```
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class User implements IFlowOperator {
* 当前身份标识(issue #288)。
*
* <p>同一个人员可能隶属多个部门/岗位,以不同身份参与流程。身份是**运行期选择**而非用户主数据,
* 因此不落库({@link Transient});由 {@code FlowOperatorGateway#get(id, identity)} 按身份返回
* 携带该标识的用户副本,示例前端通过请求参数 {@code operatorIdentity} 声明当前身份。
* 因此不落库({@link Transient});由 {@code FlowOperatorGateway#get(OperatorKey)} 按身份返回
* 携带该标识的用户副本,示例前端通过请求头 {@code X-Operator-Identity} 声明当前身份。
* 不参与 {@code equals/hashCode},避免同一 id 的两个身份副本被判定为不同用户。
*/
@Transient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,50 @@
import com.codingapi.flow.gateway.FlowOperatorGateway;
import com.codingapi.flow.operator.IFlowOperator;
import com.codingapi.flow.operator.OperatorIdentity;
import com.codingapi.flow.operator.OperatorKey;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Repository
@AllArgsConstructor
public class FlowOperatorGatewayImpl implements FlowOperatorGateway {

private final UserRepository userRepository;

@Override
public IFlowOperator get(long id) {
return userRepository.getUserById(id);
}

/**
* 按身份获取操作者(issue #288)。
*
* <p>示例组织模型里身份就是「以哪个部门/岗位的身份参与流程」,不属于用户主数据,
* 因此这里返回携带身份标识的**用户副本**,而不改写持久化上下文中的实体实例。
* 真实项目应把身份替换为自己的组织模型(例如按部门 id 查询岗位任职关系)。
* 身份不属于用户主数据,因此返回携带身份的副本,不改写持久化实体。
*/
@Override
public IFlowOperator get(long id, String identity) {
if (OperatorIdentity.of(identity).isDefault()) {
return get(id);
}
User user = userRepository.getUserById(id);
return user == null ? null : user.withIdentity(OperatorIdentity.of(identity).value());
public IFlowOperator get(OperatorKey key) {
User user = userRepository.getUserById(key.operatorId());
return withIdentity(user, key.identity());
}

@Override
public List<IFlowOperator> findByIds(List<Long> ids) {
return userRepository.findUserByIdIn(ids)
.stream()
.map(user -> (IFlowOperator) user)
.toList();
public List<IFlowOperator> findByKeys(List<OperatorKey> keys) {
List<Long> ids = keys.stream().map(OperatorKey::operatorId).distinct().toList();
Map<Long, User> usersById = userRepository.findUserByIdIn(ids).stream()
.collect(Collectors.toMap(User::getUserId, Function.identity()));
List<IFlowOperator> operators = new ArrayList<>();
for (OperatorKey key : keys) {
IFlowOperator operator = withIdentity(usersById.get(key.operatorId()), key.identity());
if (operator != null) {
operators.add(operator);
}
}
return operators;
}

/**
* 按统一身份批量获取操作者(issue #288 衍生 / #293)。
*
* <p>批量重载同样必须返回携带身份的副本:只覆盖单条 {@code get(id, identity)} 时,
* 流程树、人员设定等批量路径会把身份退化为默认身份。
*/
@Override
public List<IFlowOperator> findByIds(List<Long> ids, String identity) {
if (OperatorIdentity.of(identity).isDefault()) {
return findByIds(ids);
private IFlowOperator withIdentity(User user, String identity) {
if (user == null) {
return null;
}
String normalized = OperatorIdentity.of(identity).value();
return userRepository.findUserByIdIn(ids)
.stream()
.map(user -> (IFlowOperator) user.withIdentity(normalized))
.toList();
return OperatorIdentity.of(identity).isDefault() ? user : user.withIdentity(identity);
}

}
Loading
Loading