feat(menu): admin/超管菜单见全部(管理列表 ddb75614 可达)
WS-A:MenuController 对超管(userId trim/ignoreCase == LoggedInUser.ADMIN) bypass view_sys_usermenuid 裁剪——可见集 = 全部 isactive 叶子菜单,与老框架 ViewMenuServiceImpl(if !ADMIN.equals(userId) 才 JOIN usermenuid) 语义一致。 修复 admin 看不到「新闻管理」管理列表叶子(b6d6be5a→ddb75614)的缺口。 - MenuController.isAdmin(userId) 复用 WritePrivilegeChecker 同口径判定。 - MenuService.allLeafMenuIds(menus) 纯函数:收集全部叶子 id 供超管可见集。 - 普通用户路径不变:仍按 view_sys_usermenuid(userid=?) 参数化裁剪。 测试(17 绿): - MenuControllerTest(6):超管见 b6d6be5a 且不查 usermenuid;大小写/补白容错; 普通用户仍裁剪 b6d6be5a 不下发;无菜单空 roots;未登录空 roots;isAdmin 判定。 - MenuServiceTest 新增 3 例 allLeafMenuIds + 保留原 8 例(非 admin 剪枝不破)。 运行时实证:admin /api/menu 可见叶子 147→807(=全部 isactive 叶子); 浏览器 信息站→新闻管理 出现「新闻管理」管理列表叶子,点开 /page/ddb75614 渲染列表(281 项)+完整按钮栏(添加/修改/查看/删除/发布/显示/隐藏/置顶/ 文件归档/取消置顶/提交审批)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -98,6 +98,33 @@ public class MenuService {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算全部叶子菜单 id 集合(parentid 非空者为叶子)。
|
||||
*
|
||||
* <p>用于超管 bypass:可见集 = 全部 isactive 叶子,不裁剪。目录节点(parentid 为空)
|
||||
* 不计入——目录的下发由 {@link #buildTree} 按「子树内存在可见叶子」自然推导。
|
||||
*
|
||||
* @param menus sys_menu isactive=1 全量平铺行
|
||||
* @return 全部叶子的 id 集合(已 trim、去 null/空)
|
||||
*/
|
||||
public static Set<String> allLeafMenuIds(List<Map<String, Object>> menus) {
|
||||
Set<String> ids = new java.util.HashSet<>();
|
||||
if (menus == null) {
|
||||
return ids;
|
||||
}
|
||||
for (Map<String, Object> menu : menus) {
|
||||
String parentId = trim(menu.get("parentid"));
|
||||
if (parentId == null || parentId.isEmpty()) {
|
||||
continue; // 目录层,跳过
|
||||
}
|
||||
String id = trim(menu.get("id"));
|
||||
if (id != null && !id.isEmpty()) {
|
||||
ids.add(id);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/** 构建叶子节点 Map(templateId = view_templateid)。 */
|
||||
private Map<String, Object> buildLeafNode(Map<String, Object> leaf) {
|
||||
Map<String, Object> node = new LinkedHashMap<>();
|
||||
|
||||
@@ -97,13 +97,22 @@ public class MenuController {
|
||||
return m;
|
||||
});
|
||||
|
||||
// 4. 查询当前用户可见叶子菜单 id 集合(参数化,防注入)
|
||||
// 4. 计算当前用户可见叶子菜单 id 集合
|
||||
// 超管(ADMIN):bypass 裁剪,可见集 = 全部 isactive 叶子菜单(与老框架
|
||||
// ViewMenuServiceImpl 一致——admin 不 JOIN view_sys_usermenuid,见全部菜单)。
|
||||
// 普通用户:按 view_sys_usermenuid(userid=当前用户) 裁剪。
|
||||
Set<String> visibleMenuIds;
|
||||
if (isAdmin(userId)) {
|
||||
visibleMenuIds = MenuService.allLeafMenuIds(menus);
|
||||
} else {
|
||||
// 参数化查询,防注入
|
||||
List<String> visibleList = jdbcTemplate.queryForList(
|
||||
"SELECT trim(menuid) FROM view_sys_usermenuid WHERE userid = ?",
|
||||
String.class, userId);
|
||||
Set<String> visibleMenuIds = visibleList.stream()
|
||||
visibleMenuIds = visibleList.stream()
|
||||
.filter(id -> id != null && !id.isEmpty())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
if (visibleMenuIds.isEmpty()) {
|
||||
return emptyRoots();
|
||||
@@ -129,6 +138,20 @@ public class MenuController {
|
||||
return ctx.getProperty(LoggedInUser.CURRENTUSER);
|
||||
}
|
||||
|
||||
/**
|
||||
* 超管判定(与 {@code WritePrivilegeChecker.isAdmin} 同口径):
|
||||
* userId trim 后忽略大小写等于 {@code LoggedInUser.ADMIN}("ADMIN" + 补白)。
|
||||
*
|
||||
* <p>老框架 {@code ViewMenuServiceImpl} 对 admin 不 JOIN view_sys_usermenuid(见全部菜单),
|
||||
* 此处沿用该语义:系统管理员在菜单里看到全部 isactive 菜单。
|
||||
*
|
||||
* <p>blank 不在此判定(blank userId 在 {@link #menu()} 入口已返回空 roots)。
|
||||
*/
|
||||
static boolean isAdmin(String userId) {
|
||||
return userId != null
|
||||
&& LoggedInUser.ADMIN.trim().equalsIgnoreCase(userId.trim());
|
||||
}
|
||||
|
||||
private Map<String, Object> emptyRoots() {
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("roots", List.of());
|
||||
|
||||
@@ -215,6 +215,64 @@ class MenuServiceTest {
|
||||
assertEquals(0, MenuService.compareSeq(null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("9. allLeafMenuIds:只收叶子(parentid 非空),目录被排除")
|
||||
void allLeafMenuIdsCollectsOnlyLeaves() {
|
||||
List<Map<String, Object>> menus = List.of(
|
||||
dir("D1", "通知目录", "R1", 1),
|
||||
leaf("L1", "通知列表", "D1", "R1", "tpl-001", 1),
|
||||
leaf("L2", "公告列表", "D1", "R1", "tpl-002", 2),
|
||||
dir("D2", "申请目录", "R2", 1),
|
||||
leaf("L3", "发起申请", "D2", "R2", "tpl-003", 1));
|
||||
|
||||
Set<String> ids = MenuService.allLeafMenuIds(menus);
|
||||
|
||||
// 仅叶子(D1/D2 目录不计入)
|
||||
assertEquals(Set.of("L1", "L2", "L3"), ids);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("10. allLeafMenuIds → buildTree:超管可见集=全部叶子时,所有叶子均下发(含被 usermenuid 漏授的叶子)")
|
||||
void allLeafMenuIdsFeedsBuildTreeForAdmin() {
|
||||
List<Map<String, Object>> roots = List.of(root("R1", "信息站", 1));
|
||||
List<Map<String, Object>> menus = List.of(
|
||||
dir("D1", "新闻管理目录", "R1", 1),
|
||||
leaf("L_VIEW", "查看新闻", "D1", "R1", "tpl-view", 1),
|
||||
// 模拟 b6d6be5a「新闻管理」管理列表叶子——普通用户 usermenuid 漏授,超管应可见
|
||||
leaf("b6d6be5a", "新闻管理", "D1", "R1", "ddb75614", 10));
|
||||
|
||||
// 超管可见集 = 全部叶子
|
||||
Set<String> adminVisible = MenuService.allLeafMenuIds(menus);
|
||||
assertTrue(adminVisible.contains("b6d6be5a"), "超管可见集应含管理列表叶子 b6d6be5a");
|
||||
|
||||
List<Map<String, Object>> result = service.buildTree(roots, menus, adminVisible);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> dirs = (List<Map<String, Object>>) result.get(0).get("children");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> leaves = (List<Map<String, Object>>) dirs.get(0).get("children");
|
||||
// 两个叶子都下发,管理列表叶子带 templateId=ddb75614
|
||||
assertEquals(2, leaves.size());
|
||||
Map<String, Object> mgmt = leaves.stream()
|
||||
.filter(l -> "b6d6be5a".equals(l.get("id"))).findFirst().orElseThrow();
|
||||
assertEquals("新闻管理", mgmt.get("name"));
|
||||
assertEquals("ddb75614", mgmt.get("templateId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("11. allLeafMenuIds:null/空入参与脏数据(空 id/空 parentid)健壮处理")
|
||||
void allLeafMenuIdsRobust() {
|
||||
assertTrue(MenuService.allLeafMenuIds(null).isEmpty());
|
||||
assertTrue(MenuService.allLeafMenuIds(List.of()).isEmpty());
|
||||
|
||||
Map<String, Object> blankId = new LinkedHashMap<>();
|
||||
blankId.put("id", " ");
|
||||
blankId.put("parentid", "D1");
|
||||
Map<String, Object> good = leaf("L1", "x", "D1", "R1", "tpl", 1);
|
||||
Set<String> ids = MenuService.allLeafMenuIds(List.of(blankId, good));
|
||||
assertEquals(Set.of("L1"), ids);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("8. 多 root 多目录:各 root 独立剪枝,互不影响")
|
||||
void multiRootMultiDir() {
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.contains;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import com.dsasp.server.common.user.service.LoggedInUser;
|
||||
import com.dsasp.server.common.util.UserSession;
|
||||
|
||||
/**
|
||||
* {@link MenuController} 单测:超管 bypass 裁剪 vs 普通用户按 view_sys_usermenuid 裁剪。
|
||||
*
|
||||
* <p>WS-A 契约:
|
||||
* <ol>
|
||||
* <li>当前用户 = ADMIN(超管) → roots 含管理列表叶子 b6d6be5a(新闻管理, templateId=ddb75614),
|
||||
* 且<strong>不</strong>查 view_sys_usermenuid(bypass);</li>
|
||||
* <li>当前用户 = 普通用户 → 仍按 view_sys_usermenuid 裁剪;漏授的 b6d6be5a <strong>不</strong>下发;</li>
|
||||
* <li>未登录(UserSession 无 CURRENTUSER) → 空 roots,不报错。</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>不连库:mock {@link JdbcTemplate} 的 query/queryForList,按 SQL 子串路由桩数据;
|
||||
* 当前用户经 {@link UserSession} ThreadLocal 注入,每用例后清理防跨用例泄漏。
|
||||
*/
|
||||
class MenuControllerTest {
|
||||
|
||||
/** 模拟 sys_menu 里「新闻管理」管理列表叶子(普通用户 usermenuid 漏授)。 */
|
||||
private static final String MGMT_LEAF_ID = "b6d6be5a-6585-4f74-8378-121d36a4b0d1";
|
||||
private static final String MGMT_TEMPLATE_ID = "ddb75614-ead0-470b-8050-a37111c383b8";
|
||||
/** 普通用户被授权可见的另一个叶子(查看新闻)。 */
|
||||
private static final String VIEW_LEAF_ID = "view-leaf-0001";
|
||||
private static final String DIR_ID = "968b52d5-6360-4c4d-9dfe-0b459eb3b603";
|
||||
private static final String ROOT_ID = "info-root-0001";
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
UserSession.remove();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 桩数据
|
||||
|
||||
private List<Map<String, Object>> rootRows() {
|
||||
Map<String, Object> r = new LinkedHashMap<>();
|
||||
r.put("id", ROOT_ID);
|
||||
r.put("name", "信息站");
|
||||
r.put("seq", 1);
|
||||
return List.of(r);
|
||||
}
|
||||
|
||||
private Map<String, Object> dirRow() {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", DIR_ID);
|
||||
m.put("name", "新闻管理");
|
||||
m.put("parentid", null);
|
||||
m.put("menuroot", ROOT_ID);
|
||||
m.put("view_templateid", null);
|
||||
m.put("typename", "folder");
|
||||
m.put("seq", 1);
|
||||
return m;
|
||||
}
|
||||
|
||||
private Map<String, Object> leafRow(String id, String name, String templateId, int seq) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("name", name);
|
||||
m.put("parentid", DIR_ID);
|
||||
m.put("menuroot", ROOT_ID);
|
||||
m.put("view_templateid", templateId);
|
||||
m.put("typename", "menu");
|
||||
m.put("seq", seq);
|
||||
return m;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> menuRows() {
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
rows.add(dirRow());
|
||||
rows.add(leafRow(VIEW_LEAF_ID, "查看新闻", "tpl-view", 1));
|
||||
rows.add(leafRow(MGMT_LEAF_ID, "新闻管理", MGMT_TEMPLATE_ID, 10));
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** 按 SQL 子串路由 mock:sys_menu_root → roots;sys_menu → menus。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private JdbcTemplate mockJdbc() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.query(contains("sys_menu_root"), any(RowMapper.class))).thenReturn(rootRows());
|
||||
when(jdbc.query(contains("FROM sys_menu "), any(RowMapper.class))).thenReturn(menuRows());
|
||||
return jdbc;
|
||||
}
|
||||
|
||||
private void loginAs(String userId) {
|
||||
Properties ctx = new Properties();
|
||||
ctx.setProperty(LoggedInUser.CURRENTUSER, userId);
|
||||
UserSession.set(ctx);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Map<String, Object>> allLeaves(Map<String, Object> resp) {
|
||||
List<Map<String, Object>> roots = (List<Map<String, Object>>) resp.get("roots");
|
||||
List<Map<String, Object>> leaves = new ArrayList<>();
|
||||
for (Map<String, Object> r : roots) {
|
||||
for (Map<String, Object> d : (List<Map<String, Object>>) r.get("children")) {
|
||||
leaves.addAll((List<Map<String, Object>>) d.get("children"));
|
||||
}
|
||||
}
|
||||
return leaves;
|
||||
}
|
||||
|
||||
private boolean containsLeaf(List<Map<String, Object>> leaves, String id) {
|
||||
return leaves.stream().anyMatch(l -> id.equals(l.get("id")));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 用例
|
||||
|
||||
@Test
|
||||
@DisplayName("1. 超管(ADMIN) → roots 含管理列表叶子 b6d6be5a,且不查 view_sys_usermenuid")
|
||||
void adminSeesAllMenusBypassingUsermenuid() {
|
||||
JdbcTemplate jdbc = mockJdbc();
|
||||
loginAs("ADMIN");
|
||||
|
||||
MenuController controller = new MenuController(jdbc);
|
||||
Map<String, Object> resp = controller.menu();
|
||||
|
||||
List<Map<String, Object>> leaves = allLeaves(resp);
|
||||
assertTrue(containsLeaf(leaves, MGMT_LEAF_ID),
|
||||
"超管应在菜单里看到「新闻管理」管理列表叶子 b6d6be5a");
|
||||
// 校验 templateId 正确
|
||||
Map<String, Object> mgmt = leaves.stream()
|
||||
.filter(l -> MGMT_LEAF_ID.equals(l.get("id"))).findFirst().orElseThrow();
|
||||
assertEquals(MGMT_TEMPLATE_ID, mgmt.get("templateId"));
|
||||
|
||||
// bypass:超管不应查询 view_sys_usermenuid
|
||||
verify(jdbc, never()).queryForList(contains("view_sys_usermenuid"),
|
||||
eq(String.class), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2. 超管大小写/补白容错:'admin'/'ADMIN '同样 bypass")
|
||||
void adminCaseAndPaddingTolerant() {
|
||||
for (String uid : List.of("admin", "ADMIN ", " Admin ")) {
|
||||
JdbcTemplate jdbc = mockJdbc();
|
||||
loginAs(uid);
|
||||
Map<String, Object> resp = new MenuController(jdbc).menu();
|
||||
assertTrue(containsLeaf(allLeaves(resp), MGMT_LEAF_ID),
|
||||
"userId='" + uid + "' 应被判定为超管并见全部");
|
||||
UserSession.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("3. 普通用户 → 仍按 view_sys_usermenuid 裁剪;漏授的 b6d6be5a 不下发")
|
||||
void normalUserStillPruned() {
|
||||
JdbcTemplate jdbc = mockJdbc();
|
||||
// 普通用户只被授权 查看新闻,未授权 新闻管理(b6d6be5a)
|
||||
when(jdbc.queryForList(contains("view_sys_usermenuid"), eq(String.class),
|
||||
eq("USER_ALICE"))).thenReturn(List.of(VIEW_LEAF_ID));
|
||||
loginAs("USER_ALICE");
|
||||
|
||||
Map<String, Object> resp = new MenuController(jdbc).menu();
|
||||
|
||||
List<Map<String, Object>> leaves = allLeaves(resp);
|
||||
assertTrue(containsLeaf(leaves, VIEW_LEAF_ID), "普通用户应见被授权的查看新闻");
|
||||
assertFalse(containsLeaf(leaves, MGMT_LEAF_ID),
|
||||
"普通用户未被授权 b6d6be5a,不应下发管理列表叶子");
|
||||
// 普通用户必须查 view_sys_usermenuid(按权限裁剪)
|
||||
verify(jdbc, times(1)).queryForList(contains("view_sys_usermenuid"),
|
||||
eq(String.class), eq("USER_ALICE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("4. 普通用户无任何可见菜单 → 空 roots")
|
||||
void normalUserNoVisibleMenuEmpty() {
|
||||
JdbcTemplate jdbc = mockJdbc();
|
||||
when(jdbc.queryForList(contains("view_sys_usermenuid"), eq(String.class),
|
||||
eq("USER_BOB"))).thenReturn(List.of());
|
||||
loginAs("USER_BOB");
|
||||
|
||||
Map<String, Object> resp = new MenuController(jdbc).menu();
|
||||
assertTrue(((List<?>) resp.get("roots")).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("5. 未登录(UserSession 无 CURRENTUSER) → 空 roots,不报错")
|
||||
void noPrincipalEmpty() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
// 未设置 UserSession
|
||||
Map<String, Object> resp = new MenuController(jdbc).menu();
|
||||
assertTrue(((List<?>) resp.get("roots")).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("6. isAdmin 静态判定:ADMIN/大小写/补白=true;普通用户/null=false")
|
||||
void isAdminPredicate() {
|
||||
assertTrue(MenuController.isAdmin("ADMIN"));
|
||||
assertTrue(MenuController.isAdmin("admin"));
|
||||
assertTrue(MenuController.isAdmin("ADMIN "));
|
||||
assertTrue(MenuController.isAdmin(" Admin "));
|
||||
assertFalse(MenuController.isAdmin("USER_ALICE"));
|
||||
assertFalse(MenuController.isAdmin(null));
|
||||
assertFalse(MenuController.isAdmin(""));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user