ruby-on-railsruby-on-rails-4declarative-authorization

Redirection error when I use Declarative Authorization inside a mounted engine


In my Rails 4 project, I have an engine Blorgh which I made based on Rails Guides Then I mount it in to my app. All are good with no issue.

My current application uses declarative_authorization. Which it works fine inside my application. When I use filter_resource_access in the engine controller I get redirection error.

Log After permission denied:

: Redirected to http://0.0.0.0:3000/blog/ 
: Filter chain halted as :filter_access_filter rendered or redirected
: Completed 302 Found in 3ms (ActiveRecord: 0.3ms)

Thanks guys.

Some codes: engine application_controller.rb

module Blorgh
  class ApplicationController < ActionController::ApplicationController
  end
end

engine posts_controller.rb

module Blorgh
  class PostsController < ApplicationController
    before_action :set_post, only: [:show, :edit, :update, :destroy]    
    filter_resource_access 
...

engine routes.rb

Blorgh::Engine.routes.draw do
  resources :posts do
      resources :comments
  end
  root to: "posts#index"
end

Application routes.rb

 mount Blorgh::Engine, at: "/blog"

Everything is out of the box nothing out of ordinary except for the filter_resource_access in the engine. current_user.inspect in the engine controller returns correct out put which it means I have access to the methods in the Application via engine. Just need to find out how to redirect the user to the Application Home when filter_resource_access returns permission denied.

Please let me know if you need more code.

Thanks again.


Solution

  • Ok, I found the solution for the redirection.

    In case any one has the same issue:

    in the application_controller.rb: add the main_app to the root_url (application_controller.rb of the main app)

    before_filter { |c| Authorization.current_user = c.current_user }
      def permission_denied
        flash[:error] = "Sorry, you are not allowed to access that page.";
        redirect_to main_app.root_url
      end
    

    cheers