| 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 175
175: def find(*args)
176: options = extract_options_from_args!(args)
177: args = [:first] if args.empty? and !options.empty?
178: case args.first
179: when :first
180: find_initial(options)
181: when :all
182: options[:value] ||= args[1]
183: find_every(options)
184: else
185: find_from_dns(args, options)
186: end
187: end
# File lib/active_ldap/operations.rb, line 297
297: def ensure_dn(target)
298: attr, value, prefix = split_search_value(target)
299: "#{attr || dn_attribute}=#{value},#{prefix || base}"
300: end
# File lib/active_ldap/operations.rb, line 205
205: def find_every(options)
206: options = options.dup
207: sort_by = options.delete(:sort_by) || sort_by
208: order = options.delete(:order) || order
209: limit = options.delete(:limit) if sort_by or order
210:
211: results = search(options).collect do |dn, attrs|
212: instantiate([dn, attrs, {:connection => options[:connection]}])
213: end
214: return results if sort_by.nil? and order.nil?
215:
216: sort_by ||= "dn"
217: if sort_by.downcase == "dn"
218: results = results.sort_by {|result| DN.parse(result.dn)}
219: else
220: results = results.sort_by {|result| result.send(sort_by)}
221: end
222:
223: results.reverse! if normalize_sort_order(order || "ascend") == :descend
224: results = results[0, limit] if limit
225: results
226: end
# File lib/active_ldap/operations.rb, line 228
228: def find_from_dns(dns, options)
229: expects_array = dns.first.is_a?(Array)
230: return [] if expects_array and dns.first.empty?
231:
232: dns = dns.flatten.compact.uniq
233:
234: case dns.size
235: when 0
236: raise EntryNotFound, _("Couldn't find %s without a DN") % name
237: when 1
238: result = find_one(dns.first, options)
239: expects_array ? [result] : result
240: else
241: find_some(dns, options)
242: end
243: end
# File lib/active_ldap/operations.rb, line 190
190: def find_initial(options)
191: find_every(options.merge(:limit => 1)).first
192: end
# File lib/active_ldap/operations.rb, line 245
245: def find_one(dn, options)
246: attr, value, prefix = split_search_value(dn)
247: filter = [attr || ensure_search_attribute,
248: Escape.ldap_filter_escape(value)]
249: filter = [:and, filter, options[:filter]] if options[:filter]
250: options = {:prefix => prefix}.merge(options.merge(:filter => filter))
251: result = find_initial(options)
252: if result
253: result
254: else
255: args = [self.is_a?(Class) ? name : self.class.name,
256: dn]
257: if options[:filter]
258: format = _("Couldn't find %s: DN: %s: filter: %s")
259: args << options[:filter].inspect
260: else
261: format = _("Couldn't find %s: DN: %s")
262: end
263: raise EntryNotFound, format % args
264: end
265: end
# File lib/active_ldap/operations.rb, line 267
267: def find_some(dns, options)
268: dn_filters = dns.collect do |dn|
269: attr, value, prefix = split_search_value(dn)
270: attr ||= ensure_search_attribute
271: filter = [attr, value]
272: if prefix
273: filter = [:and,
274: filter,
275: [dn, "*,#{Escape.ldap_filter_escape(prefix)},#{base}"]]
276: end
277: filter
278: end
279: filter = [:or, *dn_filters]
280: filter = [:and, filter, options[:filter]] if options[:filter]
281: result = find_every(options.merge(:filter => filter))
282: if result.size == dns.size
283: result
284: else
285: args = [self.is_a?(Class) ? name : self.class.name,
286: dns.join(", ")]
287: if options[:filter]
288: format = _("Couldn't find all %s: DNs (%s): filter: %s")
289: args << options[:filter].inspect
290: else
291: format = _("Couldn't find all %s: DNs (%s)")
292: end
293: raise EntryNotFound, format % args
294: end
295: end