feat(前端): 公司管理加角色内在闸门(注册表+守卫+剪枝)

- 新建 src/config/company-scope.ts:SUPER_ADMIN_ONLY_MENU_KEYS=['admin_companies']
  与 COMPANY_SCOPED_PAGES(批次 1 的 8 页口径清单,后续任务的唯一分母)
- routes.ts:AdminPageDef 加 superAdminOnly/companyScoped;公司管理标 superAdminOnly: true,
  8 个 A 类页标 companyScoped: true,C 类两页显式写 false(声明而非留空)
- helpers.ts:pruneSuperAdminOnly 纯函数(超管原样返回,非超管递归剪掉专属节点)
- router/index.ts:菜单授权之外补一条与菜单树无关的角色判定
- admin-session.ts:菜单树唯一写点内接剪枝(顺序仍在 fetchCurrentUser 之后)
- 新增 tests/company-super-admin-only.test.ts 5 支护栏
- task-29 的主路径断言随唯一写点改写(原断言只认 canonicalize 裸赋值,
  剪枝并入同一写点后必然失配;改为同时要求 prune + canonicalize,意图不降反强)
This commit is contained in:
2026-10-04 18:28:43 +08:00
parent 380e41bb4a
commit 1f23369629
7 changed files with 116 additions and 16 deletions
@@ -0,0 +1,28 @@
/** 公司口径的唯一清单(设计文档 §8)。
* 单独成文件而不是塞进 routes.ts:routes.ts 被 menu-route-map.ts import,
* 而护栏用例要从这里反过来读注册表,放在一起会成环。 */
/** 角色内在闸门:只有超管可见可读写,与 user_column_permission 无关。 */
export const SUPER_ADMIN_ONLY_MENU_KEYS: readonly string[] = ['admin_companies']
export interface CompanyScopedPage {
/** 与 routes.ts 的 menuKey 逐字一致。 */
menuKey: string
/** 相对 admin-frontend-vue 的 api/取数模块路径;护栏逐文件断言它真的带了 company_id。 */
apiFiles: string[]
/** 页面组件路径;护栏断言它 watch 了 tab.activeId(否则切 tab 不重取数,tab 是装饰品)。 */
pageFiles: string[]
}
/** 参与公司 tab 的页(spec §3 归属普查:批次 1 = 9 个 A 类页里除去依赖二期 V145 的去重库)。
* 后续批次往里加行时,同一笔提交必须带上对应 api 文件的 company_id,否则新用例红。 */
export const COMPANY_SCOPED_PAGES: readonly CompanyScopedPage[] = [
{ menuKey: 'admin_task_center', apiFiles: ['src/api/task-center.ts', 'src/pages/task-center/task-center-model.ts'], pageFiles: ['src/pages/task-center/TaskCenterBoardPage.vue'] },
{ menuKey: 'admin_image_video_tasks', apiFiles: ['src/pages/tasks/image-video-api.ts'], pageFiles: ['src/pages/tasks/ImageVideoTasksPage.vue'] },
{ menuKey: 'admin_user_secret_usage', apiFiles: ['src/api/user-secret-usage.ts'], pageFiles: ['src/pages/account/UserSecretUsagePage.vue'] },
{ menuKey: 'admin_user_secrets', apiFiles: ['src/api/user-secrets.ts'], pageFiles: ['src/pages/account/UserSecretsPage.vue'] },
{ menuKey: 'admin_history', apiFiles: ['src/pages/records/history-api.ts'], pageFiles: ['src/pages/records/RecordsHistoryPage.vue'] },
{ menuKey: 'admin_device_logs', apiFiles: ['src/api/device-logs.ts'], pageFiles: ['src/pages/records/DeviceLogsPage.vue'] },
{ menuKey: 'admin_group_manage', apiFiles: ['src/pages/account/shop-group-api.ts'], pageFiles: ['src/pages/account/GroupsPage.vue'] },
{ menuKey: 'admin_shop_manage', apiFiles: ['src/pages/shop/shop-manage-api.ts'], pageFiles: ['src/pages/shop/ShopManagePage.vue'] },
]
+10
View File
@@ -1,4 +1,5 @@
import type { AdminMenuNode } from '../types/admin'
import { SUPER_ADMIN_ONLY_MENU_KEYS } from '../config/company-scope.ts'
/**
* 取后端菜单树中第一个可路由页面路径(深度优先、保序)。
@@ -32,3 +33,12 @@ export function isRouteAllowed(
if (isSuperAdmin) return true
return hasMenuKey(menuTree, menuKey)
}
/** 从菜单树上摘掉超管专属节点。非超管必须剪(它的公司管理页永远不该出现),
* 超管原样返回——超管的准入由 router/index.ts 那条角色闸门管,这里剪了反而会让侧边栏少一项。 */
export function pruneSuperAdminOnly<T extends { key: string }>(nodes: T[], isSuperAdmin: boolean): T[] {
if (isSuperAdmin) return nodes
return nodes
.filter((node) => !SUPER_ADMIN_ONLY_MENU_KEYS.includes(node.key))
.map((node) => ({ ...node, children: pruneSuperAdminOnly((node as { children?: T[] }).children || [], isSuperAdmin) }))
}
+5
View File
@@ -48,6 +48,11 @@ router.beforeEach(async (to) => {
if (to.path === '/') {
return session.firstVisible || true
}
// 角色内在闸门:与菜单树无关。超管恒 true 会让"只剪菜单树"这条对超管无效,
// 而非超管即使被历史授权过 admin_companies 也进不来(spec §8.1:两处缺一处就留旁路)。
if (to.meta.superAdminOnly && !session.isSuperAdmin) {
return session.firstVisible || '/'
}
// 页面级权限:无 menuKey 开放页放行;越权页回退首个可见页,全无则回根空态。
if (!isRouteAllowed(session.isSuperAdmin, to.meta.menuKey, session.menuTree)) {
return session.firstVisible || '/'
+17 -12
View File
@@ -3,6 +3,10 @@ import type { RouteRecordRaw } from 'vue-router'
export interface AdminPageMeta {
title: string
menuKey?: string
/** 角色内在闸门:非超管一律不可进(spec §2 的 A 类)。 */
superAdminOnly?: boolean
/** 参与公司 tab:取数必须带 company_id(spec §3)。 */
companyScoped?: boolean
}
export interface AdminPageDef extends AdminPageMeta {
@@ -16,25 +20,25 @@ export interface AdminPageDef extends AdminPageMeta {
export const adminPages: AdminPageDef[] = [
{ path: 'account/users', menuKey: 'admin_users', title: '用户管理', load: () => import('@/pages/account/UsersPage.vue') },
{ path: 'account/menus', menuKey: 'admin_columns', title: '菜单管理', load: () => import('@/pages/account/MenusPage.vue') },
{ path: 'account/groups', menuKey: 'admin_group_manage', title: '数据权限分组', load: () => import('@/pages/account/GroupsPage.vue') },
{ path: 'account/user-secrets', menuKey: 'admin_user_secrets', title: '密钥管理', load: () => import('@/pages/account/UserSecretsPage.vue') },
{ path: 'account/user-secret-usage', menuKey: 'admin_user_secret_usage', title: '密钥用量统计', load: () => import('@/pages/account/UserSecretUsagePage.vue') },
{ path: 'account/companies', menuKey: 'admin_companies', title: '公司管理', load: () => import('@/pages/account/CompaniesPage.vue') },
{ path: 'account/groups', menuKey: 'admin_group_manage', title: '数据权限分组', companyScoped: true, load: () => import('@/pages/account/GroupsPage.vue') },
{ path: 'account/user-secrets', menuKey: 'admin_user_secrets', title: '密钥管理', companyScoped: true, load: () => import('@/pages/account/UserSecretsPage.vue') },
{ path: 'account/user-secret-usage', menuKey: 'admin_user_secret_usage', title: '密钥用量统计', companyScoped: true, load: () => import('@/pages/account/UserSecretUsagePage.vue') },
{ path: 'account/companies', menuKey: 'admin_companies', title: '公司管理', superAdminOnly: true, load: () => import('@/pages/account/CompaniesPage.vue') },
{ path: 'shop-center/duplicate-check', menuKey: 'admin_shop_data_duplicate_check', title: '店铺撞款监控', load: () => import('@/pages/tasks/DuplicateCheckPage.vue') },
{ path: 'shop-center/keys', menuKey: 'admin_shop_keys', title: '店铺密钥管理', load: () => import('@/pages/shop/ShopKeysPage.vue') },
{ path: 'shop-center/shops', menuKey: 'admin_shop_manage', title: '店铺管理', load: () => import('@/pages/shop/ShopManagePage.vue') },
{ path: 'records/history', menuKey: 'admin_history', title: '生成记录', load: () => import('@/pages/records/RecordsHistoryPage.vue') },
{ path: 'shop-center/keys', menuKey: 'admin_shop_keys', title: '店铺密钥管理', companyScoped: false, load: () => import('@/pages/shop/ShopKeysPage.vue') },
{ path: 'shop-center/shops', menuKey: 'admin_shop_manage', title: '店铺管理', companyScoped: true, load: () => import('@/pages/shop/ShopManagePage.vue') },
{ path: 'records/history', menuKey: 'admin_history', title: '生成记录', companyScoped: true, load: () => import('@/pages/records/RecordsHistoryPage.vue') },
{ path: 'records/software-version', menuKey: 'admin_version', title: '软件版本管理', load: () => import('@/pages/records/RecordsSoftwareVersionPage.vue') },
{ path: 'records/tutorial', menuKey: 'admin_tutorial', title: '教程管理', load: () => import('@/pages/records/RecordsTutorialPage.vue') },
{ path: 'records/device-logs', menuKey: 'admin_device_logs', title: '日志管理', load: () => import('@/pages/records/DeviceLogsPage.vue') },
{ path: 'task-center/board', menuKey: 'admin_task_center', title: '客户端任务管理', load: () => import('@/pages/task-center/TaskCenterBoardPage.vue') },
{ path: 'records/device-logs', menuKey: 'admin_device_logs', title: '日志管理', companyScoped: true, load: () => import('@/pages/records/DeviceLogsPage.vue') },
{ path: 'task-center/board', menuKey: 'admin_task_center', title: '客户端任务管理', companyScoped: true, load: () => import('@/pages/task-center/TaskCenterBoardPage.vue') },
{ path: 'records/digital-human-version', menuKey: 'digital_human_version', title: '数字人版本管理', load: () => import('@/pages/records/RecordsDigitalHumanVersionPage.vue') },
{ path: 'records/image-video-tasks', menuKey: 'admin_image_video_tasks', title: '视频任务记录', load: () => import('@/pages/tasks/ImageVideoTasksPage.vue') },
{ path: 'records/image-video-tasks', menuKey: 'admin_image_video_tasks', title: '视频任务记录', companyScoped: true, load: () => import('@/pages/tasks/ImageVideoTasksPage.vue') },
{ path: 'shop-center/data-tasks', menuKey: 'admin_shop_data_crawl_tasks', title: '店铺数据记录', load: () => import('@/pages/tasks/ShopDataTasksPage.vue') },
{ path: 'asin-center/registry', menuKey: 'admin_dedupe_total_data', title: '数据去重总数据', load: () => import('@/pages/asin/DedupeRegistryPage.vue') },
{ path: 'asin-center/invalid', menuKey: 'admin_invalid_asin_data', title: '不符合ASIN数据', load: () => import('@/pages/asin/AsinInvalidPage.vue') },
{ path: 'asin-center/query', menuKey: 'admin_query_asin', title: '查询ASIN', load: () => import('@/pages/asin/QueryAsinPage.vue') },
{ path: 'asin-center/categories', menuKey: 'admin_product_categories', title: '商品类目', load: () => import('@/pages/asin/ProductCategoryPage.vue') },
{ path: 'asin-center/categories', menuKey: 'admin_product_categories', title: '商品类目', companyScoped: false, load: () => import('@/pages/asin/ProductCategoryPage.vue') },
{ path: 'asin-center/skip-price', menuKey: 'admin_skip_price_asin', title: '最低价ASIN', load: () => import('@/pages/asin/SkipPricePage.vue') },
]
@@ -42,8 +46,9 @@ export function routeOf(page: AdminPageDef): RouteRecordRaw {
if (!page.path || !page.load || !page.title) {
throw new Error(`路由注册表页面定义非法,需 path/load/title 齐全: ${JSON.stringify(page)}`)
}
const meta: Record<string, string> = { title: page.title }
const meta: Record<string, string | boolean> = { title: page.title }
if (page.menuKey) meta.menuKey = page.menuKey
if (page.superAdminOnly) meta.superAdminOnly = true
return { path: page.path, component: page.load, meta }
}
@@ -3,7 +3,7 @@ import { fetchAdminMenuTree, fetchCurrentUser, logout } from '@/api/session'
import { joinAdminPath } from '@/config/app'
import type { AdminMenuNode, AdminUser } from '@/types/admin'
import { isSuperAdminRole } from '@/types/admin'
import { firstVisiblePath } from '@/router/helpers'
import { firstVisiblePath, pruneSuperAdminOnly } from '@/router/helpers'
import { canonicalizeMenuTree } from '@/router/menu-route-map'
export const useAdminSessionStore = defineStore('admin-session', {
@@ -27,7 +27,11 @@ export const useAdminSessionStore = defineStore('admin-session', {
this.error = ''
try {
this.user = await fetchCurrentUser()
this.menuTree = canonicalizeMenuTree(await fetchAdminMenuTree())
// 剪枝是角色内在闸门的第三处(注册表 + 守卫 + 菜单树),必须在 this.user 赋值之后。
this.menuTree = pruneSuperAdminOnly(
canonicalizeMenuTree(await fetchAdminMenuTree()),
isSuperAdminRole(this.user?.role),
)
this.initialized = true
} catch (error) {
this.error = error instanceof Error ? error.message : '后台初始化失败'
@@ -0,0 +1,47 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { readSource } from './helpers.ts'
import { adminPages } from '../src/router/routes.ts'
import { pruneSuperAdminOnly } from '../src/router/helpers.ts'
import { SUPER_ADMIN_ONLY_MENU_KEYS } from '../src/config/company-scope.ts'
/** 公司配置是角色内在闸门,不是菜单授权(spec §2:菜单授权可以被人改坏,role 不能)。
* 12:3x 事故的教训是"把页面可见性当数据可见性用",这里反过来防另一个方向:只挂一处会留旁路。 */
test('超管专属页必须同时被注册表与口径清单认领(两处不一致就是漏了一处)', () => {
const flagged = adminPages.filter((p) => p.superAdminOnly).map((p) => p.menuKey)
assert.deepEqual(flagged.sort(), [...SUPER_ADMIN_ONLY_MENU_KEYS].sort())
assert.ok(SUPER_ADMIN_ONLY_MENU_KEYS.includes('admin_companies'))
})
test('路由守卫里必须有一条与菜单树无关的角色判定', () => {
const guard = readSource('src/router/index.ts')
assert.match(guard, /superAdminOnly/, '守卫没查角色硬闸门')
assert.match(guard, /isSuperAdmin/, '守卫必须先判 session.isSuperAdmin')
})
test('剪枝:非超管拿不到超管专属节点,超管一个字节都不少', () => {
const tree = [
{ key: 'g1', children: [{ key: 'admin_companies', children: [] }, { key: 'admin_users', children: [] }] },
{ key: 'admin_history', children: [] },
]
const forAdmin = pruneSuperAdminOnly(tree, false)
assert.equal(JSON.stringify(forAdmin).includes('admin_companies'), false, '非超管菜单树里不许留公司管理')
assert.ok(JSON.stringify(forAdmin).includes('admin_users'), '普通节点不许被误剪')
assert.equal(pruneSuperAdminOnly(tree, true), tree, '超管原样返回,不做无谓拷贝')
})
test('剪枝是纯函数且幂等(store 里会反复调用)', () => {
const tree = [{ key: 'admin_companies', children: [] as unknown[] }]
const once = pruneSuperAdminOnly(tree, false)
assert.deepEqual(pruneSuperAdminOnly(once, false), once)
})
test('C 类两页必须显式声明 companyScoped: false(spec §5:声明,不是遗漏)', () => {
// 留空和 false 运行时等价,但只有 false 是"读过普查、决定不参与"的证据。
for (const key of ['admin_shop_keys', 'admin_product_categories']) {
const page = adminPages.find((p) => p.menuKey === key)
assert.ok(page, `注册表里找不到 ${key}`) // 取样非空自检
assert.equal(page.companyScoped, false, `${key} 必须显式写 companyScoped: false,不能留空`)
}
})
+3 -2
View File
@@ -6,8 +6,9 @@ import { parseMenuTree } from '../src/api/session-model.ts'
const store = readSource('src/stores/admin-session.ts')
test('test_task_029_menu_tree_init_normal_primary_path', () => {
// 正常主路径:用户拉取成功后拉取菜单树并写入 store.menuTree。
assert.match(store, /this\.menuTree\s*=\s*canonicalizeMenuTree\(await fetchAdminMenuTree\(\)\)/)
// 正常主路径:用户拉取成功后拉取菜单树、规范化并按角色剪枝,写入 store.menuTree。
// 剪枝必须在唯一写点里——写点分成两处就会有一条不剪的树漏进侧边栏。
assert.match(store, /this\.menuTree\s*=\s*pruneSuperAdminOnly\(\s*canonicalizeMenuTree\(await fetchAdminMenuTree\(\)\)/)
})
test('test_task_029_menu_tree_init_normal_variant_input', () => {