将参数传递给sails.js策略

Ari*_*rif 10 parameters acl controllers policies sails.js

Sails.js(0.9v)控制器的策略定义为:

RabbitController:{

    '*': false, 

    nurture    : 'isRabbitMother',

    feed : ['isNiceToAnimals', 'hasRabbitFood']
}
Run Code Online (Sandbox Code Playgroud)

有没有办法将params传递给这些acls,例如:

RabbitController:{

    '*': false, 

    nurture    : 'isRabbitMother(myparam)',

    feed : ['isNiceToAnimals(myparam1, myparam2)', 'hasRabbitFood(anotherParam)']
}
Run Code Online (Sandbox Code Playgroud)

这可能会导致这些函数多次用于不同的参数.谢谢Arif

sgr*_*454 13

策略是具有签名的中间件功能:

    function myPolicy (req, res, next)
Run Code Online (Sandbox Code Playgroud)

无法为这些功能指定其他参数.但是,您可以创建包装函数以动态创建策略:

    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'
      }

    }
Run Code Online (Sandbox Code Playgroud)

在实践中,您希望将此代码分离到另一个文件中require,但这应该让您开始.