From e6198a433429a3e4c1d5025fb6eeee6dd805b3c2 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 21 Oct 2026 17:00:09 +0800 Subject: [PATCH] =?UTF-8?q?web=20=E7=AB=AF=E6=9D=83=E9=99=90=E6=94=B6?= =?UTF-8?q?=E7=B4=A7=EF=BC=9A=E4=B8=9A=E5=8A=A1=E6=95=B0=E6=8D=AE=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=B8=8E=E8=8F=9C=E5=8D=95=E4=BB=85=E4=B8=BB=E4=BD=93?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后端 - 新增 middleware/subject_admin.js:在 subjectAuth 之后校验「创建者 或 role=1」,否则 403 { status: subject_forbidden, message: 只有主体管理员可以查看该数据(请联系主体管理员) } - 挂到全部业务数据接口:/api/web/devices*、/records、/nvrs*(含 credential)、/cameras*、 /workstations*、/workstation-scan-channels - 有意不拦:/api/web/stats(首页概览)、/api/web/pack-query(桌面端打包查询,工位日常要用) 前端 - 路由加 meta.adminOnly(设备/录像机/摄像头/工作台/扫码记录,dev 的测试页同样), 守卫里非管理员直接回首页 ?denied=1 - AppLayout 菜单按 isSubjectAdmin 过滤,成员只看到 首页 / 主体·成员 / 个人中心 - 首页对成员改成说明卡片(不再请求 stats、不显示设备/通道/记录数),被挡回时给出提示 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- backend/README.md | 5 ++ backend/app/middleware/subject_admin.js | 40 +++++++++++++ backend/app/router.js | 76 +++++++++++++------------ frontend/src/components/AppLayout.vue | 33 +++++++---- frontend/src/router/index.ts | 21 ++++--- frontend/src/views/HomeView.vue | 30 +++++++++- 6 files changed, 147 insertions(+), 58 deletions(-) create mode 100644 backend/app/middleware/subject_admin.js diff --git a/backend/README.md b/backend/README.md index 7bacb71..4dfece2 100644 --- a/backend/README.md +++ b/backend/README.md @@ -1059,6 +1059,11 @@ curl -X POST http://127.0.0.1:7300/api/internal/subject-orders//pay \ - 不是该主体成员 / 主体不存在 → **404** - 主体被停用 → **403** `subject_disabled`;主体到期 → **403** `subject_expired`(带 `expireAt`) - 通过后写 `ctx.state.subjectId / subject / subjectMember`,service 一律用 `subjectId` 限定范围 +- **业务数据只给主体管理员**:设备 / 录像机 / 摄像头 / 工作台 / 扫码记录这些接口在 `subjectAuth` 之后 + 再挂 `middleware.subjectAdmin()`,普通成员一律 **403** `{ status: 'subject_forbidden' }` + (判定与 `subjectService.ensureAdmin` 一致:创建者或 `role=1`)。 + 前端同步:左侧菜单只对管理员显示这几项,路由 `meta.adminOnly` 会把成员挡回首页; + `/api/web/stats` 与 `/api/web/pack-query` **不拦**(首页概览 / 桌面端打包查询普通成员也要用)。 - 登录(`POST /api/web/auth/login`)的返回里**带 `subjects` 列表**,前端存下并记住上次使用的主体(`localStorage.packs_subject_id`)。 - 设备(中控)没有「当前主体」概念:`GET /api/device/login?subjectId=` 可选,不传就用确认人可进入的第一个主体。 diff --git a/backend/app/middleware/subject_admin.js b/backend/app/middleware/subject_admin.js new file mode 100644 index 0000000..8ed5646 --- /dev/null +++ b/backend/app/middleware/subject_admin.js @@ -0,0 +1,40 @@ +'use strict'; + +/** + * 主体管理员校验(**必须挂在 subjectAuth 之后**) + * + * 用法:`router.get('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller...)` + * + * 为什么单独一个中间件: + * - `subjectAuth` 只保证「你是这个主体的成员」,业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录) + * 只有**主体管理员**能看,普通成员连列表都不该拿到; + * - 判断口径与 `subjectService.ensureAdmin` 一致:`subjects.admin_user_id`(创建者)或 `role = 1` 的成员。 + * + * 失败:403 `{ ok:false, status:'subject_forbidden', message }` + */ +module.exports = () => { + return async function subjectAdmin(ctx, next) { + const member = ctx.state.subjectMember; + const subject = ctx.state.subject; + + if (!subject || !member) { + // 正常不会走到:忘了挂 subjectAuth 时给个明确提示,而不是静默放行 + ctx.status = 500; + ctx.body = { ok: false, message: '服务端路由缺少主体上下文(subjectAdmin 必须挂在 subjectAuth 之后)' }; + return; + } + + const isAdmin = Number(subject.adminUserId) === Number(member.userId) || Number(member.role) === 1; + if (!isAdmin) { + ctx.status = 403; + ctx.body = { + ok: false, + status: 'subject_forbidden', + message: '只有主体管理员可以查看该数据(请联系主体管理员)', + }; + return; + } + + await next(); + }; +}; diff --git a/backend/app/router.js b/backend/app/router.js index 2eeafaf..3ba264e 100644 --- a/backend/app/router.js +++ b/backend/app/router.js @@ -8,6 +8,7 @@ module.exports = (app) => { const { router, controller, middleware } = app; const apiAuth = middleware.apiAuth(); const subjectAuth = middleware.subjectAuth(); + const subjectAdmin = middleware.subjectAdmin(); const subjectActive = middleware.subjectActive(); const internalAuth = middleware.internalAuth(); const scannerAuth = middleware.scannerAuth(); @@ -80,55 +81,58 @@ module.exports = (app) => { router.get('/api/web/stats', apiAuth, subjectAuth, controller.web.stats.index); // 本主体设备:列表 / 详情 / 通道 / 解绑 / 删除(越权访问一律 404) + // ⚠️ 业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)**只有主体管理员能看**, + // 普通成员访问一律 403(subjectAdmin),前端菜单也同步隐藏 // ⚠️ /devices/active 与 /devices/activate 必须在 /devices/:id 之前 - router.get('/api/web/devices', apiAuth, subjectAuth, controller.web.device.index); - router.get('/api/web/devices/active', apiAuth, subjectAuth, controller.web.device.active); - router.post('/api/web/devices/activate', apiAuth, subjectAuth, controller.web.device.activate); - router.get('/api/web/devices/:id', apiAuth, subjectAuth, controller.web.device.show); - router.get('/api/web/devices/:id/channels', apiAuth, subjectAuth, controller.web.device.channels); - router.post('/api/web/devices/:id/unbind', apiAuth, subjectAuth, controller.web.device.unbind); - router.post('/api/web/devices/:id/logout', apiAuth, subjectAuth, controller.web.device.logout); - router.delete('/api/web/devices/:id', apiAuth, subjectAuth, controller.web.device.destroy); + router.get('/api/web/devices', apiAuth, subjectAuth, subjectAdmin, controller.web.device.index); + router.get('/api/web/devices/active', apiAuth, subjectAuth, subjectAdmin, controller.web.device.active); + router.post('/api/web/devices/activate', apiAuth, subjectAuth, subjectAdmin, controller.web.device.activate); + router.get('/api/web/devices/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.device.show); + router.get('/api/web/devices/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.device.channels); + router.post('/api/web/devices/:id/unbind', apiAuth, subjectAuth, subjectAdmin, controller.web.device.unbind); + router.post('/api/web/devices/:id/logout', apiAuth, subjectAuth, subjectAdmin, controller.web.device.logout); + router.delete('/api/web/devices/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.device.destroy); - // 本主体扫码记录(范围限定在当前主体的设备) - router.get('/api/web/records', apiAuth, subjectAuth, controller.web.record.index); + // 本主体扫码记录(范围限定在当前主体的设备;仅主体管理员) + router.get('/api/web/records', apiAuth, subjectAuth, subjectAdmin, controller.web.record.index); // 运单回放查询:运单号 → 当时的工位快照(录像机/摄像头/布局)+ 要拉的回放区间 // 桌面端拿到后按 streams 去各 NVR 拉那段录像(凭据走 /api/web/nvrs/:id/credential) + // ⚠️ 这个不挂 subjectAdmin:它是打包工位的日常查询入口,普通成员也要能用 router.get('/api/web/pack-query', apiAuth, subjectAuth, controller.web.pack.query); - // ---------- 视频接入:NVR / 摄像头 / 工作台(只碰本主体的数据)---------- + // ---------- 视频接入:NVR / 摄像头 / 工作台(只碰本主体的数据;仅主体管理员)---------- // 拉取通道 = 前端调本机软件(localhost:17300/api/hik/channels)→ 勾选 → POST /nvrs/:id/channels 落库 - router.get('/api/web/nvrs', apiAuth, subjectAuth, controller.web.nvr.index); - router.post('/api/web/nvrs', apiAuth, subjectAuth, controller.web.nvr.create); - router.get('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.show); - router.put('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.update); - router.delete('/api/web/nvrs/:id', apiAuth, subjectAuth, controller.web.nvr.destroy); - router.get('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, controller.web.nvr.cameras); - router.post('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, controller.web.nvr.syncChannels); - router.get('/api/web/nvrs/:id/credential', apiAuth, subjectAuth, controller.web.nvr.credential); + router.get('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.index); + router.post('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.create); + router.get('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.show); + router.put('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.update); + router.delete('/api/web/nvrs/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.destroy); + router.get('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.cameras); + router.post('/api/web/nvrs/:id/channels', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.syncChannels); + router.get('/api/web/nvrs/:id/credential', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.credential); // 摄像头:/options、/batch-delete 必须在 /:id 之前注册,否则会被当成 id - router.get('/api/web/cameras/options', apiAuth, subjectAuth, controller.web.camera.options); - router.post('/api/web/cameras/batch-delete', apiAuth, subjectAuth, controller.web.camera.batchDestroy); - router.get('/api/web/cameras', apiAuth, subjectAuth, controller.web.camera.index); - router.get('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.show); - router.put('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.update); - router.delete('/api/web/cameras/:id', apiAuth, subjectAuth, controller.web.camera.destroy); + router.get('/api/web/cameras/options', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.options); + router.post('/api/web/cameras/batch-delete', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.batchDestroy); + router.get('/api/web/cameras', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.index); + router.get('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.show); + router.put('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.update); + router.delete('/api/web/cameras/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.camera.destroy); // 工作台:CRUD + 绑定摄像头 + 绑定扫码通道 - router.get('/api/web/workstation-scan-channels', apiAuth, subjectAuth, controller.web.workstation.availableScanChannels); - router.get('/api/web/workstations', apiAuth, subjectAuth, controller.web.workstation.index); - router.post('/api/web/workstations', apiAuth, subjectAuth, controller.web.workstation.create); - router.get('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.show); - router.put('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.update); - router.delete('/api/web/workstations/:id', apiAuth, subjectAuth, controller.web.workstation.destroy); - router.post('/api/web/workstations/:id/cameras', apiAuth, subjectAuth, controller.web.workstation.bindCameras); - router.delete('/api/web/workstations/:id/cameras/:cameraId', apiAuth, subjectAuth, controller.web.workstation.unbindCamera); + router.get('/api/web/workstation-scan-channels', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.availableScanChannels); + router.get('/api/web/workstations', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.index); + router.post('/api/web/workstations', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.create); + router.get('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.show); + router.put('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.update); + router.delete('/api/web/workstations/:id', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.destroy); + router.post('/api/web/workstations/:id/cameras', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.bindCameras); + router.delete('/api/web/workstations/:id/cameras/:cameraId', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.unbindCamera); // 合成画面布局(拖拽/缩放后保存) - router.put('/api/web/workstations/:id/cameras/:cameraId/layout', apiAuth, subjectAuth, controller.web.workstation.updateCameraLayout); - router.post('/api/web/workstations/:id/scan-channels', apiAuth, subjectAuth, controller.web.workstation.bindScanChannels); - router.delete('/api/web/workstations/:id/scan-channels/:linkId', apiAuth, subjectAuth, controller.web.workstation.unbindScanChannel); + router.put('/api/web/workstations/:id/cameras/:cameraId/layout', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.updateCameraLayout); + router.post('/api/web/workstations/:id/scan-channels', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.bindScanChannels); + router.delete('/api/web/workstations/:id/scan-channels/:linkId', apiAuth, subjectAuth, subjectAdmin, controller.web.workstation.unbindScanChannel); // 前端「测试页」的模拟接口(模拟设备扫码登录 / 模拟中控上报),与真机走同一套 service // 只在非生产环境注册:生产不暴露模拟链路 diff --git a/frontend/src/components/AppLayout.vue b/frontend/src/components/AppLayout.vue index a8e32aa..4157500 100644 --- a/frontend/src/components/AppLayout.vue +++ b/frontend/src/components/AppLayout.vue @@ -6,6 +6,7 @@ import { clearToken, currentUser, displayName } from '@/composables/useAuth'; import { currentSubjectId, hasSubject, + isSubjectAdmin, loadSubjects, setSubjectId, subjects, @@ -20,22 +21,30 @@ withDefaults(defineProps<{ fill?: boolean }>(), { fill: false }); const router = useRouter(); -/** 菜单项:`subject: true` 表示必须已经选中主体才显示 */ +/** + * 菜单项: + * - `subject: true` → 必须已经选中主体才显示 + * - `admin: true` → **只有主体管理员**能看(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录) + */ const allMenus = [ - { to: '/', label: '首页', subject: false }, - { to: '/devices', label: '设备管理', subject: true }, - { to: '/nvrs', label: 'NVR 录像机', subject: true }, - { to: '/cameras', label: '摄像头', subject: true }, - { to: '/workstations', label: '工作台', subject: true }, - { to: '/records', label: '扫码记录', subject: true }, - { to: '/subjects', label: '主体 / 成员', subject: false }, - { to: '/profile', label: '个人中心', subject: false }, + { to: '/', label: '首页', subject: false, admin: false }, + { to: '/devices', label: '设备管理', subject: true, admin: true }, + { to: '/nvrs', label: 'NVR 录像机', subject: true, admin: true }, + { to: '/cameras', label: '摄像头', subject: true, admin: true }, + { to: '/workstations', label: '工作台', subject: true, admin: true }, + { to: '/records', label: '扫码记录', subject: true, admin: true }, + { to: '/subjects', label: '主体 / 成员', subject: false, admin: false }, + { to: '/profile', label: '个人中心', subject: false, admin: false }, // 测试页只在开发构建里出现(生产构建不会产出该页面) - ...(import.meta.env.DEV ? [{ to: '/test', label: '测试页', subject: true }] : []), + ...(import.meta.env.DEV ? [{ to: '/test', label: '测试页', subject: true, admin: true }] : []), ]; -/** 没有主体时只留首页 / 主体 / 个人中心(要求:先建主体才出现业务菜单) */ -const menus = computed(() => allMenus.filter(item => (item.subject ? hasSubject.value : true))); +/** 没有主体时只留首页 / 主体 / 个人中心;普通成员看不到业务数据菜单 */ +const menus = computed(() => allMenus.filter(item => { + if (item.subject && !hasSubject.value) return false; + if (item.admin && !isSubjectAdmin.value) return false; + return true; +})); /** 当前主体(用于右上角展示 / 过期提示) */ const current = computed(() => subjects.value.find(item => item.id === currentSubjectId.value) || null); diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index c8a53cf..a55f016 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -1,11 +1,14 @@ import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'; import { getToken } from '@/composables/useAuth'; -import { ensureSubjects, hasSubject } from '@/composables/useSubject'; +import { ensureSubjects, hasSubject, isSubjectAdmin } from '@/composables/useSubject'; /** * 路由表(meta.public = 不需要登录) * * 手机扫码打开的 `/wechat?spm=…` 也必须是公开路由(扫码的人可能还没登录)。 + * + * `meta.adminOnly = true`:业务数据页(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录), + * **只有主体管理员**能进;普通成员直接跳回首页并提示(后端同名接口也返回 403)。 */ const routes: RouteRecordRaw[] = [ { path: '/login', name: 'login', component: () => import('@/views/LoginView.vue'), meta: { public: true, bare: true } }, @@ -27,16 +30,16 @@ const routes: RouteRecordRaw[] = [ { path: '/', name: 'home', component: () => import('@/views/HomeView.vue'), meta: { allowNoSubject: true } }, // 主体 / 成员:没有主体时只有这个入口(创建主体、切换主体、绑成员) { path: '/subjects', name: 'subjects', component: () => import('@/views/SubjectsView.vue'), meta: { allowNoSubject: true } }, - { path: '/devices', name: 'devices', component: () => import('@/views/DevicesView.vue') }, + { path: '/devices', name: 'devices', component: () => import('@/views/DevicesView.vue'), meta: { adminOnly: true } }, // 视频接入:NVR → 摄像头 → 工作台 - { path: '/nvrs', name: 'nvrs', component: () => import('@/views/NvrsView.vue') }, - { path: '/cameras', name: 'cameras', component: () => import('@/views/CamerasView.vue') }, - { path: '/workstations', name: 'workstations', component: () => import('@/views/WorkstationsView.vue') }, - { path: '/records', name: 'records', component: () => import('@/views/RecordsView.vue') }, + { path: '/nvrs', name: 'nvrs', component: () => import('@/views/NvrsView.vue'), meta: { adminOnly: true } }, + { path: '/cameras', name: 'cameras', component: () => import('@/views/CamerasView.vue'), meta: { adminOnly: true } }, + { path: '/workstations', name: 'workstations', component: () => import('@/views/WorkstationsView.vue'), meta: { adminOnly: true } }, + { path: '/records', name: 'records', component: () => import('@/views/RecordsView.vue'), meta: { adminOnly: true } }, { path: '/profile', name: 'profile', component: () => import('@/views/ProfileView.vue'), meta: { allowNoSubject: true } }, // 测试页只在开发构建里存在:生产包不包含该路由,也不会产出它的 chunk ...(import.meta.env.DEV - ? [{ path: '/test', name: 'test', component: () => import('@/views/TestView.vue') } as RouteRecordRaw] + ? [{ path: '/test', name: 'test', component: () => import('@/views/TestView.vue'), meta: { adminOnly: true } } as RouteRecordRaw] : []), { path: '/:pathMatch(.*)*', redirect: '/' }, @@ -83,6 +86,10 @@ router.beforeEach(async (to) => { if (to.meta.allowNoSubject !== true && !hasSubject.value) { return { name: 'subjects', query: { need: '1' } }; } + // 业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)只有主体管理员能进 + if (to.meta.adminOnly === true && !isSubjectAdmin.value) { + return { name: 'home', query: { denied: '1' } }; + } } return true; }); diff --git a/frontend/src/views/HomeView.vue b/frontend/src/views/HomeView.vue index aec9a68..40e4eb8 100644 --- a/frontend/src/views/HomeView.vue +++ b/frontend/src/views/HomeView.vue @@ -1,18 +1,26 @@