Skip to content

Commit a696aa8

Browse files
Jamstahkschiess
authored andcommitted
Add a method to escape filter strings, and some convenience method for creating some common filter patterns, that are automatically escaped.
1 parent cee9fb6 commit a696aa8

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/net/ldap/filter.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/test_filter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def test_to_s
2424
assert_equal("(uid=george *)", Filter.eq("uid", "george *").to_s)
2525
end
2626

27+
def test_convenience_filters
28+
assert_equal("(uid=\\2a)", Filter.equals("uid", "*").to_s)
29+
assert_equal("(uid=\\28*)", Filter.begins("uid", "(").to_s)
30+
assert_equal("(uid=*\\29)", Filter.ends("uid", ")").to_s)
31+
assert_equal("(uid=*\\5c*)", Filter.contains("uid", "\\").to_s)
32+
end
33+
2734
def test_c2
2835
assert_equal("(uid=george *)",
2936
Filter.from_rfc2254("uid=george *").to_rfc2254)

0 commit comments

Comments
 (0)