Skip to content

Instantly share code, notes, and snippets.

@grosser
Forked from huned/rails_admin_without_devise.md
Last active July 27, 2016 06:25
Show Gist options
  • Save grosser/1278355 to your computer and use it in GitHub Desktop.
Save grosser/1278355 to your computer and use it in GitHub Desktop.
Using RailsAdmin without devise
# add RailsAdmin to the Gemfile
# do NOT add devise
gem "rails_admin", :git => "git://github.com/sferik/rails_admin.git"
# run Bundler
bundle
# run the generator for RailsAdmin
# looks like it's broken, but it just does not install devise
# you may also remove the app/config/locales/devise.en.yml
rails generate rails_admin:install_admin
# create an initializer for using CanCan with RailsAdmin
# found here: http://everydayrails.com/2010/12/17/rails-admin-panel.html
# config/initializers/rails_admin.rb
require "rails_admin/application_controller"
module RailsAdmin
class ApplicationController < ::ApplicationController
before_filter :can_admin?
private
def can_admin?
raise CanCan::AccessDenied if current_user.nil? || !current_user.admin?
end
end
end
# RailsAdmin needs two additional things to get it working with my OmniAuth setup
# 1. an email attribute on the User class, which i simply redirected to the nickname
# app/models/user.rb
def email
nickname
end
# 2. a logout route that matches Devises name schema
# config/routes.rb
match '/auth/destroy_user_session', :to => 'sessions#destroy_user_session', :as => :destroy_user_session
# this simply redirects to the original logout
# app/controllers/sessions_controller.rb
def destroy_user_session
redirect_to destroy_session_path
end
@mgartner
Copy link

mgartner commented Apr 8, 2012

This doesn't seem to work. When the HTTP Basic authentication box appears, pressing cancel without providing any credentials grants access to rails_admin. I fixed it by doing the following:

In config/initializers/rails_admin.rb:

RailsAdmin.config do |config|
  config.authorize_with do
    authenticate_or_request_with_http_basic('Site Message') do |username, password|
      username == 'foo' && password == 'bar'
    end
  end

  # rest of configuration

end

@grosser
Copy link
Author

grosser commented Apr 1, 2013

Thanks for the tip, updated it :)

@LaurMo
Copy link

LaurMo commented Oct 29, 2014

This did not work for me. Here is my code:

RailsAdmin.config do |config|
  config.authorize_with do
    authenticate_or_request_with_http_basic('Site Message') do |username, password|
      username == ENV["ADMIN_KEY"] && password == ENV["ADMIN_PASSWORD"]
    end
  end  
  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app
  end
  config.main_app_name { ['grade-system', 'Admin'] }
end

@vorant94
Copy link

but how to logout with this authentication?

@SpencerCloud
Copy link

SpencerCloud commented May 24, 2016

This works well locally but doesn't seem to work with Heroku.
It doesn't break, but the /admin page bypasses the authorization window completely when accessed after being deployed to Heroku.
I may be deploying wrong.. Any tips?

@inv-akhils
Copy link

How to add a normal authentication (not basic authentication) for rails admin. I have created a dedicated model Admin, apart from user table. Any tips?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment