2016-09-28 29 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