| 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(:attribute => "cn", :value => "some*val")
Subclass.find('some*val')
# File lib/active_ldap/operations.rb, line 195
195: def find(*args)
196: options = extract_options_from_args!(args)
197: args = [:first] if args.empty? and !options.empty?
198: case args.first
199: when :first
200: find_initial(options)
201: when :all
202: options[:value] ||= args[1]
203: find_every(options)
204: else
205: find_from_dns(args, options)
206: end
207: end
# File lib/active_ldap/operations.rb, line 318
318: def ensure_dn(target)
319: attr, value, prefix = split_search_value(target)
320: "#{attr || dn_attribute}=#{value},#{prefix || base}"
321: end
# File lib/active_ldap/operations.rb, line 225
225: def find_every(options)
226: options = options.dup
227: sort_by = options.delete(:sort_by) || self.sort_by
228: order = options.delete(:order) || self.order
229: limit = options.delete(:limit) if sort_by or order
230: options[:attributes] |= ["objectClass"] if options[:attributes]
231:
232: results = search(options).collect do |dn, attrs|
233: instantiate([dn, attrs, {:connection => options[:connection]}])
234: end
235: return results if sort_by.nil? and order.nil?
236:
237: sort_by ||= "dn"
238: if sort_by.downcase == "dn"
239: results = results.sort_by {|result| DN.parse(result.dn)}
240: else
241: results = results.sort_by {|result| result.send(sort_by)}
242: end
243:
244: results.reverse! if normalize_sort_order(order || "ascend") == :descend
245: results = results[0, limit] if limit
246: results
247: end
# File lib/active_ldap/operations.rb, line 249
249: def find_from_dns(dns, options)
250: expects_array = dns.first.is_a?(Array)
251: return [] if expects_array and dns.first.empty?
252:
253: dns = dns.flatten.compact.uniq
254:
255: case dns.size
256: when 0
257: raise EntryNotFound, _("Couldn't find %s without a DN") % name
258: when 1
259: result = find_one(dns.first, options)
260: expects_array ? [result] : result
261: else
262: find_some(dns, options)
263: end
264: end
# File lib/active_ldap/operations.rb, line 210
210: def find_initial(options)
211: find_every(options.merge(:limit => 1)).first
212: end
# File lib/active_ldap/operations.rb, line 266
266: def find_one(dn, options)
267: attr, value, prefix = split_search_value(dn)
268: filter = [attr || ensure_search_attribute,
269: Escape.ldap_filter_escape(value)]
270: filter = [:and, filter, options[:filter]] if options[:filter]
271: options = {:prefix => prefix}.merge(options.merge(:filter => filter))
272: result = find_initial(options)
273: if result
274: result
275: else
276: args = [self.is_a?(Class) ? name : self.class.name,
277: dn]
278: if options[:filter]
279: format = _("Couldn't find %s: DN: %s: filter: %s")
280: args << options[:filter].inspect
281: else
282: format = _("Couldn't find %s: DN: %s")
283: end
284: raise EntryNotFound, format % args
285: end
286: end
# File lib/active_ldap/operations.rb, line 288
288: def find_some(dns, options)
289: dn_filters = dns.collect do |dn|
290: attr, value, prefix = split_search_value(dn)
291: attr ||= ensure_search_attribute
292: filter = [attr, value]
293: if prefix
294: filter = [:and,
295: filter,
296: [dn, "*,#{Escape.ldap_filter_escape(prefix)},#{base}"]]
297: end
298: filter
299: end
300: filter = [:or, *dn_filters]
301: filter = [:and, filter, options[:filter]] if options[:filter]
302: result = find_every(options.merge(:filter => filter))
303: if result.size == dns.size
304: result
305: else
306: args = [self.is_a?(Class) ? name : self.class.name,
307: dns.join(", ")]
308: if options[:filter]
309: format = _("Couldn't find all %s: DNs (%s): filter: %s")
310: args << options[:filter].inspect
311: else
312: format = _("Couldn't find all %s: DNs (%s)")
313: end
314: raise EntryNotFound, format % args
315: end
316: end