Pulling information from LDAP server using Ruby on Rails

Hello, I am currently working on a project using Ruby on Rails. So far,
I have a well-populated mySQL database the RoR refers to and the basic
scaffolding functions implemented. I also designed views/layouts that
furthers the basic scaffolding interface. No problem and no big
accomplishment by any means.

Now, I want to pull user information from an already existing LDAP
server and put that information on one of my pages, but I haven’t had
any success.

I searched for resources on the internet and have done many steps to get
to where I am now, but I hit a wall and currently do not know where to
go.

Let me tell you what I did:

I am developing on a windows machine and started off following Chris
Scarf’s instructions to copy the ruby-ldap (0.9.4) library to a ruby
subdirectory so ruby has access to it.

http://www.ruby-forum.com/topic/62584

However, I couldn’t find any reason to believe that this was all I
needed for RoR to pull LDAP information. I found examples of using the
ruby-ldap module in pure ruby code, but not within the rails framework.
This led me to Ruby/ActiveLDAP.

http://projects.dataspill.org/pages/projects/ruby-activeldap

It requires the previously mentioned ruby-ldap module which I have and
log4r as well. I installed the log4r gem. I then installed
Ruby/ActiveLDAP from source. It was at this time that I followed these
instructions:

http://aaronbedra.com/pages/ldap

Basically I add additional lines in environment.rb to connect to the
LDAP server, create directory and user model files, then create a
controller to access the information.

This is where I am at the moment. The previous link has a source with
views that I used to see if I could display a page with LDAP information
but I receive an uninitialized constant ActiveLDAP. When I type require
‘activeldap’ in irb, it returns true.

Any ideas? It is entirely possible that I’m going in the wrong direction
and maybe do not need activeldap or maybe I’m just missing something
important. If anyone can help me, I’d really appreciate it. I’m fairly
new at this, so please be specific if you have a solution. Thanks!

-Gilles

hey Gilles,

I too recently had to use ruby to query an LDAP server. It did not
involve
rails(except for ActiveRecord), but I think the things that I did still
stands.

I used ruby-ldap (http://ruby-ldap.sourceforge.net/) and looked up the
docs
at http://ruby-ldap.sourceforge.net/rdoc/ .

An excerpt from the code I wrote:

require ‘ldap’
require ‘ldap/control’
require ‘rubygems’
require ‘active_record’

ActiveRecord::Base.establish_connection(
:adapter => “mysql”,
:host => “localhost”,
:username => “root”,
:password => “”,
:database => “test”
)

class Customer < ActiveRecord::Base
set_table_name “customer”
end
ActiveRecord::Base.logger = Logger.new(STDERR)

dn = “uid=test,ou=University of California Irvine,o=University of
California, c=US”
bdn = “ou=University of California Irvine,o=University of California,
c=US”

c = LDAP::Conn.new(“ldap.server.domain.com”)
c.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )
c.start_tls
c.bind(dn, “secret”)

total = Customer.count
step = 500
num_chunks = total / step

p “Updating #{total} records in chunks of #{step}”

0.upto(num_chunks) do |offset|
Customer.find(:all, :limit => step, :offset => (offset*step)).each do
|record|
next if record.UCINETID.blank?

begin
  c.search(bdn, LDAP::LDAP_SCOPE_SUBTREE, "uid=#{record.UCINETID}",

“department”) do |entry|
#do stuff
print “.”
end
end
rescue
#Skip to the next one
# p e.inspect
end
end

p “(#{offset})”
end

c.unbind

HTH.

~Rohith

Gilles wrote:

Hello, I am currently working on a project using Ruby on Rails. So far,
I have a well-populated mySQL database the RoR refers to and the basic
scaffolding functions implemented. I also designed views/layouts that
furthers the basic scaffolding interface. No problem and no big
accomplishment by any means.

Now, I want to pull user information from an already existing LDAP
server and put that information on one of my pages, but I haven’t had
any success.

You may want to look at Net::LDAP (rubyforge)- it’s in pure Ruby and the
API is quite simple so it may be easier for you integrate.

Thanks for replying Rohith and Francis. I can finally pull information
from an LDAP server. Apparently I didn’t need ActiveLDAP for anything. I
was looking at some of your code Rohith and was able to figure out what
was needed. I actually tried to print out information on the command
line with a simple ruby program. After successfully printing all kinds
of information, I implemented that code within the rails framework with
some trial and error. Since I was making so much progress, I did not get
a chance to try Net::LDAP Francis, but I will keep it in mind in the
future.

Now I have to find a method of printing specific LDAP data based on who
the user is that is viewing the page. At the moment I’m hardcoding a
filter. I need to find a way to manipulate the filter. I will continue
on soon.

-Gilles

On Friday 21 July 2006 16:11, Gilles wrote:

This is where I am at the moment. The previous link has a source with
views that I used to see if I could display a page with LDAP
information but I receive an uninitialized constant ActiveLDAP. When I
type require ‘activeldap’ in irb, it returns true.

I’m using activeldap here (together with activerecord), and it runs just
fine (and integrates really nicely). So it can be made to work :slight_smile:

Christian R. wrote:

I’m using activeldap here (together with activerecord), and it runs just
fine (and integrates really nicely). So it can be made to work :slight_smile:

Oh? Is it similar to the link I provided on my original post?

http://aaronbedra.com/pages/ldap

I wasn’t able to get it working, but I assume I was missing something
important and wasn’t able to figure it out. May I ask how you got it
working? I am already able to pull the LDAP information without
ActiveLDAP, but I’m just curious and was wondering what I was doing
wrong. Thanks!

-Gilles

On Monday 24 July 2006 15:08, Gilles wrote:

Christian R. wrote:

I’m using activeldap here (together with activerecord), and it runs
just fine (and integrates really nicely). So it can be made to work
:slight_smile:

Oh? Is it similar to the link I provided on my original post?

http://aaronbedra.com/pages/ldap

Yes. I actually got my info from IIRC both that page and
http://wiki.rubyonrails.org/rails/pages/ActiveLDAP

Basically I have activeldap in my vendor/ dir (just didn’t want to
require
people to install it, since it’s still kind of uncommon), and the
following code in environment.rb:


require ‘activeldap’
ldapconfig = File.open("#{RAILS_ROOT}/config/ldap.yml") { |f|
YAML::load(f) }
cfg = Hash.new
ldapconfig[RAILS_ENV].each { |key, val| cfg[key.to_sym] = val }
cfg[:return_objects] = true
ActiveLDAP::Base.connect(cfg)

And the ldap.yaml looks like this:


test:
host: localhost
port: 389
base: ‘ou=test,dc=dotsrc,dc=org’
bind_format: ‘uid=%s,ou=people,ou=test,dc=dotsrc,dc=org’

logger:

user: wonttellyou
password: secret
allow_anonymous: false
try_sasl: false

It was actually simpler to set up than I expected :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs