Skip to content

Commit

Permalink
Introduce setup hook for authorization/auditing adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Aug 20, 2016
1 parent 53eef4f commit ba2088c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
4 changes: 0 additions & 4 deletions app/controllers/rails_admin/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ def _audit!
instance_eval(&RailsAdmin::Config.audit_with)
end

def user_for_paper_trail
_current_user.try(:id) || _current_user
end

rescue_from RailsAdmin::ObjectNotFound do
flash[:error] = I18n.t('admin.flash.object_not_found', model: @model_name, id: params[:id])
params[:action] = 'index'
Expand Down
8 changes: 6 additions & 2 deletions lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ def authenticate_with(&blk)
def audit_with(*args, &block)
extension = args.shift
if extension
klass = RailsAdmin::AUDITING_ADAPTERS[extension]
klass.setup if klass.respond_to? :setup
@audit = proc do
@auditing_adapter = RailsAdmin::AUDITING_ADAPTERS[extension].new(*([self] + args).compact)
@auditing_adapter = klass.new(*([self] + args).compact)
end
elsif block
@audit = block
Expand Down Expand Up @@ -145,8 +147,10 @@ def audit_with(*args, &block)
def authorize_with(*args, &block)
extension = args.shift
if extension
klass = RailsAdmin::AUTHORIZATION_ADAPTERS[extension]
klass.setup if klass.respond_to? :setup
@authorize = proc do
@authorization_adapter = RailsAdmin::AUTHORIZATION_ADAPTERS[extension].new(*([self] + args).compact)
@authorization_adapter = klass.new(*([self] + args).compact)
end
elsif block
@authorize = block
Expand Down
10 changes: 9 additions & 1 deletion lib/rails_admin/extensions/paper_trail/auditing_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ class AuditingAdapter
message: :event,
}.freeze

def self.setup
raise('PaperTrail not found') unless defined?(::PaperTrail)
RailsAdmin::ApplicationController.class_eval do
def user_for_paper_trail
_current_user.try(:id) || _current_user
end
end
end

def initialize(controller, user_class = 'User', version_class = '::Version')
raise('PaperTrail not found') unless defined?(PaperTrail)
@controller = controller
@controller.send(:set_paper_trail_whodunnit) if @controller
begin
Expand Down
7 changes: 7 additions & 0 deletions lib/rails_admin/extensions/pundit/authorization_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ module Pundit
# You can create another adapter for different authorization behavior, just be certain it
# responds to each of the public methods here.
class AuthorizationAdapter
# This method is called first time only and used for setup
def self.setup
RailsAdmin::ApplicationController.class_eval do
include ::Pundit
end unless RailsAdmin::ApplicationController.ancestors.include? 'Pundit'
end

# See the +authorize_with+ config method for where the initialization happens.
def initialize(controller)
@controller = controller
Expand Down
7 changes: 3 additions & 4 deletions spec/integration/authorization/pundit_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require 'spec_helper'

describe 'RailsAdmin Pundit Authorization', type: :request do
before(:all) do
ApplicationController.send :include, ::Pundit
end

subject { page }

before do
Expand Down Expand Up @@ -104,10 +100,12 @@
context 'when ApplicationController already has pundit_user' do
let(:admin) { FactoryGirl.create :user, roles: [:admin] }
before do
RailsAdmin.config.parent_controller = 'ApplicationController'
allow_any_instance_of(ApplicationController).to receive(:pundit_user).and_return(admin)
end

it 'uses original pundit_user' do
pending 'no way to dynamically change superclass'
expect { visit dashboard_path }.not_to raise_error
end
end
Expand Down Expand Up @@ -137,6 +135,7 @@
dashboard do
authorization_key :dashboard?
end
index
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/integration/history/rails_admin_paper_trail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
end
end

after(:each) do
# if #user_for_paper_trail is left unused, PaperTrail complains about it
RailsAdmin::ApplicationController.class_eval do
undef user_for_paper_trail
end
end

describe 'on object creation', type: :request do
subject { page }
before do
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_admin/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class RecursivelyEmbedsMany

describe '.parent_controller' do
it 'uses default class' do
expect(RailsAdmin.config.parent_controller).to eq '::ApplicationController'
expect(RailsAdmin.config.parent_controller).to eq '::ActionController::Base'
end

it 'uses other class' do
Expand Down

0 comments on commit ba2088c

Please sign in to comment.