7

我有两个模型(Item和Theme)。他们都拥有has_many关联的第三个模型用户(用户有很多主题和项目)。项目和主题has_many:图像。Ruby:Declarative_authorization多态关联

图像模型是一个多态关联,因此该表具有imageable_id和imageable_type列。如果我有两个一个项目ID为1和ID为1主题表看起来像

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 

任何想法?提前致谢!

  • Corith马林
+0

很古老的宝石,多态关联似乎不被支持:https://github.com/stffn/declarative_authorization/issues/114。 为什么不使用cancancan之类的东西? https://github.com/CanCanCommunity/cancancan – gdurelle 2015-12-22 14:15:40

+0

我的意思是......当我写这篇文章并在2010年发布时,它不是一个古老的宝石。 – 2017-01-31 18:41:19

回答

0

看来你在has_permission_on方法

犯的一个错误当我检查了has_permission_onif_attribute

has_permission_on(:images, :to => :manage, :join_as => :and) do 
    if_attribute :imageable => "Item" 
    if_permitted_to :manage, :items 
    end 

希望这会帮助你! !

+0

@Corith Malin让我们知道我们的答案是否适合你? – VKatz 2015-12-26 16:08:36