简体   繁体   English

Ruby:Declarative_authorization多态关联

[英]Ruby: Declarative_authorization polymorphic associations

I have two models (Item and Theme). 我有两个模型(项目和主题)。 They are both owned by a third model Users with a has_many association (User has many Themes and Items). 它们都归第三个模型所有,具有has_many关联的用户(用户拥有许多主题和项目)。 Both Item and Theme have_many :images. 项目和主题都具有多种:图像。

The Image model is a polymorphic association so the table has the columns imageable_id and imageable_type. Image模型是一个多态关联,因此该表的列为imageable_id和imageable_type。 If I had both an Item with ID 1 and a Theme with ID 1 the table would look like 如果我同时拥有ID为1的Item和ID为1的Theme,则该表将如下所示

id    imageable_id    imageable_type
------------------------------------
1     1               Item
2     1               Theme

I'm using declarative_authorization to re-write the SQL queries of my database to keep users from accessing items outside their account. 我正在使用declarative_authorization来重写我的数据库的SQL查询,以防止用户访问其帐户外的项目。 I'd like to write an authorization rule that will allow a user to read an image only if they can read the item they own. 我想编写一个授权规则,允许用户只有在他们可以阅读他们拥有的项目时才能阅读图像。 I can't seem to get the correct syntax (perhaps it's not supported): 我似乎无法获得正确的语法(也许它不受支持):

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

Then I'd have another rule mimicking the above but for themes: 然后我会有另一个模仿上述但主题的规则:

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

Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

  • Corith Malin 科里斯马林

It seems that you commit a mistake in has_permission_on method 看来你在has_permission_on方法中犯了一个错误

As I checked over has_permission_on and if_attribute 当我查看has_permission_onif_attribute时

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

Hope this Help You !!! 希望这能帮到你!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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