Files
SupplierSystem/server/src/services/orderOwnerScope.ts
T
Ubuntu d7a9b717bd
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(订单范围): 子表归属谓词去掉写死别名 t0(修录单员详情「暂无明细」)
真机:录单员列表有单、详情「暂无明细」。根因是子表谓词写成
EXISTS(... o.order_code = t0.order_code ...),而网关给 order_items 的别名是 t5
⇒ PG 报 missing FROM-clause entry for table "t0" ⇒ 前端 fetchOrderItems 吞错返回 []。

· orderOwnerScope 新增别名无关的父单谓词:
  order_code IN (SELECT order_code FROM orders WHERE created_by::text = $val);
· order_items / order_item_fulfillments / picking_items 三张子表统一走父单归属(不按自身字段过滤);
  orders 本身仍按 created_by;
· 新增真库回归单测(事务造数 + ROLLBACK):自己单 头+明细+履约+分拣可见、他人单全 0、admin 不过滤;
· 设计文补 7.1 真机回归与修正。

验证:根/server tsc 0 错;orderOperatorScope* 37 条 + 相关子集 75 条全过;
全量后端 557 文件通过,1 文件(mainSystemWriteback 2 条)为 HEAD 既有失败。
设计门禁 6 通过/3 红(check-migrations、check-ai-cost、check-env-coverage,均为既有基线)。
2026-10-20 09:44:20 +08:00

112 lines
4.9 KiB
TypeScript
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.
/**
* 订单可见/可操作范围(2026-10-19 开发者需求)。
*
* 「录单员」角色(`operator`)**只能看/改自己录的订单**;管理员/超管/其它角色行为不变。
*
* 判定只认一个字段:`orders.created_by`(落单时写 `userId`)。
* `created_by` 为空的**历史单对 operator 不可见**(空不等于本人)—— 取舍见
* `docs/design/20261019-录单员仅见自己录的订单.md` §二。
*
* 本文件**刻意不 import tenantScope**(否则与 tenantScope 对它的依赖成环),
* 只用结构化最小入参,`TenantContext` 天然满足。
*/
/** 本模块需要的最小主体信息(`TenantContext` 结构上满足) */
export interface OrderScopePrincipal {
userId: string;
roles: string[];
branchCode: string | null;
isSuperAdmin: boolean;
}
/** 越界错误码:operator 访问他人订单(路由据此回 403 `order_scope_forbidden`) */
export const ORDER_SCOPE_FORBIDDEN = 'order_scope_forbidden';
/** operator 越界的统一提示(不泄露他人订单是否存在) */
export const ORDER_SCOPE_FORBIDDEN_MESSAGE = '录单员只能访问自己录入的订单';
/**
* 是否按「仅本人录单」收窄。
*
* - `super_admin` / `admin` 一律**不收窄**(管理员身份优先;同账号多角色时按管理员口径);
* - 只认 `roles` 里的 `operator`,其它角色(finance/warehouse/delivery/purchaser/supplier…)不受影响。
*/
export function isOperatorScoped(
principal: Pick<OrderScopePrincipal, 'isSuperAdmin' | 'roles'>
): boolean {
if (principal.isSuperAdmin) return false;
const roles = principal.roles || [];
if (roles.includes('admin')) return false;
return roles.includes('operator');
}
function forbidden(message: string, code?: string) {
// 不带 code 时行状与改动前一致(路由回 `error: 'server_error'`),避免动到既有前端分支
return Object.assign(new Error(message), code ? { status: 403, code } : { status: 403 });
}
/**
* operator 非本人单 ⇒ 403 `order_scope_forbidden`。
* 非 operator / 管理员 / 超管 ⇒ 直接放行(零行为变化)。
*
* ⚠️ 调用方传进来的 `order` **必须真的带 `created_by`**(SELECT 里别漏列)——
* 漏列会被判成"无归属",operator 连自己的单都打不开。
*/
export function assertOperatorOwnOrder(
principal: OrderScopePrincipal,
order: { created_by?: string | null }
): void {
if (!isOperatorScoped(principal)) return;
const owner = order.created_by ? String(order.created_by) : '';
if (!owner || owner !== String(principal.userId)) {
throw forbidden(ORDER_SCOPE_FORBIDDEN_MESSAGE, ORDER_SCOPE_FORBIDDEN);
}
}
/**
* 订单访问断言(**唯一实现**):分公司边界 + operator 归属边界。
*
* 原来 `orderWorkflowService` 与 `orderDeleteService` 各有一份分公司校验(逐字相同),
* 2026-10-19 合并到此处,避免"两处判断、只改一处"。
*/
export function assertOrderAccess(
principal: OrderScopePrincipal,
order: { branch_code: string | null; created_by?: string | null }
): void {
if (principal.isSuperAdmin) return;
if (!principal.branchCode) {
throw forbidden('当前账号未绑定分公司,无法操作订单');
}
if (order.branch_code && order.branch_code !== principal.branchCode) {
throw forbidden('无权操作其他分公司的订单');
}
assertOperatorOwnOrder(principal, order);
}
/**
* `/api/data` 通用网关的行级过滤(可信表达式,执行时 `$val` = 当前 `userId`)。
* 与 `assertOperatorOwnOrder` 同一口径:`created_by = 当前用户`。
*/
export const OPERATOR_ORDER_OWNER_SCOPE_TRUSTED_EXPR = `t0.created_by::text = $val`;
/**
* 订单**子表**(`order_items` / `order_item_fulfillments` / `picking_items`)没有"自己的归属",
* 归属只认**所属订单**:`created_by` 是订单的字段,不是明细行的字段。
*
* 口径:子表行可见 ⇔ `order_code` 命中的那张订单对当前用户可见。
*
* ⚠️ 左侧 `order_code` **刻意不带外层别名**:`/api/data` 通用执行器给每张表分配的别名不同
* (`queryExecutor.TABLE_ALIASES`:`orders → t0`、`order_items → t5`)。
* 2026-10-19 版本曾写成 `EXISTS (... o.order_code = t0.order_code ...)`,在 `order_items`(别名 t5)
* 上直接报 `missing FROM-clause entry for table "t0"` ⇒ 前端把网关错误吞成空数组
* ⇒ 订单详情「暂无明细」/ 保存区空白(2026-10-20 回归)。不带别名即与执行器别名解耦。
*
* 参数化:`$val` 由白名单编译为 `$N`,值 = 当前 `userId`;不做任何字符串拼接。
*/
export const OPERATOR_ORDER_CHILD_SCOPE_TRUSTED_EXPR = `order_code IN (SELECT order_code FROM orders WHERE created_by::text = $val)`;
/**
* 兼容旧名(`order_items` 与其它订单子表共用同一父单归属表达式)。
*/
export const OPERATOR_ORDER_ITEM_OWNER_SCOPE_TRUSTED_EXPR = OPERATOR_ORDER_CHILD_SCOPE_TRUSTED_EXPR;