Just wrap the following into your before filter or user class:
class User < ActiveRecord::Base def self.authenticate(username, password) userid = username if (password.empty?) then password = 'empty' end ldap = Net::LDAP.new(:host => LDAPSERVER, :port => 636, :base => LDAPBASE, :encryption => :simple_tls) #or without encryption #ldap = Net::LDAP.new(:host => LDAPSERVER, :base => 'ou=users,o=rackspace') filter = Net::LDAP::Filter.eq('uid', username) ldap.search(:filter => filter) {|entry| username = entry.dn} ldap.auth(username, password) if ldap.bind # authentication succeeded # this will look for the user in the local database also #after successfully authenticating user = find( :first, :conditions=> { :name => userid }) if (user) return user else #uncomment this to create the user on successful bind #user = User.create ( # :active => true, # :is_admin => false, # :name => userid #) return false end else # authentication failed return false end end # protected end
Discussion