Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make active admin use the users table already used by devise and cancan?

I have been using devise for authentication and cancan for authorization in my application. The application worked fine with them. But now i wanted to use active admin to manage all the users that are already in my application that are being used by devise and cancan. Active admin creates its own admin_users table for the users. how can i make active_admin use the users and roles table that was previously in use? thanx for your help.

like image 389
billy Avatar asked Mar 29 '12 08:03

billy


1 Answers

If you already have the users table created, so you should skip the creation of this table.

Running:

rails generate active_admin:install --skip-users

And don't forget to run:

bundle exec rake db:migrate

Be attempt to change the authentication method on the config/initializers/active_admin.rb file. Also make sure you have the current_admin_user method created, if you don't, you can just modify it to the devise defaults (current_user method).

You will have to modify the http method used in the logout link to :delete.

config.logout_link_method = :delete

And the route path to the logout action.

config.logout_link_path = :destroy_user_session_path

For better understand the authenticate method, I'm pasting my app/controllers/application_controller.rb relevant code:

class ApplicationController < ActionController::Base
  protect_from_forgery

  #
  # redirect registered users to a profile page
  # of to the admin dashboard if the user is an administrator
  #
  def after_sign_in_path_for(resource)
    resource.role == 'admin' ? admin_dashboard_path : user_path(resource)
  end

  def authenticate_admin_user!
    raise SecurityError unless current_user.try(:role) == 'admin'
  end

  rescue_from SecurityError do |exception|
    redirect_to root_path
  end
end

Hope it helps you and maybe someone else.

like image 70
Kleber S. Avatar answered Sep 20 '22 21:09

Kleber S.