Method: Net::LDAP::Filter#to_ber
- Defined in:
- lib/net/ldap/filter.rb
permalink #to_ber ⇒ Object
– to_ber Filter ::=
CHOICE {
and [0] SET OF Filter,
or [1] SET OF Filter,
not [2] Filter,
equalityMatch [3] AttributeValueAssertion,
substrings [4] SubstringFilter,
greaterOrEqual [5] AttributeValueAssertion,
lessOrEqual [6] AttributeValueAssertion,
present [7] AttributeType,
approxMatch [8] AttributeValueAssertion
}
SubstringFilter
SEQUENCE {
type AttributeType,
SEQUENCE OF CHOICE {
initial [0] LDAPString,
any [1] LDAPString,
final [2] LDAPString
}
}
Parsing substrings is a little tricky. We use the split method to break a string into substrings delimited by the * (star) character. But we also need to know whether there is a star at the head and tail of the string. A Ruby particularity comes into play here: if you split on * and the first character of the string is a star, then split will return an array whose first element is an empty string. But if the last character of the string is star, then split will return an array that does not add an empty string at the end. So we have to deal with all that specifically.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/net/ldap/filter.rb', line 182 def to_ber case @op when :eq if @right == "*" # present @left.to_s.to_ber_contextspecific 7 elsif @right =~ /[\*]/ #substring ary = @right.split( /[\*]+/ ) final_star = @right =~ /[\*]$/ initial_star = ary.first == "" and ary.shift seq = [] unless initial_star seq << ary.shift.to_ber_contextspecific(0) end n_any_strings = ary.length - (final_star ? 0 : 1) #p n_any_strings n_any_strings.times { seq << ary.shift.to_ber_contextspecific(1) } unless final_star seq << ary.shift.to_ber_contextspecific(2) end [@left.to_s.to_ber, seq.to_ber].to_ber_contextspecific 4 else #equality [@left.to_s.to_ber, @right.to_ber].to_ber_contextspecific 3 end when :ge [@left.to_s.to_ber, @right.to_ber].to_ber_contextspecific 5 when :le [@left.to_s.to_ber, @right.to_ber].to_ber_contextspecific 6 when :and ary = [@left.coalesce(:and), @right.coalesce(:and)].flatten ary.map {|a| a.to_ber}.to_ber_contextspecific( 0 ) when :or ary = [@left.coalesce(:or), @right.coalesce(:or)].flatten ary.map {|a| a.to_ber}.to_ber_contextspecific( 1 ) when :not [@left.to_ber].to_ber_contextspecific 2 else # ERROR, we'll return objectclass=* to keep things from blowing up, # but that ain't a good answer and we need to kick out an error of some kind. raise "unimplemented search filter" end end |