module ActiveLdap::Populate

def ensure_base(base_class=nil, options={})

def ensure_base(base_class=nil, options={})
  base_class ||= Base
  return unless base_class.search(:scope => :base).empty?
  dc_base_class = options[:dc_base_class] || base_class
  ou_base_class = options[:ou_base_class] || base_class
  base_dn = DN.parse(base_class.base)
  suffixes = []
  base_dn.rdns.reverse_each do |rdn|
    name, value = rdn.to_a[0]
    prefix = suffixes.join(",")
    suffixes.unshift("#{name}=#{value}")
    begin
      case name.downcase
      when "dc"
        ensure_dc(value, prefix, dc_base_class)
      when "ou"
        ensure_ou(value,
                  :base => prefix,
                  :base_class => ou_base_class)
      end
    rescue ActiveLdap::OperationNotPermitted
    end
  end
end