Wednesday, April 23, 2008

ActiveLDAP

ActiveLDAP has some annoying install warts, but it's a pretty cool library.

Installing

First download and build RubyLDAP. Annoyingly this is NOT a gem, so you can't gem install it.

Next, build it ...
ruby extconf.rb
make
sudo make install

Now install ActiveLDAP ...

sudo gem install ruby-activeldap
Quick Start

Many of the examples are talking to a default openldap install. If you want to talk to a Mac OS X Open Dircetory server, you will need to change the prefix for users and groups, shown below in a quick sample app:

#!/usr/bin/env ruby

require 'rubygems'
require 'active_ldap'

class Group < ActiveLdap::Base
  ldap_mapping :dn_attribute => 'cn',
               :prefix => 'cn=groups',
               :classes => ['top', 'posixGroup'],
               :scope => :one
  has_many :members,
           :class => "User",
           :wrap => "memberUid",
           :primary_key => 'uid'
end

class User < ActiveLdap::Base
  ldap_mapping :dn_attribute => 'uid',
               :prefix => 'cn=users',
               :classes => ['top','inetOrgPerson']
  belongs_to :groups,
             :class => 'Group',
             :many => 'memberUid'
end

ActiveLdap::Base.establish_connection(
  :host => 'someldapserver.mdimension.com',
  :port => 389,
  :base => 'dc=mdimension,dc=com',
  :bind_dn => "uid=mschrag,cn=mdimension,dc=com",
  :password => 'mschragpw',
  :allow_anonymous => false,
  :try_sasl => false
)

#Group.find(:all, '*').each do |group|
  #puts "#{group.cn}"
  #group.members.each do |member|
    #puts "  #{member.cn}"
  #end
#end

user = User.find('mschrag');
user.givenName = 'NewFirstName'
user.save
And that's the quick start. It should give you enough to experiment with.