@@ -79,6 +79,8 @@ class << self
79
79
# <tt>mail</tt> value containing the substring "anderson":
80
80
#
81
81
# f = Net::LDAP::Filter.eq("mail", "*anderson*")
82
+ #
83
+ # This filter does not perform any escaping
82
84
def eq ( attribute , value )
83
85
new ( :eq , attribute , value )
84
86
end
@@ -136,10 +138,44 @@ def ex(attribute, value)
136
138
# Creates a Filter object indicating that a particular attribute value
137
139
# is either not present or does not match a particular string; see
138
140
# Filter::eq for more information.
141
+ #
142
+ # This filter does not perform any escaping
139
143
def ne ( attribute , value )
140
144
new ( :ne , attribute , value )
141
145
end
142
146
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
+
143
179
##
144
180
# Creates a Filter object indicating that a particular attribute value
145
181
# is greater than or equal to the specified value.
@@ -207,6 +243,12 @@ def present?(attribute)
207
243
alias_method :present , :present?
208
244
alias_method :pres , :present?
209
245
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
+
210
252
##
211
253
# Converts an LDAP search filter in BER format to an Net::LDAP::Filter
212
254
# object. The incoming BER object most likely came to us by parsing an
0 commit comments