Created
April 19, 2011 11:57
-
-
Save Arcath/927204 to your computer and use it in GitHub Desktop.
Code for AD Auth post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
helper_method :current_user | |
private | |
def current_user | |
@current_user ||= User.find(session[:user_id]) if session[:user_id] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match "/signout" => "sessions#destroy", :as => :signout | |
match "/signin" => "sessions#new", :as => :signin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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