陪诊员身份以 Employee 在职档案为准,显式管理员优先;管理端小程序首次登录会写入 role:['user'],不能据此判断。管理员保留跨用户查询与医院资料写入,陪诊员只开放本人订单查询/改单(归属校验、状态与字段白名单、并发保护)与本人档案,患者档案与财务操作不可用。
101 lines
3.5 KiB
JavaScript
101 lines
3.5 KiB
JavaScript
import mongoose from "mongoose";
|
||
import { DBModel } from "../models/index.js";
|
||
|
||
const ADMIN_APP = "wxapp-escort-admin";
|
||
|
||
/**
|
||
* 管理端 app 条目里的角色数组
|
||
*
|
||
* 注意:普通账号首次登录管理端小程序时,user 服务会写入 role: ["user"],
|
||
* 所以「有 wxapp-escort-admin 条目」不等于陪诊员,陪诊员身份必须以 Employee 记录为准。
|
||
*
|
||
* @param {Object} user 登录用户
|
||
* @returns {string[]}
|
||
*/
|
||
function appRoles(user) {
|
||
const entry = user?.app?.[ADMIN_APP];
|
||
if (!entry) return [];
|
||
if (Array.isArray(entry.role)) return entry.role;
|
||
return entry.role ? [entry.role] : [];
|
||
}
|
||
|
||
/**
|
||
* 管理端身份判定:路由中间件、订单 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?.[ADMIN_APP];
|
||
if (!entry) return false;
|
||
|
||
const roles = appRoles(user);
|
||
if (roles.includes("admin")) return true;
|
||
|
||
if (entry.role === undefined) {
|
||
console.warn(
|
||
"[permissions] 管理员账号缺少 role 字段,请补齐 app.wxapp-escort-admin.role = ['admin']"
|
||
);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 陪诊员身份判定:以 Employee 记录为唯一可信来源
|
||
*
|
||
* @param {ObjectId|string} userId 登录用户 ID
|
||
* @returns {Promise<Object|null>} 在职员工档案(只含 role/employeeNo)
|
||
*/
|
||
async function findActiveEmployee(userId) {
|
||
const raw = typeof userId === "string" ? userId.trim() : String(userId ?? "");
|
||
if (!/^[0-9a-fA-F]{24}$/.test(raw)) return null;
|
||
try {
|
||
return await DBModel.Employee.findOne({
|
||
userId: new mongoose.Types.ObjectId(raw),
|
||
status: "active",
|
||
})
|
||
.select("role employeeNo")
|
||
.lean();
|
||
} catch (err) {
|
||
// 员工库查询失败不能把管理员一起拒之门外:返回 null 后仍走 app role 判定
|
||
console.warn("[permissions] 查询员工档案失败:", err.message);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 管理端 Agent 的使用角色
|
||
*
|
||
* - admin:显式管理员,或员工档案为管理员;
|
||
* - attendant:员工档案为在职陪诊员(app role 一般为 ["user"],不能据此判断);
|
||
* - null:无权使用。
|
||
*
|
||
* 先查员工档案再看 app role:历史账号 role 缺失时 isAdmin 会兼容放行,
|
||
* 若该账号同时也是在职陪诊员,必须收敛为陪诊员权限,不能拿到管理员级工具。
|
||
*
|
||
* @param {Object} user 登录用户
|
||
* @returns {Promise<"admin"|"attendant"|null>}
|
||
*/
|
||
async function resolveEscortAdminRole(user) {
|
||
if (!user) return null;
|
||
|
||
const roles = appRoles(user);
|
||
// 显式管理员优先,避免为管理员多查一次员工库
|
||
if (roles.includes("admin")) return "admin";
|
||
|
||
const employee = await findActiveEmployee(user._id);
|
||
if (employee?.role === "admin") return "admin";
|
||
if (employee?.role === "attendant") return "attendant";
|
||
|
||
// 兼容历史管理员(role 缺失);普通管理端账号 role 为 ["user"],不会走到这里
|
||
if (isAdmin(user)) return "admin";
|
||
if (roles.includes("attendant")) return "attendant";
|
||
return null;
|
||
}
|
||
|
||
export { isAdmin, resolveEscortAdminRole };
|