正如我在 cmets 中所说,这种设计有一些业务逻辑,可能使其不适合常规 ACL 类型的安全控制。从表面上看,似乎更容易找出解决方案是在 Mongoose 模型或控制器代码中实现您的业务规则,具体取决于您的偏好。也就是说,使用类似 ACL 的方法执行任何操作的关键在于您的 URL 设计。例如,很容易让您的 API 使所有订单都可以通过/api/orders 获得,并且可能有人会通过/api/orders?userId=12345 查询他们自己的订单。但这使得大多数基于 ACL 的方法都失败了。相反,您必须根据需要保护的层次结构来考虑 API(无论是否所有订单都存储在 Orders Mongoose 模型中,并保留在订单集合中)。
所以以你的第一个需求为例
客户可以下多个订单,并且可以查看他订购了哪些食物
这里的重点是您通过订单的客户“所有者”来保护事物,因此要以这种方式保护它,您需要以这种方式设置您的路线,例如(假设您使用的是您询问的第一个中间件):
app.post('/api/customer/:customerId/orders', acl.middleware(), (req, res, next) => {
const order = new Order(req.body); // TODO: whitelist what info you take in here
order.customerId = req.user.id; // assuming you have a logged-in user that does this
order.save(e => {
if (e) return next(e);
return res.status(201).send(order);
});
});
为了支持这一点,您需要这样注册您的 ACL 信息:
acl.allow('12345', '/api/customer/12345/orders', ['post']);
至少,你会这样做。您可能会提供更多选项,例如“获取”等。您可以猜到,这意味着您需要在创建单个用户时为它们注册权限(以支持“所有权”的概念)。
对于您的第二个要求,
他也可以只在指定的时间段内修改订单,例如在处理订单之前。
尽管我之前说过,如果您真的想这样做,可以说可以在 ACL 中执行此操作。例如,您可以为状态创建 URL 帐户,例如“/api/customers/12345/orders/modifiable/6789”,但根据我的经验,这很难维护。您最好将该逻辑放在控制器或 Mongoose 逻辑中。除非您打算在 Express 应用程序之外使用 Mongoose 模型,否则在控制器中执行此操作可能更简单。像这样的东西(注意,在这种情况下不使用 ACL,但如果你愿意,你可以使用):
app.param('orderId', (req, res, next, id) => {
Order.findById(id, (err, order) => {
if (err) return next(err);
if (order) {
req.order = order;
return next();
}
const notFound = new Error('Order not found');
notFound.status = 404;
return next(notFound);
});
});
app.put('/api/orders/:orderId', (req, res, next) => {
if (req.order.status !== 'pending') {// or whatever your code setup is
const notProcessable = new Error('Cannot modify an order in process');
notProcessable.status = 422;
return next(notProcessable);
}
// handle the modification and save stuff
});