Skip to content

Instantly share code, notes, and snippets.

@johnrails
Created March 3, 2012 16:53
Show Gist options
  • Save johnrails/1966956 to your computer and use it in GitHub Desktop.
Save johnrails/1966956 to your computer and use it in GitHub Desktop.
custom devise session controller for active directory users
def self.authenticate_user(username,password)
ldap = Net::LDAP.new
ldap.host = ActiveDirectory.first.host
ldap.port = 389
ldap.auth username, password
if ldap.bind
{:results => "successful", :message => "user authenticated"}
else
{ :results => "error", :message => ldap.get_operation_result.message}
end
end
def create
user = User.find_by_username(params[:user][:username])
password = params[:user][:password]
if user && user.user_type == "active directory"
results = ActiveDirectory.authenticate_user(user.username,password)
if results.fetch(:results) == "successful"
begin
user.password = password
user.password_confirmation = password
user.save
rescue Exception => e
logger.debug "~~~~~~~~~~~~~~~~~~~~~~~~EXCEPTION::#{e}"
end
if user.save!
super
else
redirect_to root_path, :error => "There was a problem signing in."
end
else
redirect_to root_path
flash[:error] = "Invalid username or password"
end
else
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment