| Module | ActiveLdap::Operations::Find |
| In: |
lib/active_ldap/operations.rb
|
Finds the first match for value where |value| is the value of some |field|, or the wildcard match. This is only useful for derived classes. usage: Subclass.find(:all, :attribute => "cn", :value => "some*val")
Subclass.find(:all, 'some*val')
# File lib/active_ldap/operations.rb, line 216
216: def find(*args)
217: options = extract_options_from_args!(args)
218: args = [:first] if args.empty? and !options.empty?
219: case args.first
220: when :first
221: options[:value] ||= args[1]
222: find_initial(options)
223: when :last
224: options[:value] ||= args[1]
225: find_last(options)
226: when :all
227: options[:value] ||= args[1]
228: find_every(options)
229: else
230: find_from_dns(args, options)
231: end
232: end
# File lib/active_ldap/operations.rb, line 370
370: def ensure_dn(target)
371: attr, value, prefix = split_search_value(target)
372: "#{attr || dn_attribute}=#{value},#{prefix || base}"
373: end
# File lib/active_ldap/operations.rb, line 277
277: def find_every(options)
278: options = options.dup
279: sort_by = options.delete(:sort_by) || self.sort_by
280: order = options.delete(:order) || self.order
281: limit = options.delete(:limit) if sort_by or order
282: options[:attributes] |= ["objectClass"] if options[:attributes]
283:
284: results = search(options).collect do |dn, attrs|
285: instantiate([dn, attrs, {:connection => options[:connection]}])
286: end
287: return results if sort_by.nil? and order.nil?
288:
289: sort_by ||= "dn"
290: if sort_by.downcase == "dn"
291: results = results.sort_by {|result| DN.parse(result.dn)}
292: else
293: results = results.sort_by {|result| result.send(sort_by)}
294: end
295:
296: results.reverse! if normalize_sort_order(order || "ascend") == :descend
297: results = results[0, limit] if limit
298: results
299: end
# File lib/active_ldap/operations.rb, line 301
301: def find_from_dns(dns, options)
302: expects_array = dns.first.is_a?(Array)
303: return [] if expects_array and dns.first.empty?
304:
305: dns = dns.flatten.compact.uniq
306:
307: case dns.size
308: when 0
309: raise EntryNotFound, _("Couldn't find %s without a DN") % name
310: when 1
311: result = find_one(dns.first, options)
312: expects_array ? [result] : result
313: else
314: find_some(dns, options)
315: end
316: end
# File lib/active_ldap/operations.rb, line 256
256: def find_initial(options)
257: find_every(options.merge(:limit => 1)).first
258: end
# File lib/active_ldap/operations.rb, line 260
260: def find_last(options)
261: order = options[:order] || self.order || 'ascend'
262: order = normalize_sort_order(order) == :ascend ? :descend : :ascend
263: find_initial(options.merge(:order => order))
264: end
# File lib/active_ldap/operations.rb, line 318
318: def find_one(dn, options)
319: attr, value, prefix = split_search_value(dn)
320: filter = [attr || ensure_search_attribute,
321: Escape.ldap_filter_escape(value)]
322: filter = [:and, filter, options[:filter]] if options[:filter]
323: options = {:prefix => prefix}.merge(options.merge(:filter => filter))
324: result = find_initial(options)
325: if result
326: result
327: else
328: args = [self.is_a?(Class) ? name : self.class.name,
329: dn]
330: if options[:filter]
331: format = _("Couldn't find %s: DN: %s: filter: %s")
332: args << options[:filter].inspect
333: else
334: format = _("Couldn't find %s: DN: %s")
335: end
336: raise EntryNotFound, format % args
337: end
338: end
# File lib/active_ldap/operations.rb, line 340
340: def find_some(dns, options)
341: dn_filters = dns.collect do |dn|
342: attr, value, prefix = split_search_value(dn)
343: attr ||= ensure_search_attribute
344: filter = [attr, value]
345: if prefix
346: filter = [:and,
347: filter,
348: [dn, "*,#{Escape.ldap_filter_escape(prefix)},#{base}"]]
349: end
350: filter
351: end
352: filter = [:or, *dn_filters]
353: filter = [:and, filter, options[:filter]] if options[:filter]
354: result = find_every(options.merge(:filter => filter))
355: if result.size == dns.size
356: result
357: else
358: args = [self.is_a?(Class) ? name : self.class.name,
359: dns.join(", ")]
360: if options[:filter]
361: format = _("Couldn't find all %s: DNs (%s): filter: %s")
362: args << options[:filter].inspect
363: else
364: format = _("Couldn't find all %s: DNs (%s)")
365: end
366: raise EntryNotFound, format % args
367: end
368: end