fix: 管理员判定统一口径,订单字段按状态收敛并校验档案归属
新增 utils/permissions.js 供路由、订单 handler 与 WS 共用;用户端可改字段改为按订单状态的白名单,进行中仅可改就诊人备注;下单丢弃客户端 attendant;改档校验档案归属并刷新就诊人快照。
This commit is contained in:
+76
-17
@@ -1,6 +1,7 @@
|
||||
import { DBModel } from "../models/index.js";
|
||||
import services from "../resource/services.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
import { isAdmin as isAdminUser } from "../utils/permissions.js";
|
||||
|
||||
class HandlerEscortRecord {
|
||||
constructor() {
|
||||
@@ -13,10 +14,20 @@ class HandlerEscortRecord {
|
||||
"shippingAddress",
|
||||
];
|
||||
|
||||
// 用户端可自助修改的字段:费用、状态、陪诊员分配等一律由平台控制
|
||||
static OWNER_UPDATE_FIELDS = [
|
||||
"patient", "healthProfileId", "hospital", "schedule", "notes",
|
||||
// 用户自助可改字段按订单状态区分(费用、状态、陪诊员分配、档案归属一律由平台控制):
|
||||
// - 待支付/已支付:可改医院与排期(改期会写审计时间)
|
||||
// - 进行中:服务已开始,仅可补充就诊人备注,排期与医院不允许用户单方变更
|
||||
// - 终态:全部不可改
|
||||
static OWNER_NOTE_FIELDS = ["notes.patientNote"];
|
||||
static OWNER_BOOKING_FIELDS = [
|
||||
"hospital.province", "hospital.name", "hospital.address", "hospital.department",
|
||||
"hospital.doctor", "hospital.medicalRecordNo", "schedule.date", "schedule.startTime",
|
||||
];
|
||||
static OWNER_EDITABLE_BY_STATUS = {
|
||||
pending: ["notes.patientNote", ...HandlerEscortRecord.OWNER_BOOKING_FIELDS],
|
||||
confirmed: ["notes.patientNote", ...HandlerEscortRecord.OWNER_BOOKING_FIELDS],
|
||||
in_progress: ["notes.patientNote"],
|
||||
};
|
||||
|
||||
// 用户端只能取消;服务完成由平台确认,避免用户端重复写入终态。
|
||||
static OWNER_ALLOWED_STATUS = ["cancelled"];
|
||||
@@ -36,7 +47,31 @@ class HandlerEscortRecord {
|
||||
* @param {Object} user token 解析出的用户
|
||||
*/
|
||||
static isAdmin(user) {
|
||||
return !!(user && user.app && ("wxapp-escort-admin" in user.app));
|
||||
return isAdminUser(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户端在当前状态下可自助修改的字段清单(白名单精确匹配,点号路径)
|
||||
* @param {string} status 订单状态
|
||||
* @returns {string[]}
|
||||
*/
|
||||
static ownerEditableFields(status) {
|
||||
return HandlerEscortRecord.OWNER_EDITABLE_BY_STATUS[status] || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 就诊人快照:下单与更换档案共用,保证订单里的展示字段一致
|
||||
* @param {Object} profile 健康档案文档
|
||||
* @returns {Object}
|
||||
*/
|
||||
static buildPatientSnapshot(profile) {
|
||||
const patient = {
|
||||
name: profile.profile?.name || "",
|
||||
mobile: profile.profile?.mobile || "",
|
||||
birth: profile.profile?.birth || "",
|
||||
};
|
||||
if (["male", "female"].includes(profile.profile?.sex)) patient.sex = profile.profile.sex;
|
||||
return patient;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,11 +85,17 @@ class HandlerEscortRecord {
|
||||
return String(record.userId) === String(user?._id);
|
||||
}
|
||||
|
||||
static pickFields(body, allowFields = HandlerEscortRecord.RECORD_FIELDS) {
|
||||
/**
|
||||
* 从入参中挑出白名单字段
|
||||
* @param {Object} body 请求体
|
||||
* @param {string[]} allowFields 允许的字段(支持点号路径)
|
||||
* @param {boolean} exact 是否精确匹配;用户端用 true,防止 notes.patientNote.x 这类嵌套越界
|
||||
*/
|
||||
static pickFields(body, allowFields = HandlerEscortRecord.RECORD_FIELDS, exact = false) {
|
||||
const picked = {};
|
||||
for (const key of Object.keys(body || {})) {
|
||||
const allowed = allowFields.some(
|
||||
(f) => key === f || key.startsWith(f + ".")
|
||||
(f) => key === f || (!exact && key.startsWith(f + "."))
|
||||
);
|
||||
if (allowed) {
|
||||
picked[key] = body[key];
|
||||
@@ -273,14 +314,13 @@ class HandlerEscortRecord {
|
||||
if (addressResult.error) return ResponseUtil.badRequest(ctx, addressResult.error);
|
||||
|
||||
const payload = HandlerEscortRecord.pickFields(body);
|
||||
// 陪诊员分配由平台决定:用户下单时提交的 attendant 一律丢弃,防止自选/伪造陪诊员
|
||||
if (!HandlerEscortRecord.isAdmin(ctx.state.user)) delete payload.attendant;
|
||||
payload.userId = userId;
|
||||
// healthProfileId 以校验通过的档案为准,覆盖客户端原值
|
||||
payload.healthProfileId = profile._id;
|
||||
payload.status = "pending";
|
||||
payload.patient = {
|
||||
name: profile.profile?.name || "",
|
||||
mobile: profile.profile?.mobile || "",
|
||||
birth: profile.profile?.birth || "",
|
||||
};
|
||||
if (["male", "female"].includes(profile.profile?.sex)) payload.patient.sex = profile.profile.sex;
|
||||
payload.patient = HandlerEscortRecord.buildPatientSnapshot(profile);
|
||||
payload.escort = {
|
||||
serviceId: service.id,
|
||||
serviceName: service.title,
|
||||
@@ -331,15 +371,30 @@ class HandlerEscortRecord {
|
||||
}
|
||||
|
||||
const isAdmin = HandlerEscortRecord.isAdmin(ctx.state.user);
|
||||
if (!isAdmin && ["completed", "cancelled"].includes(existing.status)) {
|
||||
// 非管理员的可改字段随订单状态收敛:终态无可改字段,进行中只剩就诊人备注
|
||||
const allowFields = isAdmin
|
||||
? HandlerEscortRecord.RECORD_FIELDS
|
||||
: HandlerEscortRecord.ownerEditableFields(existing.status);
|
||||
if (allowFields.length === 0) {
|
||||
return ResponseUtil.forbidden(ctx, "已结束的订单不可修改");
|
||||
}
|
||||
|
||||
// 非管理员只能改就诊信息类字段,费用/状态/归属/陪诊员分配不可自助修改
|
||||
const allowFields = isAdmin
|
||||
? HandlerEscortRecord.RECORD_FIELDS
|
||||
: HandlerEscortRecord.OWNER_UPDATE_FIELDS;
|
||||
const payload = HandlerEscortRecord.pickFields(update, allowFields);
|
||||
const payload = HandlerEscortRecord.pickFields(update, allowFields, !isAdmin);
|
||||
if (Object.keys(payload).length === 0) {
|
||||
// 例如进行中订单提交医院/排期:字段被状态规则过滤,明确告知而不是静默成功
|
||||
return ResponseUtil.badRequest(ctx, "该订单当前状态不支持修改这些信息");
|
||||
}
|
||||
|
||||
// 档案归属校验:档案必须属于订单所属用户,避免把订单挂到他人档案上;
|
||||
// 通过后同步刷新订单里的就诊人快照
|
||||
if (payload.healthProfileId) {
|
||||
const profile = await DBModel.HealthProfile.findProfileById(payload.healthProfileId);
|
||||
if (!profile || String(profile.userId) !== String(existing.userId)) {
|
||||
return ResponseUtil.badRequest(ctx, "就诊人档案不存在");
|
||||
}
|
||||
payload.patient = HandlerEscortRecord.buildPatientSnapshot(profile);
|
||||
}
|
||||
const service = services.find((item) => item.id === existing.escort?.serviceId);
|
||||
if (!service) return ResponseUtil.badRequest(ctx, "订单关联的服务已下架");
|
||||
|
||||
@@ -352,6 +407,10 @@ class HandlerEscortRecord {
|
||||
for (const key of Object.keys(payload)) {
|
||||
if (key.startsWith("schedule.")) delete payload[key];
|
||||
}
|
||||
// 已支付后的改期留审计时间,便于客服追溯谁在何时改过排期
|
||||
if (existing.status !== "pending") {
|
||||
scheduleResult.schedule.scheduleUpdatedAt = new Date();
|
||||
}
|
||||
payload.schedule = scheduleResult.schedule;
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -5,6 +5,7 @@ import { HandlerResource } from "../handler/resource.js";
|
||||
import { HandlerEmployee } from "../handler/employee.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
import { getUserByToken } from "../utils/user_service.js";
|
||||
import { isAdmin } from "../utils/permissions.js";
|
||||
|
||||
/**
|
||||
* 鉴权中间件:校验 token 并将用户信息挂到 ctx.state.user
|
||||
@@ -28,11 +29,11 @@ async function requireAuth(ctx, next) {
|
||||
|
||||
/**
|
||||
* 管理员校验中间件:仅管理端小程序绑定的账号可访问管理端接口
|
||||
* 与 WS 管理端 agent 的校验口径保持一致(app 中是否存在 wxapp-escort-admin)
|
||||
* 与 WS 管理端 agent、订单 handler 共用 utils/permissions.js 的同一口径
|
||||
*/
|
||||
async function requireAdmin(ctx, next) {
|
||||
const user = ctx.state.user;
|
||||
if (!user || !user.app || !("wxapp-escort-admin" in user.app)) {
|
||||
if (!isAdmin(user)) {
|
||||
ResponseUtil.forbidden(ctx, "无管理员权限");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 管理端身份判定:路由中间件、订单 handler、WebSocket 入口共用同一口径。
|
||||
*
|
||||
* 为什么不再用「app 里存在 wxapp-escort-admin 键」:
|
||||
* 该键历史上可由 /user/register 的客户端入参写入,任何人都能据此自封管理员。
|
||||
* 现在注册侧已拒绝客户端写 app,但库里可能残留历史账号,
|
||||
* 因此要求显式 role === 'admin';role 缺失的历史账号暂按管理员放行并打告警,
|
||||
* 待库中补齐 app.wxapp-escort-admin.role = ['admin'] 后再收紧(见 doc/api-review-plan.md B1)。
|
||||
*/
|
||||
function isAdmin(user) {
|
||||
const entry = user?.app?.["wxapp-escort-admin"];
|
||||
if (!entry) return false;
|
||||
|
||||
const roles = Array.isArray(entry.role) ? entry.role : [entry.role].filter(Boolean);
|
||||
if (roles.includes("admin")) return true;
|
||||
|
||||
if (entry.role === undefined) {
|
||||
console.warn(
|
||||
"[permissions] 管理员账号缺少 role 字段,请补齐 app.wxapp-escort-admin.role = ['admin']"
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export { isAdmin };
|
||||
+2
-1
@@ -4,6 +4,7 @@ import { DBModel } from "./models/index.js";
|
||||
import { chatTask } from "./agent/bots/bot-escort/task.js";
|
||||
import { adminAgent } from "./agent/bots/bot-escort-admin/agent.js"
|
||||
import { getUserByToken } from "./utils/user_service.js";
|
||||
import { isAdmin } from "./utils/permissions.js";
|
||||
|
||||
let systemMessageSequence = 0;
|
||||
|
||||
@@ -115,7 +116,7 @@ export default class WebSocketServerManager {
|
||||
}
|
||||
if (msg.agent === 'escort-admin') {
|
||||
// 管理端 agent 含写库/环境变量等高危工具,必须校验管理员角色,防止普通用户越权
|
||||
if (!userInfo.app || !("wxapp-escort-admin" in userInfo.app)) {
|
||||
if (!isAdmin(userInfo)) {
|
||||
send('system', 'error', '无管理员权限', msg.id);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user