如何在 sails 生成的路线上应用多个策略,如下所示: /users/:id/orders
。我可以在 config/routes.js 中应用这样的单个策略
'/users/:id/orders' : {
policy : 'isAuthenticated'
}
但是如何以类似的方式应用多个策略
最佳答案
遗憾的是文档http://sailsjs.org/documentation/concepts/routes/custom-routes#?policy-target-syntax不讨论路由中的链接策略。
作为替代方案,您可以保护用户 Controller 中的填充操作,如下所示:编辑 config/policies.js
UserController: {
populate: ['isAuthenticated', 'isAllowed']
}
http://sailsjs.org/documentation/reference/blueprint-api/populate-where
如果您只想将策略应用于订单关联,则可以从策略内的 req 对象中检索关联参数 (/:model/:id/:association
) 并处理您的情况:
module.exports = function(req, res, next) {
if (req.param('association') == 'orders') {
// do your magic
} else {
return next();
}
};
关于node.js - 在航线上的 sails 中应用多个策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39654172/