Files
crawler-plugin/admin-frontend-vue/tests/align-user-menu-grantable.test.ts
T
huangzd1997 2c47b27746 feat(用户授权): 非超管授权树只下发自己可授予的菜单,保存按操作者范围替换
- 授权树读接口新增 grantableOnly:非超管只返回自己已持有的菜单,可授予项的
  祖先分组随行但置灰不可勾;否则勾父会连带授予树上未下发的范围外后代
- 保存的整树替换把删除范围收窄到操作者可授予集,范围外既有授权(超管直接
  分配的)原样保留——隐藏项不在提交里,全类型删除会把它们静默清掉
- 前端提交剔除置灰分组的派生勾选;菜单权限配置页仍走全量目录
2026-09-23 15:56:50 +08:00

117 lines
5.7 KiB
TypeScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readSource } from './helpers.ts'
import {
buildMenuOptionTree,
compactDirectGrantIds,
parseMenuOptionList,
parsePermissionMenuItem,
} from '../src/pages/account/user-menu-auth.ts'
// 2026-09-16 线上事故:非超管(普通管理员)在授权树里勾到自己无权授予的菜单后,
// 后端 ensureGrantable 抛 403 并回滚整笔事务——创建用户与保存权限双双失败,
// 前端只显示「普通管理员只能分配自己已有的菜单权限」。
//
// 现在的口径:非超管的授权树**只下发**自己已持有的菜单(读接口按操作者过滤),
// 范围外的项连展示都没有。为保留「一级分组 + 子页面」层级,可授予项的祖先分组仍
// 下发但标 grantable=false 置灰——分组自身不在范围内,一旦可勾,「勾父覆盖全部后代」
// 就会把树上未展示的范围外菜单一起授出去,所以 onCheck 必须剔除置灰节点。
//
// 隐藏不会重现 09-13「权限自己没掉」:保存侧的整树替换范围跟着收窄到操作者可授予
// 集合,范围外的既有授权(超管直接分配给该员工的)不会被这次保存删掉。
test('align_user_menu_grantable_false_maps_to_disabled_node', () => {
const locked = parsePermissionMenuItem(
{ id: 5, name: '查询ASIN', parent_id: null, sort_order: 1, grantable: false },
'admin',
)
assert.equal(locked?.disabled, true, 'grantable=false → 节点置灰')
const allowed = parsePermissionMenuItem(
{ id: 6, name: '店铺管理', parent_id: null, sort_order: 2, grantable: true },
'admin',
)
assert.equal(allowed?.disabled, false, 'grantable=true → 可勾选')
// 菜单管理页等未标记 grantable 的接口必须保持旧行为(全部可勾选)
const unmarked = parsePermissionMenuItem({ id: 7, name: '菜单权限配置', parent_id: null, sort_order: 3 }, 'admin')
assert.equal(unmarked?.disabled, false, '缺省 grantable 视为可授予')
})
test('align_user_menu_grantable_survives_tree_build', () => {
const nodes = parseMenuOptionList(
[
{ id: 98, name: '账号与权限', parent_id: null, sort_order: 1, grantable: false },
{ id: 7, name: '用户管理', parent_id: 98, sort_order: 1, grantable: false },
{ id: 100, name: '店铺管理', parent_id: null, sort_order: 2, grantable: true },
],
'admin',
)
const tree = buildMenuOptionTree(nodes)
const account = tree.find((node) => node.id === 98)
assert.equal(account?.disabled, true, '分组节点置灰')
assert.equal(account?.children?.[0]?.disabled, true, '子节点置灰随树保留')
assert.equal(tree.find((node) => node.id === 100)?.disabled, false, '可授予节点不受影响')
})
test('align_user_menu_grantable_disabled_ancestor_is_never_submitted', () => {
// 置灰分组(操作者无权授予、只为层级而下发)的勾选态由子节点联动派生,
// 提交它等于授予整个分组、连带树上未展示的后代,必须剔除且子项各自保留。
const tree = buildMenuOptionTree(
parseMenuOptionList(
[
{ id: 98, name: '账号与权限', parent_id: null, sort_order: 1, grantable: false },
{ id: 7, name: '用户管理', parent_id: 98, sort_order: 1, grantable: true },
{ id: 8, name: '分组管理', parent_id: 98, sort_order: 2, grantable: true },
],
'admin',
),
)
assert.deepEqual(compactDirectGrantIds([98, 7, 8], tree), [7, 8], '剔除派生勾选的分组,子项不被它覆盖')
})
test('align_user_menu_grantable_grantable_parent_still_covers_children', () => {
// 对照组:操作者确实持有该分组时,勾父仍然压缩掉全部后代。
const tree = buildMenuOptionTree(
parseMenuOptionList(
[
{ id: 98, name: '账号与权限', parent_id: null, sort_order: 1, grantable: true },
{ id: 7, name: '用户管理', parent_id: 98, sort_order: 1, grantable: true },
],
'admin',
),
)
assert.deepEqual(compactDirectGrantIds([98, 7], tree), [98], '可授予父级已勾选时压缩掉后代')
})
test('align_user_menu_grantable_wired_end_to_end', () => {
const tree = readSource('src/pages/account/UserMenuAuthTree.vue')
assert.match(tree, /disabled: 'disabled'/, 'el-tree 按 disabled 键置灰节点')
const api = readSource('src/pages/account/user-menu-auth-api.ts')
assert.match(api, /menuType: ADMIN_MENU_TYPE, grantableOnly: true/, '后台树按授权语义请求')
assert.match(api, /menuType: APP_MENU_TYPE, grantableOnly: true/, '客户端树按授权语义请求')
const vo = readSource(
'../backend-java/src/main/java/com/nanri/aiimage/modules/permission/model/vo/PermissionMenuItemVo.java',
)
assert.match(vo, /private Boolean grantable;/, 'VO 暴露 grantable')
const controller = readSource(
'../backend-java/src/main/java/com/nanri/aiimage/modules/permission/controller/PermissionMenuController.java',
)
assert.match(
controller,
/permissionMenuService\.list\(requireAdmin\(request\), menuType, grantableOnly\)/,
'列表接口传入操作者与授权树开关',
)
const service = readSource(
'../backend-java/src/main/java/com/nanri/aiimage/modules/permission/service/PermissionMenuService.java',
)
assert.match(service, /includeAncestorIds\(grantableIds, menus\)/, '过滤后仍补齐祖先分组')
assert.match(service, /resolveGrantableMenuIds/, '按操作者计算可授予集')
assert.match(service, /ensureGrantable\(operator, grantIds, userId, grantableIds\)/, '保存校验传入目标用户以放行既有授权')
assert.match(service, /Set<Long> deleteScope = normalizedType == null \? null : scopedMenuIds;/, '删除范围跟随操作者可授予集')
})