我有两个模型(项目和主题)。它们都由具有has_many关联的第三个模型用户拥有(用户有许多主题和项)。项目和主题have_many :图像。
图像模型是一个多态关联,因此表有列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有什么想法吗?提前感谢!
https://stackoverflow.com/questions/3640239
复制相似问题