Skip to content

Instantly share code, notes, and snippets.

@lukebaker
Last active December 18, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukebaker/5760651 to your computer and use it in GitHub Desktop.
Save lukebaker/5760651 to your computer and use it in GitHub Desktop.
# For using LDAP authentication with All Our Ideas
# The strategy is to do the authenticate via LDAP, but still create a local user
# so that the rest of site still has a proper user object to use.
#
# You'll need to add net-ldap to the Gemfile and run `bundle install`
#
# Add this to app/models/user.rb
module Clearance::User::ClassMethods
def authenticate(email, password)
ldap = Net::LDAP.new(
:host => 'hostname.of.ldap.server',
:auth => { :method => :simple, :username => email, :password => password }
)
begin
return nil unless ldap.bind
rescue
return nil
end
user = find_by_email(email)
if !user
o = [('a'..'z'),('A'..'Z'), (0..9)].map{|i| i.to_a}.flatten
new_password = (0...50).map{ o[rand(o.length)] }.join
user = User.new
user.email = email
user.password = new_password
user.email_confirmed = true
user.save
end
return user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment