fix(权限): 普通成员可下载本主体录像;面单摄像头不再被 cameras.enabled 快照挡住
- 回放/下载要用的 GET /api/web/nvrs/:id/credential 不再挂 subjectAdmin: 普通成员可读(取本主体录像机凭据去拉流),范围由 credentialOf(id, subjectId) 限定当前主体, 别的主体一律 404;其余 /nvrs 管理接口仍然只给主体管理员 - 面单摄像头下发只看 cameras.status:cameras.enabled 是「导入通道那一刻」的快照, 通道后来才装上摄像头(预览正常)时它一直是 0,会把正常的一路挡在设备列表外 - 停用的工作台/摄像头不再静默丢弃,改为给 warning(设备端页面底部可见) - 补测试:nvr-credential 权限 5 条、label-scan 3 条 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
43dae198db
commit
9027857303
@ -1106,6 +1106,9 @@ curl -X POST http://127.0.0.1:7300/api/internal/subject-orders/<orderNo>/pay \
|
||||
(判定与 `subjectService.ensureAdmin` 一致:创建者或 `role=1`)。
|
||||
前端同步:左侧菜单只对管理员显示这几项,路由 `meta.adminOnly` 会把成员挡回首页;
|
||||
`/api/web/stats` 与 `/api/web/pack-query` **不拦**(首页概览 / 桌面端打包查询普通成员也要用)。
|
||||
**`GET /api/web/nvrs/:id/credential` 也不拦**:按运单回放/下载录像要凭据,是打包现场日常动作,
|
||||
普通成员可以取,但范围由 `nvr.credentialOf(id, subjectId)` 限定在当前主体(别的主体一律 404);
|
||||
其余 `/nvrs` 管理接口仍然只给管理员。
|
||||
- 登录(`POST /api/web/auth/login`)的返回里**带 `subjects` 列表**,前端存下并记住上次使用的主体(`localStorage.packs_subject_id`)。
|
||||
- 设备(中控)没有「当前主体」概念:`GET /api/device/login?subjectId=` 可选,不传就用确认人可进入的第一个主体。
|
||||
|
||||
|
||||
@ -109,6 +109,7 @@ module.exports = (app) => {
|
||||
// 本主体设备:列表 / 详情 / 通道 / 解绑 / 删除(越权访问一律 404)
|
||||
// ⚠️ 业务数据(设备 / 录像机 / 摄像头 / 工作台 / 扫码记录)**只有主体管理员能看**,
|
||||
// 普通成员访问一律 403(subjectAdmin),前端菜单也同步隐藏
|
||||
// (例外:回放/下载要用的 `/nvrs/:id/credential` 普通成员也能读,见下面)
|
||||
// ⚠️ /devices/active 与 /devices/activate 必须在 /devices/:id 之前
|
||||
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);
|
||||
@ -135,6 +136,12 @@ module.exports = (app) => {
|
||||
router.get('/api/web/edge/download/:taskId', apiAuth, subjectAuth, controller.web.edge.downloadStatus);
|
||||
router.get('/api/web/edge/download/:taskId/file', apiAuth, subjectAuth, controller.web.edge.downloadFile);
|
||||
|
||||
// 回放/下载要录像机登录凭据:**普通成员也要能取**(只是不能取别的主体的)。
|
||||
// 按运单回放/下载 = 打包现场日常动作,不该只有主体管理员能做;
|
||||
// 范围由 `nvr.credentialOf(id, subjectId)` 兜住:不属于当前主体的 NVR 一律 404。
|
||||
// ⚠️ 所以这条不能挂 subjectAdmin,其余 /nvrs 管理接口仍然只给管理员。
|
||||
router.get('/api/web/nvrs/:id/credential', apiAuth, subjectAuth, controller.web.nvr.credential);
|
||||
|
||||
// ---------- 视频接入:NVR / 摄像头 / 工作台(只碰本主体的数据;仅主体管理员)----------
|
||||
// 拉取通道 = 前端调本机软件(localhost:17300/api/hik/channels)→ 勾选 → POST /nvrs/:id/channels 落库
|
||||
router.get('/api/web/nvrs', apiAuth, subjectAuth, subjectAdmin, controller.web.nvr.index);
|
||||
@ -144,7 +151,6 @@ module.exports = (app) => {
|
||||
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, subjectAdmin, controller.web.camera.options);
|
||||
|
||||
@ -12,8 +12,11 @@ const { virtualChannelIdOf, isVirtualChannelId } = require('../lib/virtual-chann
|
||||
* 识别结果按**该工作台已绑定的扫码通道**上报(这样服务端才能找到工作台快照)。
|
||||
*
|
||||
* 这里只做「算出这台设备该跑哪些识别任务」:
|
||||
* - 只看**本主体**、启用中的工作台(**散单和团购都支持**面单摄像头);
|
||||
* - 只看**本主体**、启用中的工作台(**散单和团购都支持**面单摄像头);停用的工作台/摄像头
|
||||
* 不下发,但会给一条 warning 说明原因(现场「配了面单但设备列表里没有」时看这个);
|
||||
* - 只看被标记为面单摄像头(`workstation_cameras.is_label_camera = 1`)的绑定;
|
||||
* 摄像头本身停用(`cameras.status = 0`)时不下发,改给一条 warning;
|
||||
* 注意**不看** `cameras.enabled`:那是导入通道时的快照,不代表现在有没有画面;
|
||||
* - 上报通道取「该工作台绑定、且属于**本设备**的扫码通道」(多条时取 sortNo 最小的一条);
|
||||
* 工作台**没接扫码枪**(没绑本机通道)时,自动给它一条虚拟通道 `CAM-<工作台id>`
|
||||
* (名字 `面单摄像头·<摄像头名>`,见 `lib/virtual-channel`)——摄像头可以完全替代扫码枪;
|
||||
@ -54,6 +57,32 @@ class LabelScanService extends Service {
|
||||
});
|
||||
|
||||
const warnings = [];
|
||||
|
||||
// 停用的工作台不会下发(下面只查 status=1 的),但现场很容易以为「配了面单就该出现」,
|
||||
// 所以单独查一遍:有面单摄像头却因为工作台停用没下发的,给一条提示。
|
||||
const disabledWorkstations = (await this.Workstation.findAll({
|
||||
where: { subjectId, status: { [Op.ne]: 1 } },
|
||||
attributes: [ 'id', 'code', 'name', 'status' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
})).filter(row => Number(row.status) !== 1);
|
||||
|
||||
if (disabledWorkstations.length) {
|
||||
const disabledIds = disabledWorkstations.map(row => Number(row.id));
|
||||
const disabledLinks = await this.app.model.WorkstationCamera.findAll({
|
||||
where: { workstationId: { [Op.in]: disabledIds }, isLabelCamera: 1 },
|
||||
attributes: [ 'workstationId' ],
|
||||
});
|
||||
const hasLabel = new Set(disabledLinks.map(link => Number(link.workstationId)));
|
||||
for (const row of disabledWorkstations) {
|
||||
if (!hasLabel.has(Number(row.id))) continue;
|
||||
warnings.push({
|
||||
workstationId: Number(row.id),
|
||||
workstationName: row.name || row.code,
|
||||
message: '工作台已停用(启用后才会下发这一路),不会做面单识别',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!workstations.length) {
|
||||
return { revision: revisionOf({ items: [], channels: [] }), items: [], channels: [], warnings, scannedAt: new Date() };
|
||||
}
|
||||
@ -117,6 +146,23 @@ class LabelScanService extends Service {
|
||||
|
||||
const cameraName = camera.name || camera.channelName || `通道${camera.channelNo}`;
|
||||
|
||||
// ⚠️ 只看 `status`(用户自己的启用/停用)。
|
||||
//
|
||||
// `cameras.enabled` 是**导入通道那一刻**录像机报告的「该通道已配置」,是个快照:
|
||||
// - 通道导入时 NVR 上还没装摄像头(或当时设备服务没起来,拿不到通道状态)→ 库里存 0;
|
||||
// - 之后在 NVR 上装了摄像头,预览是通的(预览只看 status),但这个字段永远不会自己变;
|
||||
// - 前端「拉取通道」又把已导入的通道置灰,没法靠重新导入刷新它。
|
||||
// 结果就是「预览正常、设为面单摄像头却不出现在设备列表」——所以它不能当开关用。
|
||||
const cameraEnabled = Number(camera.status) === 1;
|
||||
if (!cameraEnabled) {
|
||||
warnings.push({
|
||||
workstationId,
|
||||
workstationName: workstation.name || workstation.code,
|
||||
message: `摄像头「${cameraName}」已停用(摄像头管理里启用后再试),不会做面单识别`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// 没有扫码通道也不要紧:摄像头本身就是一条「虚拟通道」(`CAM-<工作台id>`),见 ensureVirtualChannel
|
||||
const boundChannel = channelByWorkstation.get(workstationId);
|
||||
const realChannel = boundChannel && !isVirtualChannelId(boundChannel.channelId) ? boundChannel : null;
|
||||
@ -168,7 +214,7 @@ class LabelScanService extends Service {
|
||||
cameraId: Number(camera.id),
|
||||
cameraName,
|
||||
cameraChannelNo: Number(camera.channelNo) || 0,
|
||||
cameraEnabled: Number(camera.status) === 1 && Number(camera.enabled) === 1,
|
||||
cameraEnabled,
|
||||
streamType: Number(link.streamType) === 1 ? 1 : 0,
|
||||
nvr: {
|
||||
id: Number(nvr.id),
|
||||
|
||||
116
backend/test/app/controller/nvr-credential.test.js
Normal file
116
backend/test/app/controller/nvr-credential.test.js
Normal file
@ -0,0 +1,116 @@
|
||||
'use strict';
|
||||
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||||
|
||||
/** 伪造的 sequelize 实例(toJSON 返回同一份数据) */
|
||||
const instance = (data) => Object.assign({ toJSON: () => data }, data);
|
||||
|
||||
/**
|
||||
* `GET /api/web/nvrs/:id/credential` 的权限
|
||||
*
|
||||
* 按运单回放/下载录像要录像机登录凭据,这是打包现场的日常动作:
|
||||
* **普通成员也要能取自己主体的凭据**,但不能扩散到别的主体。
|
||||
* 其余 `/api/web/nvrs` 管理接口仍然只给主体管理员。
|
||||
*/
|
||||
describe('test/app/controller/nvr-credential.test.js', () => {
|
||||
const SUBJECT_ID = 5;
|
||||
const OTHER_SUBJECT_ID = 99;
|
||||
|
||||
// 普通成员:不是主体创建者(adminUserId=1),role=2
|
||||
const member = { id: 2, username: 'packer', nickname: '打包员', status: 1 };
|
||||
const adminUserId = 1;
|
||||
|
||||
const token = () => {
|
||||
const cfg = app.config.jwt;
|
||||
const signed = jwt.sign({ sub: String(member.id), typ: 'user' }, cfg.secret, {
|
||||
algorithm: 'HS256',
|
||||
expiresIn: 3600,
|
||||
issuer: cfg.issuer,
|
||||
audience: cfg.audience,
|
||||
});
|
||||
return `Bearer ${signed}`;
|
||||
};
|
||||
|
||||
const withSubject = (req, subjectId = SUBJECT_ID) => req
|
||||
.set('Authorization', token())
|
||||
.set('X-Subject-Id', String(subjectId));
|
||||
|
||||
function mockModels(models = {}) {
|
||||
mock(app, 'model', Object.assign({
|
||||
User: { async findByPk() { return instance(member); } },
|
||||
SubjectMember: {
|
||||
async findOne() {
|
||||
return instance({ id: 21, subjectId: SUBJECT_ID, userId: member.id, name: '打包员', role: 2, status: 1 });
|
||||
},
|
||||
},
|
||||
Subject: {
|
||||
async findByPk() {
|
||||
return instance({
|
||||
id: SUBJECT_ID,
|
||||
name: '测试主体',
|
||||
adminUserId,
|
||||
status: 1,
|
||||
expireAt: new Date(Date.now() + 86400000),
|
||||
});
|
||||
},
|
||||
},
|
||||
}, models));
|
||||
}
|
||||
|
||||
const nvrRow = (subjectId) => instance({
|
||||
id: 9,
|
||||
subjectId,
|
||||
name: '仓库录像机',
|
||||
host: '192.168.8.241',
|
||||
port: 8000,
|
||||
username: 'admin',
|
||||
passwordEnc: 'x',
|
||||
});
|
||||
|
||||
it('普通成员能取本主体录像机凭据(回放/下载要用)', async () => {
|
||||
mockModels({
|
||||
Nvr: { async findByPk() { return nvrRow(SUBJECT_ID); } },
|
||||
});
|
||||
app.mockService('nvr', 'passwordOf', () => 'p@ss');
|
||||
|
||||
const res = await withSubject(app.httpRequest().get('/api/web/nvrs/9/credential')).expect(200);
|
||||
assert(res.body.data.host === '192.168.8.241', JSON.stringify(res.body));
|
||||
assert(res.body.data.password === 'p@ss', '凭据要能拿到,不然桌面端拉不了流');
|
||||
assert(res.body.data.hasPassword === true);
|
||||
});
|
||||
|
||||
it('别的主体的录像机 → 404(范围只限当前主体)', async () => {
|
||||
mockModels({
|
||||
Nvr: { async findByPk() { return nvrRow(OTHER_SUBJECT_ID); } },
|
||||
});
|
||||
app.mockService('nvr', 'passwordOf', () => 'p@ss');
|
||||
|
||||
await withSubject(app.httpRequest().get('/api/web/nvrs/9/credential')).expect(404);
|
||||
});
|
||||
|
||||
it('录像机不存在 → 404', async () => {
|
||||
mockModels({ Nvr: { async findByPk() { return null; } } });
|
||||
app.mockService('nvr', 'passwordOf', () => '');
|
||||
|
||||
await withSubject(app.httpRequest().get('/api/web/nvrs/9/credential')).expect(404);
|
||||
});
|
||||
|
||||
it('没带主体 header → 400(不能靠"随便拿一台"绕过)', async () => {
|
||||
mockModels({ Nvr: { async findByPk() { return nvrRow(SUBJECT_ID); } } });
|
||||
app.mockService('nvr', 'passwordOf', () => 'p@ss');
|
||||
|
||||
const res = await app.httpRequest()
|
||||
.get('/api/web/nvrs/9/credential')
|
||||
.set('Authorization', token())
|
||||
.expect(400);
|
||||
assert(res.body.status === 'subject_required', JSON.stringify(res.body));
|
||||
});
|
||||
|
||||
it('其余 NVR 管理接口仍然只给主体管理员(403 subject_forbidden)', async () => {
|
||||
mockModels({ Nvr: { async findAll() { return []; } } });
|
||||
|
||||
const list = await withSubject(app.httpRequest().get('/api/web/nvrs')).expect(403);
|
||||
assert(list.body.status === 'subject_forbidden', JSON.stringify(list.body));
|
||||
});
|
||||
});
|
||||
@ -50,10 +50,21 @@ describe('test/app/service/label-scan.test.js', () => {
|
||||
app.mockService('nvr', 'passwordOf', () => nvrPassword);
|
||||
});
|
||||
|
||||
function mockModels({ workstations, links, channels }) {
|
||||
function mockModels({ workstations, links, channels, disabled = [], disabledLinks = [] }) {
|
||||
mock(app, 'model', {
|
||||
Workstation: { async findAll() { return workstations; } },
|
||||
WorkstationCamera: { async findAll() { return links; } },
|
||||
Workstation: {
|
||||
// 主查询是 status: 1;停用工作台那条是 status: { [Op.ne]: 1 }(对象,不是数字)
|
||||
async findAll(options = {}) {
|
||||
const status = options.where && options.where.status;
|
||||
return typeof status === 'number' && Number(status) === 1 ? workstations : disabled;
|
||||
},
|
||||
},
|
||||
WorkstationCamera: {
|
||||
// 主查询带 include(录像机/摄像头),停用工作台那条只查 workstationId
|
||||
async findAll(options = {}) {
|
||||
return options.include ? links : disabledLinks;
|
||||
},
|
||||
},
|
||||
WorkstationScanChannel: {
|
||||
async findAll() { return channels; },
|
||||
async findOrCreate({ defaults }) {
|
||||
@ -143,6 +154,61 @@ describe('test/app/service/label-scan.test.js', () => {
|
||||
nvrPassword = 'p@ss';
|
||||
});
|
||||
|
||||
it('摄像头已停用 → 不下发,给提示(现场「配了面单但列表里没有」就是这条)', async () => {
|
||||
const disabled = cameraLink(11, 2, { id: 12, name: '测试散单面单', channelNo: 64 });
|
||||
disabled.camera.status = 0; // 摄像头管理里停用
|
||||
|
||||
mockModels({
|
||||
workstations: [ workstation(2, '散单1号', 1) ],
|
||||
links: [ disabled ],
|
||||
channels: [],
|
||||
});
|
||||
|
||||
const ctx = app.createAnonymousContext();
|
||||
const data = await ctx.service.labelScan.configOf(scanner);
|
||||
|
||||
assert(data.items.length === 0, JSON.stringify(data.items));
|
||||
assert(data.warnings.length === 1, JSON.stringify(data.warnings));
|
||||
assert(/已停用/.test(data.warnings[0].message), data.warnings[0].message);
|
||||
assert(data.warnings[0].workstationId === 2);
|
||||
});
|
||||
|
||||
it('工作台已停用 → 不下发,给提示(现场「配了面单但列表里没有」的另一条原因)', async () => {
|
||||
mockModels({
|
||||
workstations: [],
|
||||
links: [],
|
||||
channels: [],
|
||||
disabled: [ instance({ id: 8, code: 'WS-8', name: '旧工作台', status: 0 }) ],
|
||||
disabledLinks: [ instance({ workstationId: 8 }) ],
|
||||
});
|
||||
|
||||
const ctx = app.createAnonymousContext();
|
||||
const data = await ctx.service.labelScan.configOf(scanner);
|
||||
|
||||
assert(data.items.length === 0);
|
||||
assert(data.warnings.length === 1, JSON.stringify(data.warnings));
|
||||
assert(data.warnings[0].workstationId === 8);
|
||||
assert(/已停用/.test(data.warnings[0].message), data.warnings[0].message);
|
||||
});
|
||||
|
||||
it('cameras.enabled = 0(导入通道时的快照)不影响下发:只有 status 是开关', async () => {
|
||||
const stale = cameraLink(11, 2, { id: 12, name: '后来才装的摄像头', channelNo: 64 });
|
||||
stale.camera.enabled = 0; // 导入通道时 NVR 上还没有摄像头,后来才装(预览正常但字段一直是 0)
|
||||
|
||||
mockModels({
|
||||
workstations: [ workstation(2, '散单1号', 1) ],
|
||||
links: [ stale ],
|
||||
channels: [ channelLink(2, 7, '2', '测试安卓') ],
|
||||
});
|
||||
|
||||
const ctx = app.createAnonymousContext();
|
||||
const data = await ctx.service.labelScan.configOf(scanner);
|
||||
|
||||
assert(data.items.length === 1, JSON.stringify(data.items));
|
||||
assert(data.items[0].cameraEnabled === true, 'status=1 就要下发,别被 enabled 快照挡住');
|
||||
assert(data.warnings.length === 0);
|
||||
});
|
||||
|
||||
it('revision 随内容变化(改通道 / 改密码都会变)', async () => {
|
||||
mockModels({
|
||||
workstations: [ workstation(2, '散单1号', 1) ],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user