I have an existing Rails app with Devise authenticating the User model and Pundit authenticating against an Enrollment model which links User to my Company model. Both User and Company are in the public schema of the apartment gem. I don't suspect apartment is part of the issue but I figured I would mention it.
I added Active Admin with the AdminUser class - I want to keep my admin user separate from the app users.
If I try to access /admin or /admin/dashboard I get:
Pundit::PolicyScopingNotPerformedError at /admin/users
Pundit::PolicyScopingNotPerformedError
If I try my models like /admin/users Pundit seems to ignore the active_admin policies and goes to the main app policies. In my case the app throws an exception because it's expecting an Enrollment vs the AdminUser.
If I disable:
##/config/initializers/active_admin.rb
config.authorization_adapter = ActiveAdmin::PunditAdapter
##/controllers/application_controller
after_action :verify_authorized, except: [:landing, :dashboard], unless: :devise_controller?
after_action :verify_policy_scoped, only: [:index]
It all works but then I lose Pundit etc in my main app.
Here is a gist of my code:
https://gist.github.com/jasper502/4b2f1b8b6f21a26c64a5
Here are the related posts that could find on this issue:
https://gorails.com/forum/using-pundit-with-activeadmin
How to get Active Admin to work with Pundit after login
I was looking to disable Pundit all together over in this post (Can you disable Pundit with Devise and Active Admin?) but it would be nice to just make this work.
UPDATE
I have work around but I still don't know if this should work out of the box and I have some weird issue causing all of this. Gist updated.
I ended up using:
https://viget.com/extend/8-insanely-useful-activeadmin-customizations
and a bit of:
Documentation for conditional before_action/before_filter
and a bit of the answer below. I shoehorned in a filter to force AA to call authorize on the resources and collections inside AA. Next would be to add the policy scopes but my brain hurts too much now.
I also had to add another filter to bypass authentication on the Dashboard as it's headless. Seems to work so far.
UPDATE 2
Hmmm... I think I spoke too soon. This all works only if I am logged in as a regular User - I if I log out it all falls apart again.