3

我有这两个模型:

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显示这个地址时,他看不到图像......

有可能这样做吗?

4

1 回答 1

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

或类似于控制器(这将导致 InheritedResourcescurrent_user.pictures作为集合)。

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

于 2012-03-13T21:43:57.340 回答