Skip to content

Commit

Permalink
Respect #pundit_user. Closes railsadminteam#2467
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya authored and jibidus committed Nov 26, 2015
1 parent bfa85ef commit 36accee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/rails_admin/extensions/pundit/authorization_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class AuthorizationAdapter
# See the +authorize_with+ config method for where the initialization happens.
def initialize(controller)
@controller = controller
@controller.class.send(:alias_method, :pundit_user, :_current_user)
end

# This method is called in every controller action and should raise an exception
Expand Down Expand Up @@ -52,7 +51,7 @@ def attributes_for(action, abstract_model)
def policy(record)
@controller.policy(record)
rescue ::Pundit::NotDefinedError
::ApplicationPolicy.new(@controller.send(:_current_user), record)
::ApplicationPolicy.new(@controller.send(:pundit_user), record)
end
end
end
Expand Down
16 changes: 15 additions & 1 deletion spec/integration/authorization/pundit_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
include Pundit

class ApplicationPolicy
attr_reader :user, :record
Expand Down Expand Up @@ -65,6 +64,10 @@ def index
end

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

subject { page }

before do
Expand Down Expand Up @@ -164,4 +167,15 @@ def index
is_expected.to have_content('Show in app')
end
end

context 'when ApplicationController already has pundit_user' do
let(:admin) { FactoryGirl.create :user, roles: [:admin] }
before do
allow_any_instance_of(ApplicationController).to receive(:pundit_user).and_return(admin)
end

it 'uses original pundit_user' do
expect { visit dashboard_path }.not_to raise_error
end
end
end

0 comments on commit 36accee

Please sign in to comment.