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
27 changes: 21 additions & 6 deletions docs/Integration/operator-identity.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,36 @@ public IFlowOperator get(long id, String identity) {

### 3.3 请求声明身份

REST 接口支持 `operatorIdentity` 参数(与 `operatorId` 对称):未传时取当前登录操作者的身份;若同时用 `operatorId`
覆盖了人员,身份归默认身份(避免出现「人员与身份错配」的副本)。
REST 接口通过**请求头** `X-Operator-Identity` 声明当前身份(issue #301):未传时取当前登录操作者的身份;
若同时用 `operatorId` 覆盖了人员,身份归默认身份(避免出现「人员与身份错配」的副本)。
显式选择「默认身份」时传空值(`X-Operator-Identity:`),与「未携带」区分。

> ⚠️ **不要用 query 参数 `operatorIdentity` 传身份**:引擎的部分查询接口(如 `/api/query/workflow/list`)
> 用 `SearchRequest` 接收请求,会把所有 query 参数当作实体过滤字段解析,多一个 `operatorIdentity` 就会抛
> `Field operatorIdentity not found in class hierarchy`,页面直接 500。
> 为兼容旧调用方,服务端仍保留 query 读取(非 `SearchRequest` 接口可用),但请求头优先。

```bash
# 以 department1 的身份发起(身份随记录落库:create_operator_identity)
curl -X POST 'http://host/api/cmd/record/create?operatorIdentity=department1' \
curl -X POST 'http://host/api/cmd/record/create' \
-H 'X-Operator-Identity: department1' \
-H 'Content-Type: application/json' -d '{...}'

# 查看待办:默认跨身份,operatorIdentity 只是「本次操作身份」上下文,不影响返回范围
curl 'http://host/api/query/record/todo?operatorId=1&operatorIdentity=department1'
# 查看待办:默认跨身份,X-Operator-Identity 只是「本次操作身份」上下文,不影响返回范围
curl 'http://host/api/query/record/todo?operatorId=1' -H 'X-Operator-Identity: department1'

# 只想看「落在 department1 下」的待办时,显式锁定身份(见 4.3)
curl 'http://host/api/query/record/todo?operatorId=1&operatorIdentity=department1&identityScope=LOCKED'
curl 'http://host/api/query/record/todo?operatorId=1' \
-H 'X-Operator-Identity: department1' \
-H 'X-Identity-Scope: LOCKED'
```

| 请求头 | 取值 | 说明 |
|---|---|---|
| `X-Operator-Identity` | 身份标识 / 空串 | 当前操作/查看的身份,空串表示显式默认身份 |
| `X-Identity-Scope` | `ALL` / `LOCKED` | 查询范围:默认跨身份,`LOCKED` 只看当前身份 |
| `X-Lock-Identity` | `true` / `false` | `X-Identity-Scope` 的等价简写 |

编程式 API 中对应各请求对象的 `operatorIdentity` 字段:

| 请求对象 | 字段 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,45 @@ public final class OperatorIdentity {
*/
public static final String KEY_SEPARATOR = "#";

/**
* 当前身份的请求头名称(issue #301)。
*
* <p>身份是请求上下文,不应放在 query 中:引擎的查询接口用 {@code SearchRequest} 接收请求,
* 会把所有 query 参数当作实体过滤字段解析,多一个 {@code operatorIdentity} 就会抛
* {@code Field operatorIdentity not found in class hierarchy}。
*
* <p>为兼容旧前端与 curl 直连,服务端仍保留 query 读取,但请求头优先。
*/
public static final String HEADER_OPERATOR_IDENTITY = "X-Operator-Identity";

/**
* 查询身份范围的请求头名称,取值 {@code ALL} / {@code LOCKED}(issue #301)
*/
public static final String HEADER_IDENTITY_SCOPE = "X-Identity-Scope";

/**
* 是否锁定身份查询的请求头名称,取值 {@code true} / {@code false}(issue #301)
*/
public static final String HEADER_LOCK_IDENTITY = "X-Lock-Identity";

private OperatorIdentity() {
}

/**
* 选取身份原始值:请求头优先,其次请求参数(issue #301)。
*
* @param headerValue 请求头中的原始值,未携带时为 {@code null}
* @param parameterValue 请求参数中的原始值
* @param parameterPresent 请求参数是否显式出现(区分「未传」与「传了空串」)
* @return 原始值;两者都未携带时返回 {@code null}
*/
public static String pick(String headerValue, String parameterValue, boolean parameterPresent) {
if (headerValue != null) {
return headerValue;
}
return parameterPresent ? parameterValue : null;
}

/**
* 归一化身份标识:null 转为空串,其余原样返回。
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.codingapi.flow.operator;

import com.codingapi.flow.exception.FlowValidationException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* 身份取值优先级测试(issue #301)。
*
* <p>背景:身份原先只从 query 参数读取,而引擎查询接口用 {@code SearchRequest} 接收请求,
* 会把 query 参数当成实体过滤字段,导致
* {@code Field operatorIdentity not found in class hierarchy}。
* 改为请求头优先,同时保留 query 兼容。
*/
class OperatorIdentityHeaderResolveTest {

@Test
void should_prefer_header_over_parameter() {
// given:请求头与 query 同时携带,且取值不同
String header = "department1";
String parameter = "department2";

// when
String raw = OperatorIdentity.pick(header, parameter, true);

// then:请求头优先
assertEquals("department1", OperatorIdentity.validate(raw));
}

@Test
void should_fallback_to_parameter_when_header_absent() {
// given:仅 query 携带(旧前端 / curl 直连)
// when
String raw = OperatorIdentity.pick(null, "department1", true);

// then
assertEquals("department1", OperatorIdentity.validate(raw));
}

@Test
void should_return_null_when_neither_present() {
// given / when:两者都未携带,交由调用方回退到登录用户身份
String raw = OperatorIdentity.pick(null, null, false);

// then
assertNull(raw);
}

@Test
void should_treat_empty_header_as_default_identity() {
// given:显式选择「默认身份」时请求头为空串,需要与「未携带」区分
// when
String raw = OperatorIdentity.pick("", "department1", true);

// then:空串表示默认身份,不能回退到 query
assertEquals(OperatorIdentity.DEFAULT, OperatorIdentity.validate(raw));
}

@Test
void should_reject_invalid_identity_from_header() {
// given:请求头携带非法身份(包含合并键分隔符)
// when / then:入口即拒绝,避免污染落库与缓存键
String raw = OperatorIdentity.pick("department#1", null, false);
assertThrows(FlowValidationException.class, () -> OperatorIdentity.validate(raw));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ private long loadCurrentOperatorId() {
}

/**
* 当前操作人的身份标识(issue #288)。
* 当前操作人的身份标识(issue #288 / #301)。
*
* <p>与 {@link #loadCurrentOperatorId()} 对称:优先取请求参数 {@code operatorIdentity},
* 其次取当前登录操作者自身的身份。身份是会话级信息,因此统一在这里解析后注入各请求对象,
* 避免每个接口各自解析导致行为不一致。
* <p>与 {@link #loadCurrentOperatorId()} 对称:优先取请求头 {@code X-Operator-Identity},
* 其次取请求参数 {@code operatorIdentity}(兼容旧前端与 curl 直连),最后取当前登录操作者自身的身份。
* 身份是会话级信息,因此统一在这里解析后注入各请求对象,避免每个接口各自解析导致行为不一致。
*
* <p>之所以以请求头优先:查询接口用 {@code SearchRequest} 接收请求,会把 query 参数当作实体
* 过滤字段解析,身份放在 query 会让列表接口直接 500(issue #301)。
*/
private String loadCurrentOperatorIdentity() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = attributes.getRequest();
if (request.getParameterMap().containsKey("operatorIdentity")) {
String raw = OperatorIdentity.pick(
request.getHeader(OperatorIdentity.HEADER_OPERATOR_IDENTITY),
request.getParameter("operatorIdentity"),
request.getParameterMap().containsKey("operatorIdentity"));
if (raw != null) {
// 入口即校验:长度 / 分隔符 / 控制字符不合法时直接拒绝,避免拖到落库阶段才失败
return OperatorIdentity.validate(request.getParameter("operatorIdentity"));
return OperatorIdentity.validate(raw);
}
// 覆盖了 operatorId(联调、无登录态场景)时不再继承登录用户的身份,
// 否则会得到「目标人员 + 当前登录人身份」的错配组合(issue #288 评审修复)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,27 @@ private long loadCurrentOperatorId(){
}

/**
* 当前操作人身份标识(issue #288 / #297)。
* 当前操作人身份标识(issue #288 / #297 / #301)。
*
* <p>优先取请求参数 {@code operatorIdentity}(宿主声明「我以哪个身份操作/查看」),
* 其次取当前登录操作者自身的身份,都为空时表示默认身份。
* <p>优先取请求头 {@code X-Operator-Identity},其次取请求参数 {@code operatorIdentity}
* (兼容旧前端与 curl 直连),最后取当前登录操作者自身的身份,都为空时表示默认身份。
*
* <p>以请求头优先的原因:本控制器的列表接口用 {@code SearchRequest} 接收请求,会把 query
* 参数当作实体过滤字段解析,身份放在 query 会让待办/已办/抄送/列表接口直接 500(issue #301)。
*
* <p>该值只作为身份上下文使用:它不会隐式改变查询范围——查询是否按身份过滤由
* {@code identityScope} 显式控制,因此宿主始终携带它也不会导致待办「消失」。
*/
private String loadCurrentOperatorIdentity(){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = attributes.getRequest();
if(request.getParameterMap().containsKey("operatorIdentity")) {
String raw = OperatorIdentity.pick(
request.getHeader(OperatorIdentity.HEADER_OPERATOR_IDENTITY),
request.getParameter("operatorIdentity"),
request.getParameterMap().containsKey("operatorIdentity"));
if(raw != null) {
// 入口即校验,与命令侧口径一致(issue #288 评审修复)
return OperatorIdentity.validate(request.getParameter("operatorIdentity"));
return OperatorIdentity.validate(raw);
}
// 覆盖了 operatorId(联调、无登录态场景)时不再继承登录用户的身份,避免人员与身份错配
if(StringUtils.hasText(request.getParameter("operatorId"))) {
Expand All @@ -95,13 +102,23 @@ private String loadCurrentOperatorIdentity(){
*
* <p>注意:{@code operatorIdentity} 只表示「本次操作/查看的身份上下文」,
* 不再隐式决定查询范围。宿主仍然可以一直携带它,不会影响返回结果。
*
* <p>参数优先取请求头({@code X-Identity-Scope} / {@code X-Lock-Identity}),
* 避免被 {@code SearchRequest} 当成实体字段解析(issue #301)。
*/
private boolean isIdentityLocked() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String scope = request.getParameter(IDENTITY_SCOPE_PARAM);
String scope = OperatorIdentity.pick(
request.getHeader(OperatorIdentity.HEADER_IDENTITY_SCOPE),
request.getParameter(IDENTITY_SCOPE_PARAM),
request.getParameterMap().containsKey(IDENTITY_SCOPE_PARAM));
if (!StringUtils.hasText(scope)) {
return "true".equalsIgnoreCase(request.getParameter(LOCK_IDENTITY_PARAM));
String locked = OperatorIdentity.pick(
request.getHeader(OperatorIdentity.HEADER_LOCK_IDENTITY),
request.getParameter(LOCK_IDENTITY_PARAM),
request.getParameterMap().containsKey(LOCK_IDENTITY_PARAM));
return "true".equalsIgnoreCase(locked);
}
String normalized = scope.trim().toUpperCase();
if (IDENTITY_SCOPE_ALL.equals(normalized)) {
Expand Down
Loading