# Pulling information from LDAP server using Ruby on Rails

**URL:** <https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316>\
**Category:** Rails\
**Created:** [July 21, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316 "2006-07-21T18:11:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [July 21, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316/1 "2006-07-21T18:11:09Z")

</div>

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](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](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](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

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [July 21, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316/2 "2006-07-21T20:39:03Z")

</div>

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/](http://ruby-ldap.sourceforge.net/)) and looked up the  
docs  
at [RDoc Documentation](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](http://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

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [July 21, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316/3 "2006-07-21T23:05:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [July 22, 2006, 1:49am UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316/4 "2006-07-22T01:49:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [July 24, 2006, 11:50am UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316/5 "2006-07-24T11:50:30Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [July 24, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316/6 "2006-07-24T17:08:16Z")

</div>

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 🙂

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

[http://aaronbedra.com/pages/ldap](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

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [July 25, 2006, 10:20am UTC](https://www.ruby-forum.com/t/pulling-information-from-ldap-server-using-ruby-on-rails/99316/7 "2006-07-25T10:20:12Z")

</div>

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  
> > 🙂
> 
> Oh? Is it similar to the link I provided on my original post?
> 
> [http://aaronbedra.com/pages/ldap](http://aaronbedra.com/pages/ldap)

Yes. I actually got my info from IIRC both that page and  
[http://wiki.rubyonrails.org/rails/pages/ActiveLDAP](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 🙂
