同城生活服务平台选系统时,模块暂停后的服务端规则值得单独验证:停止新业务后,旧待审内容仍需处理,审核员也不能借机操作其他业务。本文用Node.js实现一个只做决策的函数,区分新建与历史处理、规则版本与模块权限,并给出可运行的正常和异常输入。
假设首期开通外卖与信息服务,后来暂停信息发布。首页隐藏信息入口后,用户此前打开的页面可能继续提交;审核员仍需处理已有待审记录。若只检查菜单是否可见,便无法回答服务端是否仍接收新业务。
把两种请求拆开:新建需核当前受理状态与版本,历史处理需核对象归属、岗位授权和对象状态。下面的决策函数不执行数据库写入,用于明确采购验收或开发合同中的输入输出边界。
对象 | 字段与约束 |
|---|---|
policy | version为服务端当前规则版本;accepting为布尔受理状态 |
actor | 由认证层取得;modules为可信授权模块列表 |
request | module、op;create携带整数version,handle携带id |
object | 服务端按id加载;module和state不采信请求覆盖 |
先校验身份和模块授权,再核操作类型。创建操作必须使用当前整数版本且模块仍受理;历史处理不因停止新建而直接拒绝,但要求对象属于请求模块且处于pending。这个顺序避免把存在某个ID误当成有权操作。
真实接口还需对象级、区域级和操作级授权,完整校验请求格式。示例的可信policy与actor是前置条件,不允许客户端直接提交它们。当前函数只是单请求逻辑,不能证明并发下检查与写入原子完成。
保存为module_gate.cjs,使用支持node:assert/strict的Node.js执行 node module_gate.cjs。无需第三方包,检查失败会抛出异常。
'use strict';
const assert = require('node:assert/strict');
function decide(policy, actor, req, objects) {
if (!actor || !Array.isArray(actor.modules)) return 'unauthenticated';
if (!actor.modules.includes(req.module)) return 'forbidden';
if (!['create', 'handle'].includes(req.op)) return 'bad_operation';
if (req.op === 'create') {
if (!Number.isSafeInteger(req.version)) return 'bad_version';
if (req.version !== policy.version) return 'stale_policy';
return policy.accepting ? 'allow_create' : 'closed';
}
const obj = objects.get(req.id);
if (!obj) return 'not_found';
if (obj.module !== req.module) return 'wrong_module';
if (obj.state !== 'pending') return 'not_pending';
return 'allow_handle';
}
// Policy and authenticated identity are loaded by the server, not request JSON.
const policy = {version: 2, accepting: false};
const actor = {modules: ['info']};
const objects = new Map([['I1', {module:'info', state:'pending'}],
['O1', {module:'food', state:'pending'}],
['I2', {module:'info', state:'done'}]]);
const cases = [
[policy,actor,{module:'info',op:'create',version:2},'closed'],
[policy,actor,{module:'info',op:'create',version:1},'stale_policy'],
[policy,actor,{module:'info',op:'create',version:'2'},'bad_version'],
[policy,actor,{module:'info',op:'handle',id:'I1'},'allow_handle'],
[policy,actor,{module:'food',op:'handle',id:'O1'},'forbidden'],
[policy,actor,{module:'info',op:'handle',id:'O1'},'wrong_module'],
[policy,actor,{module:'info',op:'handle',id:'missing'},'not_found'],
[policy,actor,{module:'info',op:'handle',id:'I2'},'not_pending'],
[policy,null,{module:'info',op:'create',version:2},'unauthenticated'],
[{version:2,accepting:true},actor,{module:'info',op:'create',version:2},'allow_create'],
[policy,actor,{module:'info',op:'delete',id:'I1'},'bad_operation']
];
for (const [p,a,r,want] of cases) assert.equal(decide(p,a,r,objects),want);
console.log(JSON.stringify({checks:cases.length,closed:'closed',oldObject:'allow_handle'}));正常的历史待审请求返回allow_handle,即使accepting=false也保留处理路径。相同版本的新建返回closed;旧版本返回stale_policy;请求类型不明返回bad_operation。模块ID与对象归属不一致时返回wrong_module。
运行输出包括checks:11、closed:closed与oldObject:allow_handle,表示这批构造输入的预期结果得到检查。此结果仅是本地示例验证,没有连接任何产品、支付渠道或云服务。
若实际服务使用数据库,规则读取、对象加载和最终写入需要放进适当的事务或条件更新中,并验证并发修改。遇到版本不匹配,应让用户重新查看当前条件,而不是静默执行;返回超时后则查同一业务意图的状态。
试点分别记录新建拒绝、旧对象处理、跨模块拒绝和重复请求的最终状态。选择系统时可以用同一组场景检查不同候选,再结合业务覆盖、费用和维护约定决策,不能由函数运行结果推导产品已经通过验收。
作者:微订(上海逊柯计算机科技有限公司)。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。