import mongoose from "mongoose"; import { DBModel } from "../models/index.js"; const ADMIN_APP = "wxapp-escort-admin"; const WEB_ADMIN_APP = "web-escort-admin"; /** * 管理端应用清单:传统管理小程序 + Web 后台 * * 两者在 user_app 里是彼此独立的成员关系,这里并列是过渡期兼容写法—— * 任一应用内 role=admin 即视为管理员。等 user 服务重构完成、token 能带出 * 应用维度后,改为按请求来源应用分别校验 */ const ADMIN_APP_IDS = [ADMIN_APP, WEB_ADMIN_APP]; /** * 管理端 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)。 * * 用户模型重构后(doc/user-multi-app-plan.md),userInfo 响应会带只读数组 apps, * 此时优先按成员关系判定,status=disabled 的成员不再视为管理员。 */ function isAdmin(user) { if (!user) return false; // 重构后的只读视图:apps: [{ appId, roles, status }] if (Array.isArray(user.apps)) { return user.apps.some( (m) => ADMIN_APP_IDS.includes(m?.appId) && m.status !== "disabled" && Array.isArray(m.roles) && m.roles.includes("admin") ); } // 过渡期:仍读 user.app for (const appId of ADMIN_APP_IDS) { const entry = user.app?.[appId]; if (!entry) continue; const roles = Array.isArray(entry.role) ? entry.role : [entry.role].filter(Boolean); if (roles.includes("admin")) return true; // 历史兼容:role 缺失时按管理员放行并告警 if (entry.role === undefined) { console.warn( `[permissions] 管理员账号缺少 role 字段,请补齐 app.${appId}.role = ['admin']` ); return true; } } return false; } /** * 陪诊员身份判定:以 Employee 记录为唯一可信来源 * * @param {ObjectId|string} userId 登录用户 ID * @returns {Promise} 在职员工档案(只含 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 };