Skip to content

Instantly share code, notes, and snippets.

@Arcath
Created April 19, 2011 11:57
Show Gist options
  • Save Arcath/927204 to your computer and use it in GitHub Desktop.
Save Arcath/927204 to your computer and use it in GitHub Desktop.
Code for AD Auth post
SERVER = '10.0.0.1' # Active Directory server name or IP
PORT = 389 # Active Directory server port (default 389)
BASE = 'DC=arcath,DC=local' # Base to search from
DOMAIN = 'arcath.local' # For simplified user@domain format login
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
<%= form_tag :action => "create" do %>
<p><%= label :session, :username %><br />
<%= text_field :session, :username %></p>
<p><%= label :session, :password %><br />
<%= password_field :session, :password %></p>
<p><%= submit_tag "Login!" %></p>
<% end %>
match "/signout" => "sessions#destroy", :as => :signout
match "/signin" => "sessions#new", :as => :signin
def create
if aduser = ActiveDirectoryUser.authenticate(params[:session][:username], params[:session][:password]) then
user = User.create_and_update_from_ldap(aduser)
session[:user_id] = user.id
redirect_to root_path
else
flash[:error] = "invalid login"
redirect_to new_session_path
end
end
def destroy
session[:user_id] = nil
redirect_to root_path
end
def self.create_and_update_from_ldap(aduser)
user = find_or_create_by_username(aduser.login)
user.name = aduser.name
user.groups = aduser.groups.join(",")
user.save
return user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment