Class: ActiveLdap::Adapter::NetLdap

Inherits:
Base
  • Object
show all
Defined in:
lib/active_ldap/adapter/net_ldap.rb

Constant Summary collapse

METHOD =
{
  :ssl => :simple_tls,
  :tls => :start_tls,
  :plain => nil,
}

Constants inherited from Base

Base::VALID_ADAPTER_CONFIGURATION_KEYS

Instance Method Summary collapse

Methods inherited from Base

#bound?, #connecting?, #disconnect!, #entry_attribute, #initialize, jndi_connection, ldap_connection, #naming_contexts, net_ldap_connection, #rebind, #schema, #supported_control

Methods included from GetTextSupport

included

Constructor Details

This class inherits a constructor from ActiveLdap::Adapter::Base

Instance Method Details

#add(dn, entries, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_ldap/adapter/net_ldap.rb', line 111

def add(dn, entries, options={})
  super do |_dn, _entries|
    attributes = {}
    _entries.each do |type, key, attrs|
      attrs.each do |name, values|
        attributes[name] = values
      end
    end
    args = {:dn => _dn, :attributes => attributes}
    info = args.dup
    execute(:add, info, args)
  end
end

#bind(options = {}) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/active_ldap/adapter/net_ldap.rb', line 56

def bind(options={})
  begin
    super
  rescue Net::LDAP::Error
    raise AuthenticationError, $!.message
  end
end

#bind_as_anonymous(options = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/active_ldap/adapter/net_ldap.rb', line 64

def bind_as_anonymous(options={})
  super do
    execute(:bind, {:name => "bind: anonymous"}, {:method => :anonymous})
    true
  end
end

#connect(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_ldap/adapter/net_ldap.rb', line 23

def connect(options={})
  super do |host, port, method|
    config = {
      :host => host,
      :port => port,
    }
    if method
      config[:encryption] = { :method => method }
      config[:encryption][:tls_options] = @tls_options if @tls_options
    end
    begin
      uri = construct_uri(host, port, method == :simple_tls)
      with_start_tls = method == :start_tls
      info = {:uri => uri, :with_start_tls => with_start_tls}
      [log("connect", info) {Net::LDAP::Connection.new(config)},
       uri, with_start_tls]
    rescue Net::LDAP::ConnectionError => error
      raise ConnectionError, error.message
    rescue Net::LDAP::Error => error
      message = "#{error.class}: #{error.message}"
      raise ConnectionError, message, caller(0) + error.backtrace
    end
  end
end

#delete(targets, options = {}) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/active_ldap/adapter/net_ldap.rb', line 103

def delete(targets, options={})
  super do |target|
    args = {:dn => target}
    info = args.dup
    execute(:delete, info, args)
  end
end

#modify(dn, entries, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/active_ldap/adapter/net_ldap.rb', line 125

def modify(dn, entries, options={})
  super do |_dn, _entries|
    info = {:dn => _dn, :attributes => _entries}
    execute(:modify, info,
            :dn => _dn,
            :operations => parse_entries(_entries))
  end
end

#modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_ldap/adapter/net_ldap.rb', line 134

def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={})
  super do |_dn, _new_rdn, _delete_old_rdn, _new_superior|
    info = {
      :name => "modify: RDN",
      :dn => _dn,
      :new_rdn => _new_rdn,
      :new_superior => _new_superior,
      :delete_old_rdn => _delete_old_rdn
    }
    execute(:rename, info,
            :olddn => _dn,
            :newrdn => _new_rdn,
            :delete_attributes => _delete_old_rdn,
            :new_superior => _new_superior)
  end
end

#search(options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_ldap/adapter/net_ldap.rb', line 71

def search(options={})
  use_paged_results = options[:use_paged_results]
  if use_paged_results or use_paged_results.nil?
    paged_results_supported = supported_control.paged_results?
  else
    paged_results_supported = false
  end
  super(options) do |base, scope, filter, attrs, limit|
    args = {
      :base => base,
      :scope => scope,
      :filter => filter,
      :attributes => attrs,
      :size => limit,
      :paged_searches_supported => paged_results_supported,
    }
    info = {
      :base => base, :scope => scope_name(scope),
      :filter => filter, :attributes => attrs, :limit => limit,
      :paged_results_supported => paged_results_supported,
    }
    execute(:search, info, args) do |entry|
      attributes = {}
      entry.original_attribute_names.each do |name|
        value = entry[name]
        attributes[name] = value if value
      end
      yield([entry.dn, attributes])
    end
  end
end

#unbind(options = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/active_ldap/adapter/net_ldap.rb', line 48

def unbind(options={})
  super do
    log("unbind") do
      @connection.close # Net::LDAP doesn't implement unbind.
    end
  end
end