【问题标题】:Ruby: Declarative_authorization polymorphic associationsRuby:Declarative_authorization 多态关联
【发布时间】:2011-04-08 02:27:07
【问题描述】:

我有两个模型(项目和主题)。它们都由具有 has_many 关联的第三个模型用户拥有(用户有许多主题和项目)。项目和主题都有_many :images。

Image 模型是一个多态关联,因此该表具有 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

有什么想法吗?提前致谢!

  • 科里斯·马林

【问题讨论】:

    标签: ruby-on-rails ruby polymorphic-associations declarative-authorization


    【解决方案1】:

    看来你在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
    

    希望对你有帮助!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      相关资源
      最近更新 更多