我有两个模型(项目和主题)。它们都由具有 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
有任何想法吗?提前致谢!
- 科里斯·马林