1

我有4個不同的訪問級別;管理員,合作伙伴,員工和客戶。管理員,合作伙伴和員工可以對客戶進行管理訪問。基本上我所做的是創建一個Rails應用程序,客戶可以將文檔上傳到供應商(合作伙伴)。除了客戶端級別的訪問,一切都很好。我有兩個代碼; partercode和客戶端代碼。合作伙伴只能看到文件的partercode字段等於合作伙伴的合作伙伴代碼的文件。這工作得很好。但是,客戶端只能查看合作伙伴代碼匹配和客戶端代碼匹配的文件。以下是我的客戶部分。工作原理是,客戶端只允許看到partercode文件,但它設置他們看到屬於合作伙伴的其他客戶端。 (通過輸入URL中的數字)。我懷疑這會成爲一個問題,但這是一個安全漏洞,我肯定想要關閉它。在declarative_authorization中給用戶多個條件if_attribute

role :client do 
    has_permission_on [:users], :to => [:client, :edit, :update] do 
     if_attribute :username => is { user.username } 
    end 
    has_permission_on [:documents], :to => [:client, :new, :create] 
    has_permission_on [:documents], :to => [:client, :index, :show, :edit, :update, :destroy, :documents] do 
     if_attribute :partnercode => is { user.partnercode }, :clientcode => is { user.clientcode } 
    end 
    end 

回答

0

我找到了答案。具有諷刺意味的是,這個問題的答案在於這個問題的標籤。嵌套。

role :client do 
    has_permission_on [:users], :to => [:client, :edit, :update] do 
     if_attribute :username => is { user.username } 
    end 
    has_permission_on [:documents], :to => [:client, :new, :create] 
    has_permission_on [:documents], :to => [:client, :index, :show, :edit, :update, :destroy, :documents] do 
     if_attribute :clientcode => is { user.clientcode } do 
     if_attribute :partnercode => is { user.partnercode } 
     end 
    end 
    end 

一旦我確認客戶端代碼是正確的,然後創建另一個do .. end來檢查夥伴代碼。

3

我不認爲嵌套的if_attribute聲明實際上是這樣的。 Declarative_authorization默認將if_attribute語句與or語句連接起來,但如果我理解正確,則希望它們與and連接。更改此方法的方法是將:join_by屬性設置爲:and

role :client do 
    has_permission_on [:documents], :to => [:do_whatever], :join_by => :and do 
    if_attribute :partnercode => is { user.partnercode } 
    if_attribute :clientcode => is { user.clientcode } 
    end 
end 

來源:http://www.tzi.org/~sbartsch/declarative_authorization/master/classes/Authorization/Reader/AuthorizationRulesReader.html#M000184

0

:join_by的作品,但我與它的問題是,有時你需要同時ANDOR。您可以指定多個if條件,並將其作爲不同參數在一行中指定。這些將使用AND進行比較,而每條線則使用OR進行評估。

role :client do 
    has_permission_on [:documents], :to => [:do_whatever], :join_by => :and do 
    if_attribute :partnercode => is { user.partnercode }, 
       :clientcode => is { user.clientcode } # These two are evaluated with AND 
    if_attribute :some_attr => is { some_val } 
    end 
end