module ActiveLdap::Operations::Find

Public Instance Methods

all(*args) click to toggle source

This is an alias for find(:all). You can pass in all the same arguments to this method as you can to find(:all)

# File lib/active_ldap/operations.rb, line 260
def all(*args)
  find(:all, *args)
end
find(*args) click to toggle source

find

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 225
def find(*args)
  options = extract_options_from_args!(args)
  args = [:first] if args.empty? and !options.empty?
  case args.first
  when :first
    options[:value] ||= args[1]
    find_initial(options)
  when :last
    options[:value] ||= args[1]
    find_last(options)
  when :all
    options[:value] ||= args[1]
    find_every(options)
  else
    find_from_dns(args, options)
  end
end
first(*args) click to toggle source

A convenience wrapper for find(:first, *args). You can pass in all the same arguments to this method as you can to find(:first).

# File lib/active_ldap/operations.rb, line 246
def first(*args)
  find(:first, *args)
end
last(*args) click to toggle source

A convenience wrapper for find(:last, *args). You can pass in all the same arguments to this method as you can to find(:last).

# File lib/active_ldap/operations.rb, line 253
def last(*args)
  find(:last, *args)
end

Private Instance Methods

ensure_dn(target) click to toggle source
# File lib/active_ldap/operations.rb, line 384
def ensure_dn(target)
  attr, value, prefix = split_search_value(target)
  "#{attr || dn_attribute}=#{value},#{prefix || base}"
end
find_every(options) click to toggle source
# File lib/active_ldap/operations.rb, line 286
def find_every(options)
  options = options.dup
  sort_by = options.delete(:sort_by) || self.sort_by
  order = options.delete(:order) || self.order
  limit = options.delete(:limit) if sort_by or order
  offset = options.delete(:offset) || offset
  options[:attributes] = options.delete(:attributes) || ['*']
  options[:attributes] |= ['objectClass']
  if options.delete(:include_operational_attributes)
    options[:attributes] |= ["+"]
  end
  results = search(options).collect do |dn, attrs|
    instantiate([dn, attrs, {:connection => options[:connection]}])
  end
  return results if sort_by.nil? and order.nil?

  sort_by ||= "dn"
  if sort_by.downcase == "dn"
    results = results.sort_by {|result| DN.parse(result.dn)}
  else
    results = results.sort_by {|result| result.send(sort_by)}
  end

  results.reverse! if normalize_sort_order(order || "ascend") == :descend
  results = results[offset, results.size] if offset
  results = results[0, limit] if limit
  results
end
find_from_dns(dns, options) click to toggle source
# File lib/active_ldap/operations.rb, line 315
def find_from_dns(dns, options)
  expects_array = dns.first.is_a?(Array)
  return [] if expects_array and dns.first.empty?

  dns = dns.flatten.compact.uniq

  case dns.size
  when 0
    raise EntryNotFound, _("Couldn't find %s without a DN") % name
  when 1
    result = find_one(dns.first, options)
    expects_array ? [result] : result
  else
    find_some(dns, options)
  end
end
find_initial(options) click to toggle source
# File lib/active_ldap/operations.rb, line 265
def find_initial(options)
  find_every(options.merge(:limit => 1)).first
end
find_last(options) click to toggle source
# File lib/active_ldap/operations.rb, line 269
def find_last(options)
  order = options[:order] || self.order || 'ascend'
  order = normalize_sort_order(order) == :ascend ? :descend : :ascend
  find_initial(options.merge(:order => order))
end
find_one(dn, options) click to toggle source
# File lib/active_ldap/operations.rb, line 332
def find_one(dn, options)
  attr, value, prefix = split_search_value(dn)
  filter = [attr || ensure_search_attribute,
            Escape.ldap_filter_escape(value)]
  filter = [:and, filter, options[:filter]] if options[:filter]
  options = {:prefix => prefix}.merge(options.merge(:filter => filter))
  result = find_initial(options)
  if result
    result
  else
    args = [self.is_a?(Class) ? name : self.class.name,
            dn]
    if options[:filter]
      format = _("Couldn't find %s: DN: %s: filter: %s")
      args << options[:filter].inspect
    else
      format = _("Couldn't find %s: DN: %s")
    end
    raise EntryNotFound, format % args
  end
end
find_some(dns, options) click to toggle source
# File lib/active_ldap/operations.rb, line 354
def find_some(dns, options)
  dn_filters = dns.collect do |dn|
    attr, value, prefix = split_search_value(dn)
    attr ||= ensure_search_attribute
    filter = [attr, value]
    if prefix
      filter = [:and,
                filter,
                [dn, "*,#{Escape.ldap_filter_escape(prefix)},#{base}"]]
    end
    filter
  end
  filter = [:or, *dn_filters]
  filter = [:and, filter, options[:filter]] if options[:filter]
  result = find_every(options.merge(:filter => filter))
  if result.size == dns.size
    result
  else
    args = [self.is_a?(Class) ? name : self.class.name,
            dns.join(", ")]
    if options[:filter]
      format = _("Couldn't find all %s: DNs (%s): filter: %s")
      args << options[:filter].inspect
    else
      format = _("Couldn't find all %s: DNs (%s)")
    end
    raise EntryNotFound, format % args
  end
end
normalize_sort_order(value) click to toggle source
# File lib/active_ldap/operations.rb, line 275
def normalize_sort_order(value)
  case value.to_s
  when /\Aasc(?:end)?\z/i
    :ascend
  when /\Adesc(?:end)?\z/i
    :descend
  else
    raise ArgumentError, _("Invalid order: %s") % value.inspect
  end
end