2016-09-28 77 views
2

我开始得到相当看起来像这几个帆政策:帆政策 - 利用变量

... 
'MyController':{ 
    'some': ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled'], 
    'action': ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled'], 
    'here': ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled', 'extra'], 
}, 
.... 

的问题是重复的。我想要有一个快捷方式,如“userIsAuthenticated”。

我可以声明一个变量

var userIsAuthenticated = ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled']; 
.... 

'MyController':{ 
    'some': userIsAuthenticated, 
    'action': userIsAuthenticated, 
    'here': Array.concat(userIsAuthenticated, ['extra'], 
}, 

我的问题是这里的行动。我认为语法很难阅读和容易出错。

我试着写:

'MyController':{ 
    'some': userIsAuthenticated, 
    'action': userIsAuthenticated, 
    'here': [userIsAuthenticated, 'extra'], 
}, 

这看上去很美。它可能有效。虽然我明白我得到一个数组有两个项目,第一个项目是一个包含3个项目的数组。

所以,问题是。在Sails中声明这样的策略是否安全?我在手册中找不到任何提及的语法(http://sailsjs.org/documentation/concepts/policies)。有没有其他方法?

回答

2

所以,问题是。宣布像Sails或 这样的政策不安全。我在手册 (http://sailsjs.org/documentation/concepts/policies)中找不到任何提及的语法。也许有其他一些方法吗? ?

通过使用

var userIsAuthenticated = ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled']; 
.... 

'MyController':{ 
    'some': userIsAuthenticated, 
    'action': userIsAuthenticated, 
    'here': Array.concat(userIsAuthenticated, ['extra'], 
}, 

'MyController':{ 
    'some': userIsAuthenticated, 
    'action': userIsAuthenticated, 
    'here': [userIsAuthenticated, 'extra'], 
}, 

你赢了什么。你让更难理解其他开发人员,因为文档描述它这样

... 
'MyController':{ 
    'some': ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled'], 
    'action': ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled'], 
    'here': ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled', 'extra'], 
}, 
.... 

但我知道你的感受。那么,你所能做的就是在模型或控制器级别定义策略:不过

module.exports.policies = { 
    '*': true, 
    MyModel: ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled'], // model level 
    MyController: { // controller level  
     '*': ['runPassport', 'isLoggedIn', 'ensureTotpOkIfEnabled'], 
     fooAction: 'extra' 
    } 
}; 

的问题是,fooAction不继承的*政策是怪异。因此只有extra政策将被要求fooAction。所以你的问题没有真正的解决方案。您可以在这里创建一个提案https://github.com/balderdashy/sails/issues

+0

好的,谢谢。猜猜没有完美的方式。 – tkarls