Net::LDAP

Im having difficulties setting up my controller to connect to our ldap
server. I’ve searched around in google and found wikis of snippets of
some code to set ruby ldap. Below is what I currently have but Im
getting “uninitialized constant LDAP” when I try to access the login.
Im not what what Im missing or how to debug this. Any suggestions or
tips is appreciated.

class LoginController < ApplicationController

#Bind with the main credential and query the full DN of the email
address
#given to us as a parameter, then unbind and rebind as the user.
def self.authenticate(username,password)
logger.debug("username = " + username)

ldap_con = initialize_ldap_con(username,password)
treebase = "DC=domain,DC=domainext"
mail_filter = Net::LDAP::Filter.eq( "mail", '[email protected]' )
op_filter = Net::LDAP::Filter.eq( "objectClass",

“organizationalPerson” )
dn = String.new
ldap_con.search( :base => treebase, :filter => op_filter &
mail_filter, :attributes=> ‘dn’) do |entry|
dn = entry.dn
end
login_succeeded = false
unless dn.empty?
ldap_con = initialize_ldap_con(dn,password)
login_succeeded = true if ldap_con.bind
end
logger.debug("login_succeeded = " + login_succeeded)
login_succeeded
end

private
def self.initialize_ldap_con(user_name, password)
Net::LDAP.new( {:host => ‘hostnamehere’, :port => 389, :auth => {
:method => :simple, :username => user_name, :password => password }} )
end

def index
case @request.method
when :post
if @session[‘user’] =
LoginController.authenticate(@params[‘user_login’],
@params[‘user_password’])

      flash['notice']  = "Login successful"
      redirect_back_or_default :action => "overview"
    else
      @login    = @params['user_login']
      @message  = "Login unsuccessful"
  end
end

end
end

Here is my embarrasingly simplest-thing-that-could-work:

class LoginTestController < ApplicationController
def index
ldap = Net::LDAP.new
ldap.host = ‘COSEAD.cose.win2k’
ldap.port = 389
ldap.auth ‘[email protected]’, ‘some_test_password’
if ldap.bind
# authentication succeeded
render :text=>‘yay’
else
# authentication failed
render :text=>‘boo’
end
end
end

That at least makes sure the LDAP part works, then it’s simple to build
out the Rails part.

Ron