| Class | ActiveLdap::Adapter::Ldap |
| In: |
lib/active_ldap/adapter/ldap.rb
|
| Parent: | Base |
# File lib/active_ldap/adapter/ldap.rb, line 131
131: def add(dn, entries, options={})
132: super do |_dn, _entries|
133: controls = options[:controls]
134: attributes = parse_entries(_entries)
135: info = {:dn => _dn, :attributes => _entries}
136: if controls
137: info.merge!(:name => :add, :controls => controls)
138: execute(:add_ext, info, _dn, attributes, controls, [])
139: else
140: execute(:add, info, _dn, attributes)
141: end
142: end
143: end
# File lib/active_ldap/adapter/ldap.rb, line 69
69: def bind(options={})
70: super do
71: @connection.error_message
72: end
73: end
# File lib/active_ldap/adapter/ldap.rb, line 75
75: def bind_as_anonymous(options={})
76: super do
77: execute(:bind, :name => "bind: anonymous")
78: true
79: end
80: end
# File lib/active_ldap/adapter/ldap.rb, line 53
53: def connect(options={})
54: super do |host, port, method|
55: uri = construct_uri(host, port, method.ssl?)
56: with_start_tls = method.start_tls?
57: info = {:uri => uri, :with_start_tls => with_start_tls}
58: [log("connect", info) {method.connect(host, port)},
59: uri, with_start_tls]
60: end
61: end
# File lib/active_ldap/adapter/ldap.rb, line 117
117: def delete(targets, options={})
118: super do |target|
119: controls = options[:controls]
120: info = {:dn => target}
121: if controls
122: info.merge!(:name => :delete, :controls => controls)
123: execute(:delete_ext, info,
124: target, controls, [])
125: else
126: execute(:delete, info, target)
127: end
128: end
129: end
# File lib/active_ldap/adapter/ldap.rb, line 145
145: def modify(dn, entries, options={})
146: super do |_dn, _entries|
147: controls = options[:controls]
148: attributes = parse_entries(_entries)
149: info = {:dn => _dn, :attributes => _entries}
150: if controls
151: info.merge!(:name => :modify, :controls => controls)
152: execute(:modify_ext, info, _dn, attributes, controls, [])
153: else
154: execute(:modify, info, _dn, attributes)
155: end
156: end
157: end
# File lib/active_ldap/adapter/ldap.rb, line 159
159: def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={})
160: super do |_dn, _new_rdn, _delete_old_rdn, _new_superior|
161: info = {
162: :name => "modify: RDN",
163: :dn => _dn, :new_rdn => _new_rdn, :delete_old_rdn => _delete_old_rdn
164: }
165: execute(:modrdn, info, _dn, _new_rdn, _delete_old_rdn)
166: end
167: end
# File lib/active_ldap/adapter/ldap.rb, line 82
82: def search(options={}, &block)
83: super(options) do |base, scope, filter, attrs, limit, callback|
84: begin
85: i = 0
86: info = {
87: :base => base, :scope => scope_name(scope),
88: :filter => filter, :attributes => attrs,
89: }
90: execute(:search, info, base, scope, filter, attrs) do |entry|
91: i += 1
92: attributes = {}
93: entry.attrs.each do |attr|
94: attributes[attr] = entry.vals(attr)
95: end
96: callback.call([entry.dn, attributes], block)
97: break if limit and limit <= i
98: end
99: rescue RuntimeError
100: begin
101: @connection.assert_error_code
102: rescue LDAP::ServerDown
103: raise ConnectionError, $!.message
104: end
105: if $!.message == "no result returned by search"
106: @logger.debug do
107: args = [filter, attrs.inspect]
108: _("No matches: filter: %s: attributes: %s") % args
109: end
110: else
111: raise
112: end
113: end
114: end
115: end
# File lib/active_ldap/adapter/ldap.rb, line 63
63: def unbind(options={})
64: super do
65: execute(:unbind)
66: end
67: end
# File lib/active_ldap/adapter/ldap.rb, line 186
186: def ensure_method(method)
187: normalized_method = method.to_s.downcase
188: Method.constants.each do |name|
189: if normalized_method == name.to_s.downcase
190: return Method.const_get(name).new
191: end
192: end
193:
194: available_methods = Method.constants.collect do |name|
195: name.downcase.to_sym.inspect
196: end.join(", ")
197: format = _("%s is not one of the available connect methods: %s")
198: raise ConfigurationError, format % [method.inspect, available_methods]
199: end
# File lib/active_ldap/adapter/ldap.rb, line 269
269: def ensure_mod_type(type)
270: case type
271: when :replace, :add, :delete
272: LDAP.const_get("LDAP_MOD_#{type.to_s.upcase}")
273: else
274: raise ArgumentError, _("unknown type: %s") % type
275: end
276: end
# File lib/active_ldap/adapter/ldap.rb, line 201
201: def ensure_scope(scope)
202: scope_map = {
203: :base => LDAP::LDAP_SCOPE_BASE,
204: :sub => LDAP::LDAP_SCOPE_SUBTREE,
205: :one => LDAP::LDAP_SCOPE_ONELEVEL,
206: }
207: value = scope_map[scope || :sub]
208: if value.nil?
209: available_scopes = scope_map.keys.inspect
210: format = _("%s is not one of the available LDAP scope: %s")
211: raise ArgumentError, format % [scope.inspect, available_scopes]
212: end
213: value
214: end
# File lib/active_ldap/adapter/ldap.rb, line 176
176: def execute(method, info=nil, *args, &block)
177: begin
178: name = (info || {}).delete(:name) || method
179: log(name, info) {@connection.send(method, *args, &block)}
180: rescue LDAP::ResultError
181: @connection.assert_error_code
182: raise $!.message
183: end
184: end
# File lib/active_ldap/adapter/ldap.rb, line 252
252: def parse_entries(entries)
253: result = []
254: entries.each do |type, key, attributes|
255: mod_type = ensure_mod_type(type)
256: binary = schema.attribute(key).binary?
257: mod_type |= LDAP::LDAP_MOD_BVALUES if binary
258: attributes.each do |name, values|
259: additional_mod_type = 0
260: if values.any? {|value| Ldif::Attribute.binary_value?(value)}
261: additional_mod_type |= LDAP::LDAP_MOD_BVALUES
262: end
263: result << LDAP.mod(mod_type | additional_mod_type, name, values)
264: end
265: end
266: result
267: end
# File lib/active_ldap/adapter/ldap.rb, line 170
170: def prepare_connection(options={})
171: operation(options) do
172: @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
173: end
174: end
# File lib/active_ldap/adapter/ldap.rb, line 224
224: def sasl_bind(bind_dn, options={})
225: super do |_bind_dn, mechanism, quiet|
226: begin
227: _bind_dn ||= ''
228: sasl_quiet = @connection.sasl_quiet
229: @connection.sasl_quiet = quiet unless quiet.nil?
230: args = [_bind_dn, mechanism]
231: if need_credential_sasl_mechanism?(mechanism)
232: args << password(_bind_dn, options)
233: end
234: info = {
235: :name => "bind: SASL", :dn => _bind_dn, :mechanism => mechanism
236: }
237: execute(:sasl_bind, info, *args)
238: true
239: ensure
240: @connection.sasl_quiet = sasl_quiet
241: end
242: end
243: end
# File lib/active_ldap/adapter/ldap.rb, line 216
216: def scope_name(scope)
217: {
218: LDAP::LDAP_SCOPE_BASE => :base,
219: LDAP::LDAP_SCOPE_SUBTREE => :sub,
220: LDAP::LDAP_SCOPE_ONELEVEL => :one,
221: }[scope]
222: end