billy
billy

Reputation: 83

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.

Upvotes: 5

Views: 4065

Answers (3)

Kapil Dewade
Kapil Dewade

Reputation: 1

if you already ran the activeadmin commands.... you can use existing users table by changing admin_user to user in active_admin.rb in initializer and define an ability for admin in cancan ability model.Also you have to do something like authorize admin to authorize DSL

Upvotes: 0

Kleber S.
Kleber S.

Reputation: 8240

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.

Upvotes: 9

Scott
Scott

Reputation: 2258

By default it will create a new Devise user / model called AdminUser. To change the name of the user class, simply pass the class as the last argument:

rails generate active_admin:install User

http://activeadmin.info/docs/0-installation.html

http://activeadmin.info/docs/1-general-configuration.html#authentication

Upvotes: 5

Related Questions