我有两个模型(项目和主题)。它们都由第三个模型用户拥有,用户有很多关联(用户有很多主题和项目)。项目和主题都有很多:图片。
图像模型是一个多态关联,因此表中有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
有什么想法吗?提前谢谢!