Last active
December 18, 2015 09:19
-
-
Save lukebaker/5760651 to your computer and use it in GitHub Desktop.
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
# 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