0

4 응용 프로그램 I 컨트롤러에서 index, create, show, updatedestroy 방법에 액세스 할 수있는 API를 만드는거야.레일 필터 사용하기 전에 내 레일에서

인증을 위해 사용자 이름과 비밀번호 (access_key = username, secret_key = passwordmaster_secret_key = password) 대신 HTTP 기본을 사용하여 특정 방법에 액세스합니다.

나는 access_key/secret_key 쌍 만 createupdate, destroy 방법과 access_key/master_secret_keyindexshow 방법에 대한 액세스를 허용하는 액세스 좋아 (뿐만 아니라 다른 사람) 것입니다.

인증이 작동하지만 요청 방법에 따라 인증 방법을 Rails에 알려주는 데 문제가 있습니다.

제안 사항?

class Api::V1::TestsController < ApplicationController 
    before_filter { :authenticate! || :authenticate_with_master! } 
    skip_before_filter :verify_authenticity_token 
    respond_to :json 

    def index 
    end 

    def create 
    end 

    def show 
    end 

    def update 
    end 

    def destroy 
    end 

    protected 

    def authenticate! 
    authenticate_or_request_with_http_basic do |access_key, secret_key| 
     @credential = Credentials.find_by_access_key(access_key) 
     (@credential.secret_key == secret_key) ? true : false 
    end 
    end 

    def authenticate_with_master! 
    authenticate_or_request_with_http_basic do |access_key, master_secret_key| 
     @credential = Credentials.find_by_access_key(access_key) 
     (@user.master_secret_key == master_secret_key) ? true : false 
    end 
    end 
end 

답변

2

이 before_filter을위한 '전용'매개 변수를 시도하고

before_action :authenticate!, only: [:create, :update, :destroy] 

before_action :authenticate_with_master! , only: [:index, :show] 

4가 도움이되기를 바랍니다 레일에 before_action를 사용

는 여기에 지금까지 가지고있는 코드입니다. 감사합니다.