Class: Ldaptic::Adapters::LDAPConnAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/ldaptic/adapters/ldap_conn_adapter.rb

Direct Known Subclasses

ActiveDirectoryAdapter

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#attribute_type, #attribute_types, #dit_content_rules, #logger, #object_classes, register_as, #root_dse, #schema, #server_default_base_dn

Constructor Details

#initialize(options) ⇒ LDAPConnAdapter

Returns a new instance of LDAPConnAdapter.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 8

def initialize(options)
  require 'ldap'
  if defined?(::LDAP::Conn) && options.kind_of?(::LDAP::Conn)
    options = {:adapter => :ldap_conn, :connection => options}
  else
    options = options.dup
  end
  options[:version] ||= 3
  @options = options
  if @connection = @options.delete(:connection)
    begin
      host, port = @connection.get_option(::LDAP::LDAP_OPT_HOST_NAME).split(':')
      @options[:host] ||= host
      @options[:port] ||= port.to_i if port
    rescue
    end
  else
    if username = @options.delete(:username)
      @options[:username] = full_username(username)
    end
    if @options[:username]
      connection = new_connection
      bind_connection(connection, @options[:username], @options[:password])
      connection.unbind
    end
  end
  @logger = @options.delete(:logger)
  super(@options)
end

Instance Method Details

#add(dn, attributes) ⇒ Object



38
39
40
41
42
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 38

def add(dn, attributes)
  with_writer do |conn|
    conn.add(dn, attributes)
  end
end

#authenticate(dn, password) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 114

def authenticate(dn, password)
  conn = new_connection
  bind_connection(conn, full_username(dn) || "", password)
  true
rescue ::LDAP::ResultError => exception
  message = exception.message
  err = error_for_message(message)
  unless err == 49 # Invalid credentials
    Ldaptic::Errors.raise_unless_zero(err, message)
  end
  false
ensure
  conn.unbind rescue nil
end

#compare(dn, attr, value) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 76

def compare(dn, attr, value)
  with_reader do |conn|
    conn.compare(dn, attr, value)
  end
rescue Ldaptic::Errors::CompareFalse
  false
rescue Ldaptic::Errors::CompareTrue
  true
end

#default_base_dnObject



129
130
131
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 129

def default_base_dn
  @options[:base] || server_default_base_dn
end

#delete(dn) ⇒ Object



55
56
57
58
59
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 55

def delete(dn)
  with_writer do |conn|
    conn.delete(dn)
  end
end

#modify(dn, attributes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 44

def modify(dn, attributes)
  if attributes.kind_of?(Array)
    attributes = attributes.map do |(op, key, vals)|
      LDAP::Mod.new(mod(op) | LDAP::LDAP_MOD_BVALUES, key, vals)
    end
  end
  with_writer do |conn|
    conn.modify(dn, attributes)
  end
end

#rename(dn, new_rdn, delete_old, new_superior = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 61

def rename(dn, new_rdn, delete_old, new_superior = nil)
  with_writer do |conn|
    if new_superior
      # This is from a patch I hope to get accepted upstream.
      if conn.respond_to?(:rename)
        conn.rename(dn, new_rdn, new_superior, delete_old)
      else
        Ldaptic::Errors.raise(NotImplementedError.new("rename unsupported"))
      end
    else
      conn.modrdn(dn, new_rdn, delete_old)
    end
  end
end

#search(options = {}, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ldaptic/adapters/ldap_conn_adapter.rb', line 86

def search(options = {}, &block)
  parameters = search_parameters(options)
  with_reader do |conn|
    begin
      if options[:limit]
        # Some servers don't support this option.  If that happens, the
        # higher level interface will simulate it.
        conn.set_option(LDAP::LDAP_OPT_SIZELIMIT, options[:limit]) rescue nil
      end
      cookie = ""
      while cookie
        ctrl = paged_results_control(cookie)
        if !options[:disable_pagination] && paged_results?
          conn.set_option(LDAP::LDAP_OPT_SERVER_CONTROLS, [ctrl])
        end
        params = parameters
        result = conn.search2(*params, &block)
        ctrl   = conn.controls.detect {|c| c.oid == ctrl.oid}
        cookie = ctrl && ctrl.decode.last
        cookie = nil if cookie.to_s.empty?
      end
    ensure
      conn.set_option(LDAP::LDAP_OPT_SERVER_CONTROLS, []) rescue nil
      conn.set_option(LDAP::LDAP_OPT_SIZELIMIT, 0) rescue nil
    end
  end
end