Cancan работает нормально, когда пользователь вошел в систему. Но когда пользователь является «гостем», я хотел бы, чтобы он мог видеть некоторые фотографии, но не те, где родительский пост Photo имеет установленный флаг ограничения employees_only
.
Проблема в том, что если в массиве @photos
есть какая-либо фотография, связанная с постом employee_only
, Cancan выдаст CanCan::AccessDenied in PhotosController#index
. Но разве load_and_authorize_resource :photo, :through => :event
в контроллере не должно было удалить эти несанкционированные записи из @photos
?
способность.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role? :admin
can :manage, :all
elsif user.role? :moderator
can :read, :all
can :manage, Post
can :manage, Event
elsif user.role? :employee
can :read, :all
can :create, Post
can :update, Post, :user_id => user.id
can :destroy, Post, :user_id => user.id
can :update, User, :id => user.id
can :create, Photo
can :update, Photo, :id => user.id
can :destroy, Photo, :id => user.id
else
can :read, :all
cannot :read, Post, :employee_only => true
cannot :read, Photo, :post => { :employee_only => true } ## <- problem?
end
end
end
событие.rb:
class Event < ActiveRecord::Base
Event has_many :posts
Event has_many :photos, :through => :posts
пост.рб
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :photos, :dependent => :destroy
photos_controller:
class PhotosController < ApplicationController
load_and_authorize_resource :event ## <- problem?
load_and_authorize_resource :photo, :through => :event ## <- problem?
def index
# @event = Event.find(params[:event_id])
# @photos = @event.photos.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @events }
end
end
...
user.rb # не уверен, что это необходимо для решения этой проблемы, но J.I.C.:
class User < ActiveRecord::Base
attr_protected :roles_mask
has_many :posts
has_many :photos, :through => :posts
...