代码之家  ›  专栏  ›  技术社区  ›  Corith Malin

Ruby:声明性授权多态关联

  •  7
  • Corith Malin  · 技术社区  · 16 年前

    我有两个模型(项目和主题)。它们都由第三个模型用户拥有,用户有很多关联(用户有很多主题和项目)。项目和主题都有很多:图片。

    图像模型是一个多态关联,因此表中有imageable\u id和imageable\u type列。如果我同时拥有一个ID为1的项和一个ID为1的主题,那么这个表就会是这样的

    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 { "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
    

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

    • 科里斯·马林
    1 回复  |  直到 16 年前
        1
  •  0
  •   Gupta    10 年前

    看来你犯了一个错误 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
    

    推荐文章