2016-04-11 21 views
0

Gibt es eine Chance, authentifizieren Doorkeeper-Methode von einem Rails Action Controller zugreifen? Ich möchte die Authentifizierung nur für eine meiner Aktionen überspringen ('show'), aber wenn eine bestimmte Bedingung zutrifft, möchte ich die Methode apiauthenticate aufrufen, um ihre Aufgabe zu erledigen. In der Aktion 'show' überprüfe ich zuerst eine Bedingung, und wenn dies nicht zutrifft, muss ich die api_authenticate aktivieren. Ich starte einen Test, der api_authenticate aufrufen und dort stoppen soll. Aber aus irgendeinem Grund geht es weiter und es hört nicht auf. Access Doorkeeper von Aktion Rails Controller

Dies ist der Code meiner Controller

skip_before_action :api_authenticate, only: :show 

def show 
    param! :id, String, required: true 

    post = Services::Posts.find(params[:id]) 

    if post.public 
    @post = post 
    @user_id = nil 
    else 
    api_authenticate 
    ap "it shouldnt get here if user is not logged in" 
    user = current_resource_owner 
    @post = Services::Posts.show(params[:id], user) 
    @user_id = user.identity rescue nil 
    end 
end 

#more actions.... 

Und das ist die api_controller.rb, wo ich die authenticate Methode haben

class ApiController < ApplicationController 
    protect_from_forgery with: :null_session 

    # Check the user is authenticated 
    before_action :api_authenticate 

    rescue_from ActionController::RoutingError, :with => :route_error 
    rescue_from ::AbstractController::ActionNotFound, :with => :action_error 
    rescue_from Exception, :with => :base_error if Rails.env.production? 

    def api_authenticate 
    doorkeeper_authorize!() 
    end 
end 

Antwort

1

ich etwas Ähnliches umgesetzt haben. Habe den untenstehenden Code nicht getestet, aber es sollte funktionieren.

skip_before_filter :doorkeeper_authorize! , only: :show 

def show 
    param! :id, String, required: true 

    post = Services::Posts.find(params[:id]) 

    if post.public 
    @post = post 
    @user_id = nil 
    else 
    doorkeeper_authorize! 
    ap "it shouldnt get here if user is not logged in" 
    user = current_resource_owner 
    @post = Services::Posts.show(params[:id], user) 
    @user_id = user.identity rescue nil 
    end 
end 

Die Api-Controller,

class ApiController < ApplicationController 
    protect_from_forgery with: :null_session 

    # Check the user is authenticated 
    before_action :doorkeeper_authorize! 

    rescue_from ActionController::RoutingError, :with => :route_error 
    rescue_from ::AbstractController::ActionNotFound, :with => :action_error 
    rescue_from Exception, :with => :base_error if Rails.env.production? 

    def doorkeeper_unauthorized_render_options(error: nil) 
    response_hash = { status: false, description: error.description, expired: false } 
    response_hash[:expired] = true if error.description == "The access token expired" 
    { json: response_hash } 
    end 

end 

Wenn das Problem weiterhin besteht, fügen Sie bitte die params zur Show Aktion übergeben.