feat(tools): 数据库查询按权限拆分为 user/admin,强制 userId 隔离
- query/ 为内部实现,user/ 仅供 bot-escort(绑定登录用户、schema 不暴露 userId),admin/ 仅供 escort-admin(显式跨用户) - 新增 employee、employee_review 查询;health_profile 用户端补齐 userId 隔离,按 _id 精确查询也带归属条件 - organization 是公共数据且无 userId,不进用户端;机构与写入工具统一移入 admin/ - utils 增加严格 ObjectId 解析,避免 CastError 与畸形 ID 进入查询 - 用户端员工视图剔除手机号、证件号、证书编号、紧急联系人与内部备注 - 新增 test/db_tools.test.js,用模型桩断言查询条件确实带 userId
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
* 数据库工具的公共小工具
|
||||
*/
|
||||
|
||||
import mongoose from "mongoose";
|
||||
|
||||
/**
|
||||
* 转义正则元字符,用于把用户/模型输入当作字面量做模糊匹配
|
||||
*
|
||||
@@ -19,4 +21,20 @@ function escapeRegex(input, maxLength = 64) {
|
||||
.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
export { escapeRegex };
|
||||
/**
|
||||
* 严格解析 ObjectId
|
||||
*
|
||||
* 用固定 24 位十六进制格式校验,而不是 isValid(后者对部分非标准字符串也返回 true)。
|
||||
* 非法值返回 null,供工具提前返回错误,避免 Mongoose CastError 或把字符串直接带进查询。
|
||||
*
|
||||
* @param {*} value 待解析的 ID
|
||||
* @returns {mongoose.Types.ObjectId|null}
|
||||
*/
|
||||
function parseObjectId(value) {
|
||||
if (value === null || value === undefined) return null;
|
||||
const raw = typeof value === "string" ? value.trim() : String(value);
|
||||
if (!/^[0-9a-fA-F]{24}$/.test(raw)) return null;
|
||||
return new mongoose.Types.ObjectId(raw);
|
||||
}
|
||||
|
||||
export { escapeRegex, parseObjectId };
|
||||
|
||||
Reference in New Issue
Block a user