Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging ActiveAdmin users with existing user model

I've set up ActiveAdmin early in my project and used the default admin_users model for authentication. I've since used Devise to set up a separate User model and have realized it would probably be much smarter to merge the two tables, such that an Administrator can have administrative actions both in Activeadmin and in the front end of the site. How can I configure ActiveAdmin to use the Users model with maybe a column to flag an administrator (eg. is_admin or event a permissions level to make Administrators and Moderators)?

Rails 3.1
ActiveAdmin 0.3.3
Devise 1.4.9
like image 488
Kyle Macey Avatar asked Feb 12 '12 23:02

Kyle Macey


1 Answers

For a quick code block of how to do this using an existing "User" model with activeadmin, the answer is actually really easy. In the ApplicationController:

class ApplicationController < ActionController::Base
    def authenticate_admin_user! #use predefined method name
      redirect_to '/' and return if user_signed_in? && !current_user.is_admin? 
      authenticate_user! 
    end 
    def current_admin_user #use predefined method name
      return nil if user_signed_in? && !current_user.is_admin? 
      current_user 
    end 
end

And just use what Devise already has set up for authentication. The redirect_to is where you want to send users who ARE signed in and DO NOT have administrative privileges.

like image 197
Kyle Macey Avatar answered Oct 21 '22 03:10

Kyle Macey