|
| 1 | +# Encoding: UTF-8 |
1 | 2 | # Copyright (C) 2006 by Francis Cianfrocca and other contributors. All |
2 | 3 | # Rights Reserved. |
3 | 4 | # |
@@ -79,6 +80,8 @@ class << self |
79 | 80 | # <tt>mail</tt> value containing the substring "anderson": |
80 | 81 | # |
81 | 82 | # f = Net::LDAP::Filter.eq("mail", "*anderson*") |
| 83 | + # |
| 84 | + # This filter does not perform any escaping |
82 | 85 | def eq(attribute, value) |
83 | 86 | new(:eq, attribute, value) |
84 | 87 | end |
@@ -136,10 +139,44 @@ def ex(attribute, value) |
136 | 139 | # Creates a Filter object indicating that a particular attribute value |
137 | 140 | # is either not present or does not match a particular string; see |
138 | 141 | # Filter::eq for more information. |
| 142 | + # |
| 143 | + # This filter does not perform any escaping |
139 | 144 | def ne(attribute, value) |
140 | 145 | new(:ne, attribute, value) |
141 | 146 | end |
142 | 147 |
|
| 148 | + ## |
| 149 | + # Creates a Filter object indicating that the value of a particular |
| 150 | + # attribute must match a particular string. The attribute value is |
| 151 | + # escaped, so the "*" character is interpreted literally. |
| 152 | + def equals(attribute, value) |
| 153 | + new(:eq, attribute, escape(value)) |
| 154 | + end |
| 155 | + |
| 156 | + ## |
| 157 | + # Creates a Filter object indicating that the value of a particular |
| 158 | + # attribute must begin with a particular string. The attribute value is |
| 159 | + # escaped, so the "*" character is interpreted literally. |
| 160 | + def begins(attribute, value) |
| 161 | + new(:eq, attribute, escape(value) + "*") |
| 162 | + end |
| 163 | + |
| 164 | + ## |
| 165 | + # Creates a Filter object indicating that the value of a particular |
| 166 | + # attribute must end with a particular string. The attribute value is |
| 167 | + # escaped, so the "*" character is interpreted literally. |
| 168 | + def ends(attribute, value) |
| 169 | + new(:eq, attribute, "*" + escape(value)) |
| 170 | + end |
| 171 | + |
| 172 | + ## |
| 173 | + # Creates a Filter object indicating that the value of a particular |
| 174 | + # attribute must contain a particular string. The attribute value is |
| 175 | + # escaped, so the "*" character is interpreted literally. |
| 176 | + def contains(attribute, value) |
| 177 | + new(:eq, attribute, "*" + escape(value) + "*") |
| 178 | + end |
| 179 | + |
143 | 180 | ## |
144 | 181 | # Creates a Filter object indicating that a particular attribute value |
145 | 182 | # is greater than or equal to the specified value. |
@@ -207,6 +244,30 @@ def present?(attribute) |
207 | 244 | alias_method :present, :present? |
208 | 245 | alias_method :pres, :present? |
209 | 246 |
|
| 247 | + # http://tools.ietf.org/html/rfc4515 lists these exceptions from UTF1 |
| 248 | + # charset for filters. All of the following must be escaped in any normal |
| 249 | + # string using a single backslash ('\') as escape. |
| 250 | + # |
| 251 | + ESCAPES = { |
| 252 | + '!' => '21', # EXCLAMATION = %x21 ; exclamation mark ("!") |
| 253 | + '&' => '26', # AMPERSAND = %x26 ; ampersand (or AND symbol) ("&") |
| 254 | + '*' => '2A', # ASTERISK = %x2A ; asterisk ("*") |
| 255 | + ':' => '3A', # COLON = %x3A ; colon (":") |
| 256 | + '|' => '7C', # VERTBAR = %x7C ; vertical bar (or pipe) ("|") |
| 257 | + '~' => '7E', # TILDE = %x7E ; tilde ("~") |
| 258 | + } |
| 259 | + # Compiled character class regexp using the keys from the above hash. |
| 260 | + ESCAPE_RE = Regexp.new( |
| 261 | + "[" + |
| 262 | + ESCAPES.keys.map { |e| Regexp.escape(e) }.join + |
| 263 | + "]") |
| 264 | + |
| 265 | + ## |
| 266 | + # Escape a string for use in an LDAP filter |
| 267 | + def escape(string) |
| 268 | + string.gsub(ESCAPE_RE) { |char| "\\" + ESCAPES[char] } |
| 269 | + end |
| 270 | + |
210 | 271 | ## |
211 | 272 | # Converts an LDAP search filter in BER format to an Net::LDAP::Filter |
212 | 273 | # object. The incoming BER object most likely came to us by parsing an |
|
0 commit comments