簡體   English   中英

在聲明性授權 if_attribute 中向用戶提供多個條件

[英]multiple conditions to user in declarative_authorization if_attribute

我有 4 個不同級別的訪問權限; 管理員、合作伙伴、員工和客戶。 Admin、Partner 和 Employee 對客戶端具有管理訪問權限。 基本上我所做的是創建了一個 Rails 應用程序,客戶可以在其中將文檔上傳給他們的供應商(合作伙伴)。 除了客戶端級別的訪問之外,一切都很好。 我有兩個代碼; partercode 和客戶端代碼。 合作伙伴只能查看文件的 partercode 字段等於合作伙伴的合作伙伴代碼的文件。 這工作得很好。 但是,客戶端只能看到合作伙伴代碼匹配且客戶端代碼匹配的文件。 下面是我的客戶部分。 起作用的是客戶端只允許查看partercode文件,但它正在設置它們查看屬於合作伙伴的其他客戶端。 (通過在 URL 中輸入數字)。 我懷疑這永遠不會是一個問題,但這是一個我絕對希望關閉的安全漏洞。

  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

我不認為嵌套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

:join_by有效,但我的問題是有時你需要ANDOR 您可以在同一行上指定多個 if 條件作為不同的 arguments。 這些將使用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

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

  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 以檢查合作伙伴代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM