Created
December 27, 2011 03:56
-
-
Save shelling/1522659 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class LDAPAdapter | |
| def initialize(options={}) | |
| @config = options.select do |key, value| | |
| [:uid].include? key | |
| end | |
| @server = options.select do |key, value| | |
| [:host, :port, :base].include? key | |
| end | |
| @conn = ldap(options) | |
| end | |
| def search(user) | |
| if @conn.bind | |
| filter = Net::LDAP::Filter.eq(@config[:uid], user) | |
| @conn.search(:base => @server[:base], :filter => filter).first | |
| else | |
| return nil | |
| end | |
| rescue Net::LDAP::LdapError => e | |
| return false | |
| end | |
| def bindable?(user, pass) | |
| return false if user.empty? or pass.empty? | |
| user = search(user) | |
| ldap(username: user.dn, password: pass).bind ? user : false | |
| rescue | |
| false | |
| end | |
| private | |
| def ldap(options={}) | |
| Net::LDAP.new auth(options).merge(@server) | |
| end | |
| def auth(options={}) | |
| { auth: | |
| { method: :simple }.merge(options.select { |key, value| | |
| [:username, :password, :method].include? key | |
| }) | |
| } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment