Module: ActiveLdap::Populate — activeldap - ActiveLdap

Module: ActiveLdap::Populate

Defined in:
lib/active_ldap/populate.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) ensure_base(base_class = nil)



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_ldap/populate.rb', line 4

def ensure_base(base_class=nil)
  base_class ||= Base
  return unless base_class.search(:scope => :base).empty?

  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, base_class)
      when "ou"
        ensure_ou(value,
                  :base => prefix,
                  :base_class => base_class)
      end
    rescue ActiveLdap::OperationNotPermitted
    end
  end
end

+ (Object) ensure_dc(name, prefix, base_class = nil)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_ldap/populate.rb', line 48

def ensure_dc(name, prefix, base_class=nil)
  base_class ||= Base
  name = name.to_s if name.is_a?(DN)
  name = name.gsub(/\Adc\s*=\s*/i, '')

  dc_class = Class.new(base_class)
  dc_class.ldap_mapping(:dn_attribute => "dc",
                        :prefix => "",
                        :scope => :base,
                        :classes => ["top", "dcObject", "organization"])
  dc_class.base = prefix
  return if dc_class.exist?(name)
  dc = dc_class.new(name)
  dc.o = dc.dc
  dc.save!
end

+ (Object) ensure_ou(name, options = {})



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_ldap/populate.rb', line 29

def ensure_ou(name, options={})
  if options.is_a?(Class)
    base_class = options
    options = {}
  else
    base_class = options[:base_class] || Base
  end
  name = name.to_s if name.is_a?(DN)
  name = name.gsub(/\Aou\s*=\s*/i, '')

  ou_class = Class.new(base_class)
  ou_class.ldap_mapping(:dn_attribute => "ou",
                        :prefix => "",
                        :classes => ["top", "organizationalUnit"])
  ou_class.base = options[:base]
  return if ou_class.exist?(name)
  ou_class.new(name).save!
end