2017-04-07 12 views
1

다른 모델 객체를 기반으로 한 모델이없는 컨트롤러에서 작업을 어떻게 승인 할 수 있습니까?중첩 된 자원에 대한 전문가의 헤드리스 정책

서버라고하는 모델이 있고 해당 모델이없는 config_files_controller라는 중첩 컨트롤러가 있다고 가정 해 보겠습니다.

서버 개체와 현재 사용자 및 서버에 정의 된 정책을 기반으로 config_files_controller에서 작업을 인증 할 수 있기를 원합니다. routes.rb에서

내가 가진 :

:

이 configuration_file_policy.rb에서
class ConfigFilesController < ApplicationController 
    before_filter :authenticate_user! 
    before_filter :load_server 

    def index 
    # displays file names 
    end 

    def load_file 
    # gets file content 
    end 

    private 

    def load_server 
    @server = Server.find(params[:server_id]) 
    authorize :config_file, "#{action_name}?" 
    end 
end 

나는 이런 식으로 뭔가를하고 싶은 :

resources :servers do 
    resources :config_files do 
    collection do 
     get 'load_file' 
    end 
    end 
end 

config_files_controller.rb이 같이 보입니다

class ConfigurationFilePolicy < Struct.new(:user, :configuration_file, :server) 
    def index? 
    ServerPolicy.new(user, server).show? 
    end 

    def load_file? 
    ServerPolicy.new(user, server).update? 
    end 
end 

나는 아마도 뭔가가 누락되었거나 확실한 해결책이 보이지 않고 있습니다. 모든 제안을 부탁드립니다!

감사합니다.

답변

1

컨트롤러가 @server 개체를 설정하고 Server이 모델 인 것으로 보입니다. 그러므로 그것을 승인하는 것으로 충분할 것입니다. (ConfigurationFilePolicy에 대한 필요가 없습니다.)

config_files_controller.rb

... 

def index 
    authorize @server, :show? 
    # displays file names 
    ... 
end 

def load_file 
    authorize @server, :update? 
    # gets file content 
    ... 
end 

https://github.com/elabs/pundit#policies

+0

내가 그것을과 복잡함을 알고 있었다! 고맙습니다! –