I currently have Devise and Pundit working great in a closed system that requires a user be logged in to see anything.
The new goal: How can I use Pundit to require an AdminUser be logged in to access the Admin namespace?
I have a separate AdminUser model, separate Admin controllers, Policies and Namespace:
routes.rb
namespace :admin do
root to: 'home#index'
resources :users
end
devise_for :admin_users, skip: [:sessions]
as :admin_user do
get 'admin/signin', to: 'admin/devise/sessions#new', as: :new_admin_user_session
post 'admin/signin', to: 'admin/devise/sessions#create', as: :admin_user_session
delete 'admin/signout', to: 'admin/devise/sessions#destroy', as: :destroy_admin_user_session
end
controllers/admin/admin_controller.rb
class Admin::AdminController < ApplicationController
end
controllers/admin/home_controller.rb
class Admin::HomeController < Admin::AdminController
def index
authorize [:admin, :home]
end
end
policies/admin/admin_policy.rb (Closed system, currently looks for User instead of AdminUser)
class Admin::AdminPolicy
attr_reader :user, :record
def initialize(user, record)
# Must be logged in
raise Pundit::NotAuthorizedError, "You must be logged in to perform this action" unless user
@user = user
@record = record
end
def index?
false
end
def show?
false
end
def create?
false
end
def new?
create?
end
def update?
false
end
def edit?
update?
end
def destroy?
false
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
raise Pundit::NotAuthorizedError, "You must be logged in to perform this action" unless user
@user = user
@scope = scope
end
def resolve
scope.all
end
end
end
policies/admin/home_policy.rb (Example sub-policy of the Admin namespace)
class Admin::HomePolicy < Admin::AdminPolicy
def index?
user.present?
end
end
Both of these policies are setup in the same way as my User policies, and therefore are not looking for an AdminUser. How can I make these work for my AdminUser model?