3

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 
    ... 
+1

** Cancan **이 호의에서 벗어났습니다. & 메모를받지 못했습니까? – Meltemi

답변

0

첫째, 당신의 능력 파일에 난 당신이 제한되므로 것이 필요하다고 생각하지 않습니다

cannot :read, Photo, :post => { :employee_only => true } ## <- problem? 

제거 : 414,user.rb 번호 확실하지이이 문제 만 해결하는 JIC 필요하다 게시물에. 컨트롤러의 변화 그리고

load_and_authorize_resource :event      ## <- problem? 
load_and_authorize_resource :photo, :through => :event ## <- problem? 

load_and_authorize_resource :post 
load_and_authorize_resource :photo, :through => :post 

에 나는 무엇을 당신이하려고하는 것은 포스트를 통해 포스트가 employee_only있는 사람으로 제한되어 있기 때문에 사진을로드 믿습니다 = false, 암시 적으로 사진을 제한합니다. 그런 다음 사진 컨트롤러에서 사진을로드하고 권한을 부여하는 데 사용할 수 있도록 게시물을로드해야합니다. Cancan에서 일어날 일은 posts.employee_only = false 인 게시물과 사진 사이에서 내부 조인 (inner join)이 이루어지는 것입니다.