将参数传递给sails.js策略 [英] Passing Parameters to sails.js policies
问题描述
Sails.js(0.9v)控制器的策略定义为:
Sails.js (0.9v) controllers have policies defined as:
RabbitController:{
RabbitController: {
'*': false,
nurture : 'isRabbitMother',
feed : ['isNiceToAnimals', 'hasRabbitFood']
}
是否可以将参数传递给这些acl,例如:
is there a way to pass params to these acls eg:
RabbitController:{
RabbitController: {
'*': false,
nurture : 'isRabbitMother(myparam)',
feed : ['isNiceToAnimals(myparam1, myparam2)', 'hasRabbitFood(anotherParam)']
}
这可能导致针对不同的参数多次使用这些功能. 谢谢 阿里夫(Arif)
This may lead to multiple use of these functions for different params. Thanks Arif
推荐答案
策略是具有签名的中间件功能:
The policies are middleware functions with the signature:
function myPolicy (req, res, next)
无法为这些功能指定其他参数.但是,您可以创建包装函数来动态创建策略:
There's no way to specify additional parameters for these functions. However, you could create wrapper functions to create the policies dynamically:
function policyMaker (myArg) {
return function (req, res, next) {
if (req.params('someParam') == myArg) {
return next();
} else {
return res.forbidden();
}
}
}
module.exports = {
RabbitController: {
// create a policy for the nurture action
nurture: policyMaker('foo'),
// use the policy at
// /api/policies/someOtherPolicy.js for the feed action
feed: 'someOtherPolicy'
}
}
在实践中,您想将此代码分成另一个文件并require
,但这应该可以帮助您入门.
In practice you'd want to separate this code into another file and require
it, but this should get you started.
这篇关于将参数传递给sails.js策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!