Integrating ActiveAdmin and adauth

active-directory, activeadmin, ruby-on-rails-3

Solution

I finally was able to manage to integrate AD in ActiveAdmin.

Here's what I did, in case someone is interested:

- Include gem 'adauth' in your gems

- Execute bundle install

Execute rails g adauth:config

Configure the config/initializers/adauth.rb for your AD connection. For example, if your domain is example.com, you must include:

c.domain = "example.com" 
c.server = "IP address of your domain controller"
c.base = "dc=example, dc=com"

Execute rails g adauth:sessions

Modify your application_controller.rb. Mine was:

class ApplicationController< ActionController::Base
    protect_from_forgery
    helper_method :current_user
    def current_user
        @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end
    def authenticate_user!
       if current_user.nil?
           redirect_to '/sessions/new', :error => "Invalid Login" 
       end
    end
end

Execute rails g adauth:user_model user install_adauth. This creates the migration install_adauth, but for some reason it was empty. I had to fill it myself with:

class InstallAdauth < ActiveRecord::Migration
    def up
        create_table :users do |u|
            u.string 'login'
            u.text 'group_strings'
            u.string 'name'
            u.string 'ou_strings'
        end
    end
    def down
        drop_table :users
    end
end

Execute rake db:migrate

Modify your sessions_controller.rb. Mine was:

class SessionsController < ApplicationController
    def new
        redirect_to '/admin' if current_user 
    end
    def create
        ldap_user = Adauth.authenticate(params[:username], params[:password])
        if ldap_user
            user = User.return_and_create_with_adauth(ldap_user)
            session[:user_id] = user.id
            redirect_to '/admin' 
        else
            redirect_to '/sessions/new', :error => "Invalid Login" 
        end
    end

    def destroy
        session[:user_id] = nil
        redirect_to '/sessions/new' 
    end
end

So far the validation through ActiveAdmin still works. To switch to ActiveDirectory we must change the file initializers/active_admin.rb

# config.authentication_method = :authenticate_admin_user!
config.authentication_method = :authenticate_user!

#config.current_user_method = :current_admin_user
config.current_user_method = :current_user  

In my case, I needed to restart Apache too.

If anytime we want to switch back to ActiveAdmin, we just need to undo the last change

Problem

I have a running Rails application, using ActiveAdmin and its models to autenticate users. Now I'm interested in moving to an ActiveDirectory authentication, so my users can validate wiht the domain's users. I've been trying adauth and it looks like a great gem, but I'm a little bit lost when trying to "mix" this gem with my ActiveAdmin authentication. I'm pretty sure I'm not the first one in doing it, so any help would be appreciated. Thanks!

Original source