fix(flow): restore workflow task converted pages

This commit is contained in:
2026-06-17 20:44:52 +08:00
parent 5946afa6e6
commit 57c3412aff
6 changed files with 689 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -284,7 +285,7 @@ public class FlowController {
yield forbiddenWriteViaGet(id);
}
Properties ctx = UserSession.getAdmin();
Object output = flowExcuteService.excute(map, ctx, id);
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, id));
yield ok(output, null);
}
case NAMED_READ -> {
@@ -297,7 +298,7 @@ public class FlowController {
inputCoercer.coerceSearchDatesByFlowId(id, map);
}
Properties ctx = UserSession.getAdmin();
Object output = flowExcuteService.excute(map, ctx, id);
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, id));
yield ok(output, flowNameResolver.entityTableForNamedFlow(id));
}
case NAMED_WRITE -> {
@@ -316,17 +317,20 @@ public class FlowController {
? inputCoercer.missingRequiredIdInput(id, map) : null;
if (pkInput != null) {
Properties fanoutCtx = currentCtx();
List<Object> results = new ArrayList<>();
for (String one : ((String) map.get("ids")).split(",")) {
if (one.isBlank()) {
continue;
List<Object> results = withEngineContext(fanoutCtx, () -> {
List<Object> fanoutResults = new ArrayList<>();
for (String one : ((String) map.get("ids")).split(",")) {
if (one.isBlank()) {
continue;
}
Map<String, Object> per = new HashMap<>(map);
per.remove("ids");
per.put(pkInput, one.trim());
inputCoercer.coerceByFlowId(id, per);
fanoutResults.add(flowExcuteService.excute(per, fanoutCtx, id));
}
Map<String, Object> per = new HashMap<>(map);
per.remove("ids");
per.put(pkInput, one.trim());
inputCoercer.coerceByFlowId(id, per);
results.add(flowExcuteService.excute(per, fanoutCtx, id));
}
return fanoutResults;
});
Map<String, Object> agg = new LinkedHashMap<>();
agg.put("count", results.size());
agg.put("results", results);
@@ -339,7 +343,7 @@ public class FlowController {
inputCoercer.coerceByFlowId(id, map);
}
Properties ctx = currentCtx();
Object output = flowExcuteService.excute(map, ctx, id);
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, id));
yield ok(output, null);
}
case SYNTHESIZED -> {
@@ -352,7 +356,7 @@ public class FlowController {
if (inputCoercer != null) {
inputCoercer.coerceSearchDatesByTable(tableFromSearchKey(flowKey), map);
}
Object output = flowExcuteService.excute(map, ctx, flowKey);
Object output = withEngineContext(ctx, () -> flowExcuteService.excute(map, ctx, flowKey));
// 合成搜索流的 key = <TABLE>_SEARCH,剥后缀即数据源实体表名,用于用户引用列 id→姓名解析
yield ok(output, tableFromSearchKey(flowKey));
}
@@ -385,6 +389,26 @@ public class FlowController {
return UserSession.getAdmin();
}
/**
* 保真层脚本除接收 {@code ctx} 参数外,还会直接调用 {@link UserSession#get()}。
* JWT 请求已由 {@code JwtAuthFilter} 绑定 ThreadLocal;无 JWT 的本地/巡检调用则在此临时绑定
* 本次执行上下文,并在返回后清理,避免脚本 NPE 且不污染 Tomcat 复用线程。
*/
private static <T> T withEngineContext(Properties ctx, Supplier<T> action) {
boolean fallbackBound = false;
if (UserSession.get() == null && ctx != null) {
UserSession.set(ctx);
fallbackBound = true;
}
try {
return action.get();
} finally {
if (fallbackBound) {
UserSession.remove();
}
}
}
private ResponseEntity<Map<String, Object>> ok(Object output, String table) {
Object data = unwrapCrudResult(normalize(output));
// read-path issue D1=603:把元数据标记为用户引用的列 id/工号 → 姓名(仅在已知数据源实体的合成搜索流上)

View File

@@ -154,6 +154,58 @@ class FlowControllerTest {
assertNotNull(resp.getBody().get("data"));
}
@Test
@DisplayName("4b. 无 JWT 上下文时,读流执行期间临时绑定 ADMIN UserSession,执行后清理")
void readFlowBindsAdminUserSessionWhenMissing() {
String flowId = "VIEW_ACT_RU_TASK_SEARCH";
UserSession.remove();
try {
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(Properties.class), eq(flowId))).thenAnswer(inv -> {
Properties bound = UserSession.get();
assertNotNull(bound, "老脚本会直接 UserSession.get(),执行期间必须有上下文");
assertEquals(LoggedInUser.ADMIN, bound.getProperty(LoggedInUser.CURRENTUSER));
return new HashMap<>();
});
FlowController controller = new FlowController(engine, resolverReturning(flowId, namedHit()));
ResponseEntity<Map<String, Object>> resp = controller.executeGet(flowId, new HashMap<>());
assertEquals(HttpStatus.OK, resp.getStatusCode());
assertEquals(0, resp.getBody().get("status"));
assertNull(UserSession.get(), "临时绑定必须在请求结束后清理,避免污染复用线程");
} finally {
UserSession.remove();
}
}
@Test
@DisplayName("4c. 已有 JWT UserSession 时,读流不覆盖当前用户上下文")
void readFlowKeepsExistingUserSession() {
String flowId = "VIEW_ACT_RU_TASK_SEARCH";
Properties realUser = new Properties();
realUser.setProperty(LoggedInUser.CURRENTUSER, "USER-1");
UserSession.set(realUser);
try {
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(Properties.class), eq(flowId))).thenAnswer(inv -> {
org.junit.jupiter.api.Assertions.assertSame(realUser, UserSession.get(),
"JWT 绑定的真实用户上下文不能被 ADMIN 兜底覆盖");
return new HashMap<>();
});
FlowController controller = new FlowController(engine, resolverReturning(flowId, namedHit()));
ResponseEntity<Map<String, Object>> resp = controller.executeGet(flowId, new HashMap<>());
assertEquals(HttpStatus.OK, resp.getStatusCode());
assertEquals(0, resp.getBody().get("status"));
org.junit.jupiter.api.Assertions.assertSame(realUser, UserSession.get(),
"已有上下文由 JwtAuthFilter 负责 finally 清理,FlowController 不能提前移除");
} finally {
UserSession.remove();
}
}
@Test
@DisplayName("5. listSelect 结果归一化:{listresult,pagecount} → {items,total}")
void normalizeListSelectResult() {