Ruby on rails Ruby:声明性授权多态关联
我有两个模型(项目和主题)。它们都是由第三个模型用户拥有的,用户有很多关联(用户有很多主题和项目)。项目和主题都有很多:图像 图像模型是一个多态关联,因此表中有imageable\u id和imageable\u type列。如果我同时拥有一个ID为1的项目和一个ID为1的主题,那么这个表将是这样的Ruby on rails Ruby:声明性授权多态关联,ruby-on-rails,ruby,polymorphic-associations,declarative-authorization,Ruby On Rails,Ruby,Polymorphic Associations,Declarative Authorization,我有两个模型(项目和主题)。它们都是由第三个模型用户拥有的,用户有很多关联(用户有很多主题和项目)。项目和主题都有很多:图像 图像模型是一个多态关联,因此表中有imageable\u id和imageable\u type列。如果我同时拥有一个ID为1的项目和一个ID为1的主题,那么这个表将是这样的 id imageable_id imageable_type ------------------------------------ 1 1 Ite
id imageable_id imageable_type
------------------------------------
1 1 Item
2 1 Theme
我正在使用声明性_授权来重新编写数据库的SQL查询,以防止用户访问其帐户之外的项目。我想编写一个授权规则,允许用户仅在能够读取自己拥有的项目时才能读取图像。我似乎无法获得正确的语法(可能不支持):
然后,我会有另一个类似于上述规则的规则,但对于主题:
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
有什么想法吗?提前谢谢
- 科里思·马林
- 您似乎在
拥有方法的权限时犯了错误
当我反复检查时
希望这对你有帮助 非常旧的gem,似乎不支持多态关联:。为什么不使用像cancancan这样的工具呢?我是说。。。当我写这篇文章并在2010年发布时,它已经不是一块古老的宝石了。@Corith Malin如果答案对你有用,请接受——Gupta
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(:images, :to => :manage, :join_as => :and) do
if_attribute :imageable => "Item"
if_permitted_to :manage, :items
end