2011-12-23 40 views
3

我有这两种模式:如何设置declarative_authorization为每个用户

class Photo < ActiveRecord::Base 
    belongs_to :user 
end 

class User < ActiveRecord::Base 
    has_many :photos 
end 

declarative_authorization为角色这套起来:

role :reg_ser do 
    has_permission_on :photos, :to => [:show] do 
     if_attribute :user_id => is { user.id } 
    end 
    end 

,我想允许显示器用户他上传的图像 - 只有他的图像。我的意思是,例如:

user_id | photo 
     1 | 1 
     1 | 2 
     1 | 3 
     2 | 4 

,并在用户设定的URL /photos/1,所以这个图像将只显示user_id编号为1,当user_id=2会显示这个地址,他没有看到image ...

是否有可能这样做?

+1

你的意思是您正在使用“declarative_authorization” ???如果是这样的话,你已经非常熟悉头部,假设你有一个具有“filter_resource_access”指令的适当命名的控制器。 – dmcnally 2011-12-23 21:26:24

+0

我需要一些睡眠。谢谢你把我踢开! – user984621 2011-12-23 23:23:49

+0

你可以使用康康和设计让事情变得更容易 – Uchenna 2011-12-23 23:45:05

回答

0

你需要把像

filter_access_to [:index, :new, :create] 
filter_access_to [:show, :edit, :update], :attribute_check => true 

在你的控制器和类似

role :reg_ser do 
    has_permission_on :photos, :to => [:index, :new, :create] 
    has_permission_on :photos, :to => [:show, :edit: update] do 
    if_attribute :user_id => is { user.id } 
    end 
end 

authorization_rules.rb。因此,一切工作正常,您必须将:attribute_check包含在控制器中,以查看要检查属性的每个操作。 我离开:index没有它,因为它是相当容易的,只是通过增加

def begin_of_association_chain 
    current_user 
end 

或类似控制器显示当前用户的事情(这是造成InheritedResources有current_user.pictures的集合)。

您可能也有兴趣阅读http://asciicasts.com/episodes/188-declarative-authorization

相关问题