P2-4: 多维数据权限全形态实测——R1 风险判定(引擎多维隔离语义成立)

普查(客户库只读):1079 条活跃规则归型——多规则同实体 303/345(角色 OR 88%)、
多条件 op 串联 117、type 分布 CURRENTUSERORGID 323/OWNNERPROJECT 165/CURRENTUSERDEPARTID 27、
SPECIALDEPART/ORG 客户零用;OwnProjectWhere SysConfig 实际值取证。

实测(本地 eip4m0,可写):bootstrap-multidim.sql 构造 7 受控实体 + 规则矩阵,
MultiDimPrivilegeRunner + ParityCli --multidim 多维 ctx 驱动引擎过滤 vs JDBC 等价 WHERE
逐行对拍——14 形态 100% 一致(39 行)+ 7/7 负例被拒。覆盖部门/组织/SPECIALDEPART/
OWNNERPROJECT 单维、op=AND 串联、多规则角色 OR 三态、建筑三维 OWNER×部门×项目。

客户库抽测(只读,P2-1 口径):DM_FILE(299)/SYS_USER(806)组织维 count+行级 MATCH + 负例被拒。

测试通道:EntityController 加 asDepart/asOrg(与 asUser 同性质,不传零回归),
使引擎可解析 CURRENTUSERDEPARTID/CURRENTUSERORGID 维度。

单测:MultiDimDataPrivilegeTest 9 测(getWhereClause 拼接矩阵,FakePOInfo 模式)。
全模块 test + ArchUnit 绿。手册新增 §5.34/§5.35。

R1 判定:引擎多维隔离语义成立,不构成 P2 阻塞;缺口(OWNNERPROJECT 客户库直拍/
生产 ctx 注入/SPECIALDEPART 无真实样本)如实登记跟踪。未触发决策会 S1。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 14:05:09 +08:00
parent 82f52c1114
commit b6de637905
8 changed files with 1285 additions and 11 deletions

View File

@@ -0,0 +1,263 @@
package com.dsasp.server.privilege;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.dsasp.server.common.dataDic.bean.AbstractPOJOInfo;
import com.dsasp.server.common.exception.PrivilegeException;
import com.dsasp.server.common.privilege.bean.Conditional;
import com.dsasp.server.common.privilege.bean.DataFilterRule;
import com.dsasp.server.common.user.service.LoggedInUser;
import com.dsasp.server.privilege.service.DataPrivilegeServiceImpl;
/**
* P2-4 多维数据权限单测:覆盖 {@code DataPrivilegeServiceImpl.getWhereClause} 的<b>多维 + 多条件 +
* 多规则</b>拼接矩阵(部门 / 组织 / 指定部门集 / op=AND/OR 串联 / 多规则角色 OR)。
*
* <p>沿用 {@link DataPrivilegeTest} 的 FakePOInfo 反射注入 + 纯 ctx 模式(绕开 DB/Spring)。
* 仅用 CURRENTUSERDEPART / CURRENTUSERORG / SPECIALDEPART(convertSplitStrToSql,纯函数)+ 等值/in,
* <b>不触达 SysConfig / OWNNERPROJECT</b>(那两者需 Spring 上下文,留给 --multidim 集成对拍验)。
* 语义与 framework 原版逐字一致。
*/
class MultiDimDataPrivilegeTest {
private final DataPrivilegeServiceImpl service = new DataPrivilegeServiceImpl();
@BeforeEach
@SuppressWarnings("unchecked")
void clearCache() throws Exception {
Field f = AbstractPOJOInfo.class.getDeclaredField("s_cache");
f.setAccessible(true);
((Map<String, ?>) f.get(null)).clear();
}
@SuppressWarnings("unchecked")
private void putPoInfo(String table, FakePOInfo info) throws Exception {
Field f = AbstractPOJOInfo.class.getDeclaredField("s_cache");
f.setAccessible(true);
((Map<String, FakePOInfo>) f.get(null)).put(table, info);
}
private static Conditional cond(String col, String sign, String type, String back, String op) {
Conditional c = new Conditional();
c.setPreConditional(col);
c.setSign(sign);
c.setType(type);
c.setBackConditional(back);
c.setOp(op);
return c;
}
private static DataFilterRule rule(List<String> roles, Conditional... conds) {
DataFilterRule r = new DataFilterRule();
r.setId("rule-" + roles);
r.setRoles(roles);
r.setConditionalList(Arrays.asList(conds));
return r;
}
/** 多维 ctx:user / depart / org 任意可空。 */
private static Properties ctx(String user, String depart, String org, String... roles) {
Properties p = new Properties();
p.setProperty(LoggedInUser.CURRENTUSER, user);
if (depart != null) {
p.setProperty(LoggedInUser.CURRENTDEPART, depart);
}
if (org != null) {
p.setProperty(LoggedInUser.CURRENTORG, org);
}
List<String> roleList = new ArrayList<>(Arrays.asList(roles));
p.put("ROLES", roleList);
return p;
}
// ---- 单维:部门 / 组织 ----
@Test
void whereClause_currentUserDepart_singleDim() throws Exception {
FakePOInfo info = new FakePOInfo("EIP4PT_MDDEPT", "部门台账",
List.of(rule(List.of("ROLE_DEPT"),
cond("DEPARTID", "=", Conditional.CURRENTUSERDEPART, "{CURRENTUSERDEPARTID}", null))));
putPoInfo("EIP4PT_MDDEPT", info);
String where = service.getWhereClause(ctx("U1", "DEP_1", null, "ROLE_DEPT"), "EIP4PT_MDDEPT");
assertNotNull(where);
assertTrue(where.contains("P.DEPARTID"), where);
assertTrue(where.contains("='DEP_1'"), where);
}
@Test
void whereClause_currentUserOrg_singleDim() throws Exception {
FakePOInfo info = new FakePOInfo("EIP4PT_MDORG", "组织台账",
List.of(rule(List.of("ROLE_ORG"),
cond("ORGID", "=", Conditional.CURRENTUSERORG, "{CURRENTUSERORGID}", null))));
putPoInfo("EIP4PT_MDORG", info);
String where = service.getWhereClause(ctx("U1", null, "ORG_2", "ROLE_ORG"), "EIP4PT_MDORG");
assertNotNull(where);
assertTrue(where.contains("P.ORGID"), where);
assertTrue(where.contains("='ORG_2'"), where);
}
// ---- SPECIALDEPART:指定部门集(强制 in,convertSplitStrToSql) ----
@Test
void whereClause_specialDepart_forcedInClause() throws Exception {
// sign 写 '=' 也应被引擎强制为 in(...);多值用逗号分隔
FakePOInfo info = new FakePOInfo("EIP4PT_MDSPECDEPT", "指定部门",
List.of(rule(List.of("ROLE_SPEC"),
cond("DEPARTID", "=", Conditional.SPECIALDEPART, "DEP_1,DEP_2", null))));
putPoInfo("EIP4PT_MDSPECDEPT", info);
String where = service.getWhereClause(ctx("U1", "DEP_9", null, "ROLE_SPEC"), "EIP4PT_MDSPECDEPT");
assertNotNull(where);
assertTrue(where.contains("P.DEPARTID"), where);
// 引擎 SIGN_CONTAIN="in" 直接拼,后随 "(...)"(无空格):P.DEPARTID in('DEP_1','DEP_2')
assertTrue(where.contains("in('DEP_1','DEP_2')"), where);
// 强制 in:不应出现等号比较单值
assertFalse(where.contains("='DEP_1,DEP_2'"), where);
}
// ---- 多条件 op 串联(单规则内) ----
@Test
void whereClause_multiConditional_opAnd() throws Exception {
FakePOInfo info = new FakePOInfo("EIP4PT_MDCOMBO", "部门项目",
List.of(rule(List.of("ROLE_COMBO"),
cond("DEPARTID", "=", Conditional.CURRENTUSERDEPART, "{CURRENTUSERDEPARTID}", "AND"),
cond("ORGID", "=", Conditional.CURRENTUSERORG, "{CURRENTUSERORGID}", null))));
putPoInfo("EIP4PT_MDCOMBO", info);
String where = service.getWhereClause(ctx("U1", "DEP_1", "ORG_1", "ROLE_COMBO"), "EIP4PT_MDCOMBO");
assertNotNull(where);
assertTrue(where.contains("P.DEPARTID"), where);
assertTrue(where.contains("='DEP_1'"), where);
assertTrue(where.contains(" AND "), where);
assertTrue(where.contains("P.ORGID"), where);
assertTrue(where.contains("='ORG_1'"), where);
}
@Test
void whereClause_multiConditional_opOr() throws Exception {
FakePOInfo info = new FakePOInfo("EIP4PT_MDOR2", "会议纪要",
List.of(rule(List.of("ROLE_M"),
cond("COMPERE", "=", Conditional.CURRENTUSER, "{CURRENTUSERID}", "OR"),
cond("CLERK", "=", Conditional.CURRENTUSER, "{CURRENTUSERID}", null))));
putPoInfo("EIP4PT_MDOR2", info);
String where = service.getWhereClause(ctx("U7", null, null, "ROLE_M"), "EIP4PT_MDOR2");
assertNotNull(where);
assertTrue(where.contains("P.COMPERE"), where);
assertTrue(where.contains("P.CLERK"), where);
// 单规则内 op=OR 连接两个条件(getFilterSql 内层 OR,非多规则间 OR)
assertTrue(where.contains(" OR "), where);
assertTrue(where.contains("='U7'"), where);
}
// ---- 多规则角色 OR(同实体两条规则) ----
@Test
void whereClause_multiRule_roleOr_bothMatched() throws Exception {
FakePOInfo info = new FakePOInfo("EIP4PT_SUBCONTRACT", "分包商台账",
List.of(
rule(List.of("ROLE_OR_A"),
cond("OWNER", "=", Conditional.CURRENTUSER, "{CURRENTUSERID}", null)),
rule(List.of("ROLE_OR_B"),
cond("DEPARTID", "=", Conditional.CURRENTUSERDEPART, "{CURRENTUSERDEPARTID}", null))));
putPoInfo("EIP4PT_SUBCONTRACT", info);
// 同时持有两角色 → 两条规则子句以 OR 合并
String where = service.getWhereClause(
ctx("USER_PM", "DEP_1", null, "ROLE_OR_A", "ROLE_OR_B"), "EIP4PT_SUBCONTRACT");
assertNotNull(where);
assertTrue(where.startsWith(" AND ("), where);
assertTrue(where.contains("P.OWNER"), where);
assertTrue(where.contains("='USER_PM'"), where);
assertTrue(where.contains(" OR "), where);
assertTrue(where.contains("P.DEPARTID"), where);
assertTrue(where.contains("='DEP_1'"), where);
}
@Test
void whereClause_multiRule_roleOr_onlyOneMatched() throws Exception {
FakePOInfo info = new FakePOInfo("EIP4PT_SUBCONTRACT", "分包商台账",
List.of(
rule(List.of("ROLE_OR_A"),
cond("OWNER", "=", Conditional.CURRENTUSER, "{CURRENTUSERID}", null)),
rule(List.of("ROLE_OR_B"),
cond("DEPARTID", "=", Conditional.CURRENTUSERDEPART, "{CURRENTUSERDEPARTID}", null))));
putPoInfo("EIP4PT_SUBCONTRACT", info);
// 只持有 ROLE_OR_B → 只拼部门子句,无 OWNER 子句、无规则间 OR
String where = service.getWhereClause(
ctx("USER_X", "DEP_2", null, "ROLE_OR_B"), "EIP4PT_SUBCONTRACT");
assertNotNull(where);
assertTrue(where.contains("P.DEPARTID"), where);
assertTrue(where.contains("='DEP_2'"), where);
assertFalse(where.contains("P.OWNER"), where);
}
@Test
void whereClause_multiRule_roleOr_noneMatched_throws() throws Exception {
FakePOInfo info = new FakePOInfo("EIP4PT_SUBCONTRACT", "分包商台账",
List.of(
rule(List.of("ROLE_OR_A"),
cond("OWNER", "=", Conditional.CURRENTUSER, "{CURRENTUSERID}", null)),
rule(List.of("ROLE_OR_B"),
cond("DEPARTID", "=", Conditional.CURRENTUSERDEPART, "{CURRENTUSERDEPARTID}", null))));
putPoInfo("EIP4PT_SUBCONTRACT", info);
// 无任何命中角色 → first 分支抛 PrivilegeException
PrivilegeException ex = assertThrows(PrivilegeException.class,
() -> service.getWhereClause(ctx("USER_Z", "DEP_9", null, "ROLE_NOBODY"), "EIP4PT_SUBCONTRACT"));
assertTrue(ex.getMessage().contains("分包商台账"), ex.getMessage());
}
// ---- 建筑三维:单规则 3 条件 AND 串联(负责人 OWNER AND 部门 AND 组织,均纯类型) ----
@Test
void whereClause_threeDim_andChain() throws Exception {
// 三维全用纯类型(CURRENTUSER/DEPART/ORG),不触达 SysConfig;验证 3 条件 op=AND 串联拼接
FakePOInfo info = new FakePOInfo("EIP4PT_SUBCONTRACT", "分包商台账",
List.of(rule(List.of("ROLE_SUB"),
cond("OWNER", "=", Conditional.CURRENTUSER, "{CURRENTUSERID}", "AND"),
cond("DEPARTID", "=", Conditional.CURRENTUSERDEPART, "{CURRENTUSERDEPARTID}", "AND"),
cond("ORGID", "=", Conditional.CURRENTUSERORG, "{CURRENTUSERORGID}", null))));
putPoInfo("EIP4PT_SUBCONTRACT", info);
String where = service.getWhereClause(
ctx("USER_PM", "DEP_1", "ORG_1", "ROLE_SUB"), "EIP4PT_SUBCONTRACT");
assertNotNull(where);
assertTrue(where.contains("P.OWNER"), where);
assertTrue(where.contains("='USER_PM'"), where);
assertTrue(where.contains("P.DEPARTID"), where);
assertTrue(where.contains("='DEP_1'"), where);
assertTrue(where.contains("P.ORGID"), where);
assertTrue(where.contains("='ORG_1'"), where);
// 三条件链 → 2 个 op=AND 连接;外层 getWhereClause 还有 " AND ( " 包裹(共 3 处 " AND ")
assertEquals(3, countOccurrences(where, " AND "), where);
}
private static int countOccurrences(String haystack, String needle) {
int n = 0;
int idx = 0;
while ((idx = haystack.indexOf(needle, idx)) >= 0) {
n++;
idx += needle.length();
}
return n;
}
}