oms/backend/app/middleware/principal_check.js
liufumingren e49c25cd19 feat: 主体停用后下拉置灰不可切换、数据只读、定时任务跳过
- principalCheck 中间件加载主体状态:停用主体只放行读请求(GET/HEAD/OPTIONS)与导出任务提交,
  其余写操作(新建/编辑/推送/删除)一律 403「主体已停用,仅可查看与导出」
- 可选主体列表包含停用主体并按「启用优先」排序;/api/auth/profile 返回 status/disabled
- 顶栏主体下拉显示「xxx(已停用)」且不可选择;当前主体停用时在顶栏下方显示只读提示条
- 定时任务跳过停用主体:自动拉单、订单推送 WMS、发货推送、出库推送、发货结果回传平台、一键拉单任务
- 主体自身的 GET/PUT 未加主体状态校验,运维仍可通过接口重新启用

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 23:31:45 +08:00

105 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 主体校验中间件(防跨主体操作)
* 适用于“需要传入/关联主体ID(principal_id)”的请求:
* - 取不到可校验的主体ID -> 400
* - 当前账号未与该主体绑定(principal_users 无记录)-> 403
* - 主体已停用(principals.status=0)-> 只允许读(GET/HEAD/OPTIONS)与导出,其余写操作 403
* 校验通过后把 principalId 写入 ctx.state.principalId,供控制器做数据隔离(列表过滤、创建归属)。
*
* 解析与鉴权规则:
* - 资源详情/更新/删除(带 :id 且配置了 model):以“资源记录本身的 principal_id”为准做鉴权,
* 忽略请求体里的 principal_id,防止通过篡改请求体越权访问/移动其它主体的数据。
* - 列表/新建(无 :id):以请求入参(body / query)里的 principal_id 为准。
*
* @param {Object} options 中间件配置项
* @param {string} [options.model] 资源模型名,如 'Product',用于按 :id 反查资源所属主体
* @return {Function} koa 中间件
*/
module.exports = (options = {}) => {
const modelName = options.model;
return async (ctx, next) => {
const userId = ctx.userId;
if (!userId) {
ctx.status = 401;
ctx.body = { code: 401, message: '缺少登录凭证,请先登录' };
return;
}
let principalId = null;
let recordMissing = false;
if (modelName && ctx.params && ctx.params.id != null) {
// 资源按 id 访问:以记录真实的 principal_id 鉴权
const Model = ctx.model[modelName];
if (Model) {
const record = await Model.findByPk(ctx.params.id, { attributes: [ 'principal_id' ] });
if (record) principalId = record.principal_id;
else recordMissing = true; // 资源不存在,交给控制器返回 404,无需主体校验
}
} else {
// 列表/新建:从入参取主体ID
const body = ctx.request.body || {};
const q = ctx.query || {};
const p = ctx.params || {};
if (body.principal_id != null) principalId = body.principal_id;
else if (q.principal_id != null) principalId = q.principal_id;
else if (p.principal_id != null) principalId = p.principal_id;
else principalId = null;
}
if (principalId == null || principalId === '') {
if (recordMissing) return await next();
ctx.status = 400;
ctx.body = { code: 400, message: '缺少主体ID(principal_id)' };
return;
}
principalId = Number(principalId);
// 校验账号与主体的绑定关系:主体ID列表来自缓存(JWT 中间件已加载),避免每次请求查关联表
const principalIds = ctx.state.principalIds || await ctx.service.cache.getUserPrincipalIds(userId);
const bound = new Set((principalIds || []).map(Number)).has(principalId);
if (!bound) {
ctx.status = 403;
ctx.body = { code: 403, message: '无权操作该主体下的数据' };
return;
}
ctx.state.principalId = principalId;
// 主体停用:数据只读(可查看 / 导出),写操作一律拒绝
const principal = await ctx.service.cache.getPrincipal(principalId);
if (!principal) {
ctx.status = 404;
ctx.body = { code: 404, message: '主体不存在' };
return;
}
const disabled = Number(principal.status) !== 1;
ctx.state.principalDisabled = disabled;
if (disabled && !isReadOnlyRequest(ctx)) {
ctx.status = 403;
ctx.body = { code: 403, message: '主体已停用,仅可查看与导出,不能新建、修改、推送或删除数据' };
return;
}
await next();
};
};
/** 只读方法(不改变数据) */
const READ_METHODS = new Set([ 'GET', 'HEAD', 'OPTIONS' ]);
/**
* 停用主体下仍允许的“读”请求
* - 常规 GET/HEAD/OPTIONS
* - 导出任务提交(POST /api/export-tasks):属于查看数据,按主体停用策略放行
* @param {Object} ctx koa 上下文
* @return {boolean} 是否放行
*/
function isReadOnlyRequest(ctx) {
if (READ_METHODS.has(ctx.method)) return true;
if (ctx.method === 'POST' && ctx.path === '/api/export-tasks') return true;
return false;
}