Class: Ldap::LdapConnection

Inherits:
Object
  • Object
show all
Includes:
Slf4r::Logger
Defined in:
lib/adapters/ldap_adapter.rb

Overview

the class provides two ways of getting a LdapFacade. either one which is put on the current Thread or a new one

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ LdapConnection

Returns a new instance of LdapConnection.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/adapters/ldap_adapter.rb', line 13

def initialize(options)
  if options[:facade].nil?
    require 'ldap/net_ldap_facade'
    @facade = ::Ldap::NetLdapFacade
  else
    case options[:facade].to_sym
    when :ruby_ldap
      require 'ldap/ruby_ldap_facade'
      @facade = ::Ldap::RubyLdapFacade
    when :net_ldap
      require 'ldap/net_ldap_facade'
      @facade = ::Ldap::NetLdapFacade
    else
      "please add a :facade parameter to the adapter setup. possible values are :ruby_ldap or net_ldap"
    end
  end
  logger.info("using #{@facade}")
  @ldaps = { }
  @config = {
    :host => options[:host],
    :port => options[:port].to_i,
    :base => options[:base]
  }

  if not options[:bind_name].nil? then
    @config.update({
      :auth => {
        :method => :simple,
        :username => options[:bind_name],
        :password => options[:password]
      }
    })
  end

  if not options[:adapter_options].nil? then
    options[:adapter_options].each do |k,v|
      @config[k.to_sym] = v
    end
  end
end

Instance Method Details

#currentLdap::LdapFacade

Returns either the one from the current Thread or a new one.

Returns:

  • (Ldap::LdapFacade)

    either the one from the current Thread or a new one



69
70
71
72
73
74
75
76
# File 'lib/adapters/ldap_adapter.rb', line 69

def current
  ldap = @ldaps[Thread.current]
  if ldap
    ldap
  else
    @facade.new(@config)
  end
end

#openObject

puts a LdapFacade into the current thread and executes the given block.



56
57
58
59
60
61
62
63
64
65
# File 'lib/adapters/ldap_adapter.rb', line 56

def open
  begin
    @facade.open(@config) do |ldap|
      @ldaps[Thread.current] = @facade.new(ldap)
      yield
    end
  ensure
    @ldaps[Thread.current] = nil
  end
end