ldap.rb 977 Bytes
class SdAdmin::Database::Ldap
  attr_writer :base_dn, :mapper

  def initialize(args = {})
    @con = {:host => 'host.one.virtual', :port => 389}

    @con[:host] = args[:host] if args[:host]
    @con[:port] = args[:port] if args[:port]

    @base_dn = args[:base_dn] if args[:base_dn]

    @ldap  = Net::LDAP.new(@con)
    filter = args[:filter] if args[:filter]
  end

  def filter=(filter)
    @filter = Net::LDAP::Filter::construct(filter)
  end

  def each
    _load.each{|key,entry| puts "DEBUG: #{key}"; yield key,entry}
  end

  def [](id)
    _load[id]
  end

  def []=(id, data)
  end

  def insert(data)
  end

  def delete(id)
  end

  private

  def _load!
    @data = Hash.new
    @ldap.search(:base => @base_dn, :filter => @filter) do |entry|
      attributes = Hash.new
      entry.each{|attr,value| attributes.merge!({attr => value})}
      @data.merge!({attributes[:dn][0] => attributes})
    end
  end

  def _load
    _load! if ! @data
    @data
  end

end