Cancan은 사용자가 로그인 할 때 잘 작동합니다. 그러나 사용자가 "게스트"인 경우 사진을 볼 수 있지만 사진은 볼 수 없도록하고 싶습니다. 상위 게시물은 제한 employees_only
플래그가 설정되어 있습니다.Cancan이 예상대로 중첩 된 리소스를로드하지 못합니다.
@photos
배열에 employee_only
게시물과 관련된 사진이 있으면 Cancan은 CanCan::AccessDenied in PhotosController#index
을 던집니다. 하지만 콘트롤러의 load_and_authorize_resource :photo, :through => :event
은 승인되지 않은 레코드를 @photos
에서 제거하지 않아야합니까?
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
ability.rb event.rb :
class Event < ActiveRecord::Base
Event has_many :posts
Event has_many :photos, :through => :posts
post.rb
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
...
이
class User < ActiveRecord::Base
attr_protected :roles_mask
has_many :posts
has_many :photos, :through => :posts
...