[英]Ruby: Declarative_authorization polymorphic associations
我有兩個模型(項目和主題)。 它們都歸第三個模型所有,具有has_many關聯的用戶(用戶擁有許多主題和項目)。 項目和主題都具有多種:圖像。
Image模型是一個多態關聯,因此該表的列為imageable_id和imageable_type。 如果我同時擁有ID為1的Item和ID為1的Theme,則該表將如下所示
id imageable_id imageable_type
------------------------------------
1 1 Item
2 1 Theme
我正在使用declarative_authorization來重寫我的數據庫的SQL查詢,以防止用戶訪問其帳戶外的項目。 我想編寫一個授權規則,允許用戶只有在他們可以閱讀他們擁有的項目時才能閱讀圖像。 我似乎無法獲得正確的語法(也許它不受支持):
has_permission_on [:images], :to => [:manage], :join_as => :and do
if_attribute :imageable => is { "Item" }
if_permitted_to :manage, :items # Somehow I need to tell declarative_auth to imageable_id is an item_id in this case.
end
然后我會有另一個模仿上述但主題的規則:
has_permission_on [:images], :to => [:manage], :join_as => :and do
if_attribute :imageable => is { "Theme" }
if_permitted_to :manage, :themes # Somehow I need to tell declarative_auth to imageable_id is a theme_id in this case.
end
有任何想法嗎? 提前致謝!
看來你在has_permission_on
方法中犯了一個錯誤
當我查看has_permission_on和if_attribute時
has_permission_on(:images, :to => :manage, :join_as => :and) do
if_attribute :imageable => "Item"
if_permitted_to :manage, :items
end
希望這能幫到你!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.