@@ -79,6 +79,8 @@ class << self
7979 # <tt>mail</tt> value containing the substring "anderson":
8080 #
8181 # f = Net::LDAP::Filter.eq("mail", "*anderson*")
82+ #
83+ # This filter does not perform any escaping
8284 def eq ( attribute , value )
8385 new ( :eq , attribute , value )
8486 end
@@ -136,10 +138,44 @@ def ex(attribute, value)
136138 # Creates a Filter object indicating that a particular attribute value
137139 # is either not present or does not match a particular string; see
138140 # Filter::eq for more information.
141+ #
142+ # This filter does not perform any escaping
139143 def ne ( attribute , value )
140144 new ( :ne , attribute , value )
141145 end
142146
147+ ##
148+ # Creates a Filter object indicating that the value of a particular
149+ # attribute must match a particular string. The attribute value is
150+ # escaped, so the "*" character is interpreted literally.
151+ def equals ( attribute , value )
152+ new ( :eq , attribute , escape ( value ) )
153+ end
154+
155+ ##
156+ # Creates a Filter object indicating that the value of a particular
157+ # attribute must begin with a particular string. The attribute value is
158+ # escaped, so the "*" character is interpreted literally.
159+ def begins ( attribute , value )
160+ new ( :eq , attribute , escape ( value ) + "*" )
161+ end
162+
163+ ##
164+ # Creates a Filter object indicating that the value of a particular
165+ # attribute must end with a particular string. The attribute value is
166+ # escaped, so the "*" character is interpreted literally.
167+ def ends ( attribute , value )
168+ new ( :eq , attribute , "*" + escape ( value ) )
169+ end
170+
171+ ##
172+ # Creates a Filter object indicating that the value of a particular
173+ # attribute must contain a particular string. The attribute value is
174+ # escaped, so the "*" character is interpreted literally.
175+ def contains ( attribute , value )
176+ new ( :eq , attribute , "*" + escape ( value ) + "*" )
177+ end
178+
143179 ##
144180 # Creates a Filter object indicating that a particular attribute value
145181 # is greater than or equal to the specified value.
@@ -207,6 +243,12 @@ def present?(attribute)
207243 alias_method :present , :present?
208244 alias_method :pres , :present?
209245
246+ ##
247+ # Escape a string for use in an LDAP filter
248+ def escape ( string )
249+ string . gsub ( /[\* \( \) \\ \0 ]/ ) { |s | sprintf ( "\\ %02x" , s [ 0 ] ) }
250+ end
251+
210252 ##
211253 # Converts an LDAP search filter in BER format to an Net::LDAP::Filter
212254 # object. The incoming BER object most likely came to us by parsing an
0 commit comments