繁体   English   中英

Ruby:Declarative_authorization多态关联

[英]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_onif_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM