Class Net::LDAP::Filter
In: lib/net/ldap/filter.rb
Parent: Object

Class Net::LDAP::Filter is used to constrain LDAP searches. An object of this class is passed to Net::LDAP#search in the parameter :filter.

Net::LDAP::Filter supports the complete set of search filters available in LDAP, including conjunction, disjunction and negation (AND, OR, and NOT). This class supplants the (infamous) RFC 2254 standard notation for specifying LDAP search filters.

Here‘s how to code the familiar "objectclass is present" filter:

 f = Net::LDAP::Filter.present("objectclass")

The object returned by this code can be passed directly to the :filter parameter of Net::LDAP#search.

See the individual class and instance methods below for more examples.

Methods

&   ==   begins   bineq   construct   contains   ends   eq   equals   escape   ex   execute   ge   intersect   join   le   match   ne   negate   parse_ber   parse_ldap_filter   present?   to_ber   to_raw_rfc2254   to_rfc2254   to_s   |   ~  

Constants

FilterTypes = [ :ne, :eq, :ge, :le, :and, :or, :not, :ex, :bineq ]   Known filter types.
ESCAPES = { "\0" => '00', # NUL = %x00 ; null character '*' => '2A', # ASTERISK = %x2A ; asterisk ("*") '(' => '28', # LPARENS = %x28 ; left parenthesis ("(") ')' => '29', # RPARENS = %x29 ; right parenthesis (")") '\\' => '5C', # ESC = %x5C ; esc (or backslash) ("\") }   tools.ietf.org/html/rfc4515 lists these exceptions from UTF1 charset for filters. All of the following must be escaped in any normal string using a single backslash (’\’) as escape.
ESCAPE_RE = Regexp.new( "[" + ESCAPES.keys.map { |e| Regexp.escape(e) }.join + "]")   Compiled character class regexp using the keys from the above hash.

External Aliases

present? -> present
present? -> pres
construct -> from_rfc2254
construct -> from_rfc4515

Public Class methods

Creates a Filter object indicating that the value of a particular attribute must begin with a particular string. The attribute value is escaped, so the "*" character is interpreted literally.

Creates a Filter object indicating a binary comparison. this prevents the search data from being forced into a UTF-8 string.

This is primarily used for Microsoft Active Directory to compare GUID values.

   # for guid represented as hex charecters
   guid = "6a31b4a12aa27a41aca9603f27dd5116"
   guid_bin = [guid].pack("H*")
   f = Net::LDAP::Filter.bineq("objectGUID", guid_bin)

This filter does not perform any escaping.

Converts an LDAP filter-string (in the prefix syntax specified in RFC-2254) to a Net::LDAP::Filter.

Creates a Filter object indicating that the value of a particular attribute must contain a particular string. The attribute value is escaped, so the "*" character is interpreted literally.

Creates a Filter object indicating that the value of a particular attribute must end with a particular string. The attribute value is escaped, so the "*" character is interpreted literally.

Creates a Filter object indicating that the value of a particular attribute must either be present or match a particular string.

Specifying that an attribute is ‘present’ means only directory entries which contain a value for the particular attribute will be selected by the filter. This is useful in case of optional attributes such as mail. Presence is indicated by giving the value "*" in the second parameter to eq. This example selects only entries that have one or more values for sAMAccountName:

  f = Net::LDAP::Filter.eq("sAMAccountName", "*")

To match a particular range of values, pass a string as the second parameter to eq. The string may contain one or more "*" characters as wildcards: these match zero or more occurrences of any character. Full regular-expressions are not supported due to limitations in the underlying LDAP protocol. This example selects any entry with a mail value containing the substring "anderson":

  f = Net::LDAP::Filter.eq("mail", "*anderson*")

This filter does not perform any escaping

Creates a Filter object indicating that the value of a particular attribute must match a particular string. The attribute value is escaped, so the "*" character is interpreted literally.

Escape a string for use in an LDAP filter

Creates a Filter object indicating extensible comparison. This Filter object is currently considered EXPERIMENTAL.

  sample_attributes = ['cn:fr', 'cn:fr.eq',
    'cn:1.3.6.1.4.1.42.2.27.9.4.49.1.3', 'cn:dn:fr', 'cn:dn:fr.eq']
  attr = sample_attributes.first # Pick an extensible attribute
  value = 'roberts'

  filter = "#{attr}:=#{value}" # Basic String Filter
  filter = Net::LDAP::Filter.ex(attr, value) # Net::LDAP::Filter

  # Perform a search with the Extensible Match Filter
  Net::LDAP.search(:filter => filter)

Creates a Filter object indicating that a particular attribute value is greater than or equal to the specified value.

Creates a disjoint comparison between two or more filters. Selects entries where either the left or right side are true. Calling Filter.intersect(left, right) is the same as left | right.

  # Selects only entries that have an <tt>objectclass</tt> attribute.
  x = Net::LDAP::Filter.present("objectclass")
  # Selects only entries that have a <tt>mail</tt> attribute that begins
  # with "George".
  y = Net::LDAP::Filter.eq("mail", "George*")
  # Selects only entries that meet either condition above.
  z = x | y

Joins two or more filters so that all conditions must be true. Calling Filter.join(left, right) is the same as left & right.

  # Selects only entries that have an <tt>objectclass</tt> attribute.
  x = Net::LDAP::Filter.present("objectclass")
  # Selects only entries that have a <tt>mail</tt> attribute that begins
  # with "George".
  y = Net::LDAP::Filter.eq("mail", "George*")
  # Selects only entries that meet both conditions above.
  z = Net::LDAP::Filter.join(x, y)

Creates a Filter object indicating that a particular attribute value is less than or equal to the specified value.

Creates a Filter object indicating that a particular attribute value is either not present or does not match a particular string; see Filter::eq for more information.

This filter does not perform any escaping

Negates a filter. Calling Fitler.negate(filter) i s the same as ~filter.

  # Selects only entries that do not have an <tt>objectclass</tt>
  # attribute.
  x = ~Net::LDAP::Filter.present("objectclass")

Converts an LDAP search filter in BER format to an Net::LDAP::Filter object. The incoming BER object most likely came to us by parsing an LDAP searchRequest PDU. See also the comments under to_ber, including the grammar snippet from the RFC.

Convert an RFC-1777 LDAP/BER "Filter" object to a Net::LDAP::Filter object.

This is a synonym for eq(attribute, "*"). Also known as present and pres.

Public Instance methods

Joins two or more filters so that all conditions must be true.

  # Selects only entries that have an <tt>objectclass</tt> attribute.
  x = Net::LDAP::Filter.present("objectclass")
  # Selects only entries that have a <tt>mail</tt> attribute that begins
  # with "George".
  y = Net::LDAP::Filter.eq("mail", "George*")
  # Selects only entries that meet both conditions above.
  z = x & y

Equality operator for filters, useful primarily for constructing unit tests.

Perform filter operations against a user-supplied block. This is useful when implementing an LDAP directory server. The caller‘s block will be called with two arguments: first, a symbol denoting the "operation" of the filter; and second, an array consisting of arguments to the operation. The user-supplied block (which is MANDATORY) should perform some desired application-defined processing, and may return a locally-meaningful object that will appear as a parameter in the :and, :or and :not operations detailed below.

A typical object to return from the user-supplied block is an array of Net::LDAP::Filter objects.

These are the possible values that may be passed to the user-supplied block:

  * :equalityMatch (the arguments will be an attribute name and a value
    to be matched);
  * :substrings (two arguments: an attribute name and a value containing
    one or more "*" characters);
  * :present (one argument: an attribute name);
  * :greaterOrEqual (two arguments: an attribute name and a value to be
    compared against);
  * :lessOrEqual (two arguments: an attribute name and a value to be
    compared against);
  * :and (two or more arguments, each of which is an object returned
    from a recursive call to #execute, with the same block;
  * :or (two or more arguments, each of which is an object returned from
    a recursive call to #execute, with the same block; and
  * :not (one argument, which is an object returned from a recursive
    call to #execute with the the same block.

Converts the filter to BER format.

Converts the Filter object to an RFC 2254-compatible text format.

Creates a disjoint comparison between two or more filters. Selects entries where either the left or right side are true.

  # Selects only entries that have an <tt>objectclass</tt> attribute.
  x = Net::LDAP::Filter.present("objectclass")
  # Selects only entries that have a <tt>mail</tt> attribute that begins
  # with "George".
  y = Net::LDAP::Filter.eq("mail", "George*")
  # Selects only entries that meet either condition above.
  z = x | y

Negates a filter.

  # Selects only entries that do not have an <tt>objectclass</tt>
  # attribute.
  x = ~Net::LDAP::Filter.present("objectclass")

[Validate]