Module: ActiveLdap::Populate

Defined in:
lib/active_ldap/populate.rb

Class Method Summary collapse

Class Method Details

.ensure_base(base_class = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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}")
    next unless name == "dc"
    dc_class = Class.new(base_class)
    dc_class.ldap_mapping :dn_attribute => "dc",
                          :prefix => "",
                          :scope => :base,
                          :classes => ["top", "dcObject", "organization"]
    dc_class.base = prefix
    next if dc_class.exist?(value)
    dc = dc_class.new(value)
    dc.o = dc.dc
    begin
      dc.save
    rescue ActiveLdap::OperationNotPermitted
    end
  end
end

.ensure_ou(name, base_class = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_ldap/populate.rb', line 32

def ensure_ou(name, base_class=nil)
  base_class ||= Base
  name = name.gsub(/\Aou\s*=\s*/, '')

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