Rails admin with authlogic

If you have used the rails_admin gem, you would have noticed that its tightly coupled with Devise. What happens if devise is not the pick and the application is already integrated with authlogic. Lets see,

After installing the rails_admin gem, create a ‘rails_admin.rb‘ file in config/initializers. Paste the following code,

  RailsAdmin.config do |config|
    config.authorize_with do
      redirect_to root_path, :alert => "You are not authorized!" unless current_user.admin?
    end
  end

  RailsAdmin.config do |config|
    config.authenticate_with do
      unless current_user
        redirect_to login_url
      end
    end
  end

The authentication block checks if the user is currently logged in and the authorization block checks if the logged in user is an admin or not.

Also add the rails admin route in the config/routes.rb file.

  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

This should enable the admin to run at /admin, now authenticated and authorized using authlogic.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s