11

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

兔子控制器:{

    '*': false, 

    nurture    : 'isRabbitMother',

    feed : ['isNiceToAnimals', 'hasRabbitFood']
}

有没有办法将参数传递给这些 acl,例如:

兔子控制器:{

    '*': false, 

    nurture    : 'isRabbitMother(myparam)',

    feed : ['isNiceToAnimals(myparam1, myparam2)', 'hasRabbitFood(anotherParam)']
}

这可能导致将这些函数多次用于不同的参数。谢谢阿里夫

4

3 回答 3

16

策略是带有签名的中间件函数:

    function myPolicy (req, res, next)

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

    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中,但这应该可以帮助您入门。

于 2014-03-10T04:21:29.383 回答
0

我创建了一个完成这项工作的 Sails 钩子:https ://www.npmjs.com/package/sails-hook-parametized-policies

我仍然需要为它编写文档,但您可以查看测试文件夹以了解它是如何工作的。

您只需要创建一个文件api/policiesFactories/isNiceTo.js

module.exports = function(niceTo){
    return function(req, res, next){
        // policy code
    };
};

config/policies.json

{
    RabbitController: {
        '*': false, 
        nurture: 'isRabbitMother(\'myparam\')',
        feed : ['isNiceToAnimals(\'myparam1\', \'myparam2\')', 'hasRabbitFood(\'anotherParam\')']
    }
}
于 2015-06-16T10:39:26.077 回答
0

退房风帆必须

// in config/policies.js 

var must = require('sails-must')();

module.exports = {
    //.. 
    RabbitController: {
        nurture: must().be.a('rabbit').mother,
        feed: [must().be.nice.to('rabbits'), must().have('rabbit').food]
    },

    DogController: {
        nurture: must().be.a('dog').mother,
        feed: [must().be.nice.to('dogs'), must().have('dog').food]
    }
    //.. 

    //.. 
    SomeController: {
        someAction: must().be.able.to('read', 'someModel'),
        someOtherAction: must().be.able.to('write', 'someOtherModel').or.be.a.member.of('admins'),
        someComplexAction: must().be.able.to(['write', 'publish'], 'someDifferentModel')
    }
    //.. 

    //.. 
    ProjectController: {
        sales: must().be.a.member.of('sales').or.a.member.of('underwriting'),
        secret: must().not.be.a.member.of('hr')
    }
    //.. 

    //.. 
    MovieController: {
        adults: must().be.at.least(18, 'years').old,
        kids: must().be.at.most(17, 'years').old,
        teens: [must().be.at.least(13, 'years').old, must().be.at.most(19, 'years').old]
    }
    //.. 
};
于 2015-08-31T04:58:04.183 回答