@@ -311,26 +311,37 @@ def encode_sort_controls(sort_definitions)
311311 # type-5 packet, which might never come. We need to support the time-limit
312312 # in the protocol.
313313 #++
314- def search ( args = { } )
315- search_filter = ( args && args [ :filter ] ) ||
316- Net ::LDAP ::Filter . eq ( "objectclass" , "*" )
317- search_filter = Net ::LDAP ::Filter . construct ( search_filter ) if search_filter . is_a? ( String )
318- search_base = ( args && args [ :base ] ) || "dc=example, dc=com"
319- search_attributes = ( ( args && args [ :attributes ] ) || [ ] ) . map { |attr | attr . to_s . to_ber }
320- return_referrals = args && args [ :return_referrals ] == true
321- sizelimit = ( args && args [ :size ] . to_i ) || 0
322- raise Net ::LDAP ::LdapError , "invalid search-size" unless sizelimit >= 0
323- paged_searches_supported = ( args && args [ :paged_searches_supported ] )
324-
325- attributes_only = ( args and args [ :attributes_only ] == true )
326- scope = args [ :scope ] || Net ::LDAP ::SearchScope_WholeSubtree
314+ def search ( args = nil )
315+ args ||= { }
316+
317+ # filtering, scoping, search base
318+ filter = args [ :filter ] || Net ::LDAP ::Filter . eq ( "objectClass" , "*" )
319+ base = args [ :base ]
320+ scope = args [ :scope ] || Net ::LDAP ::SearchScope_WholeSubtree
321+
322+ # attr handling
323+ attrs = Array ( args [ :attributes ] )
324+ attrs_only = args [ :attributes_only ] == true
325+
326+ # references
327+ refs = args [ :return_referrals ] == true
328+ deref = args [ :deref ] || Net ::LDAP ::DerefAliases_Never
329+
330+ # limiting, paging, sorting
331+ size = args [ :size ] . to_i
332+ paged = args [ :paged_searches_supported ]
333+ sort = args . fetch ( :sort_controls , false )
334+
335+ # arg validation
336+ raise Net ::LDAP ::LdapError , "search base is required" unless base
337+ raise Net ::LDAP ::LdapError , "invalid search-size" unless size >= 0
327338 raise Net ::LDAP ::LdapError , "invalid search scope" unless Net ::LDAP ::SearchScopes . include? ( scope )
339+ raise Net ::LDAP ::LdapError , "invalid alias dereferencing value" unless Net ::LDAP ::DerefAliasesArray . include? ( deref )
328340
329- sort_control = encode_sort_controls ( args . fetch ( :sort_controls ) { false } )
330-
331- deref = args [ :deref ] || Net ::LDAP ::DerefAliases_Never
332- raise Net ::LDAP ::LdapError . new ( "invalid alias dereferencing value" ) unless Net ::LDAP ::DerefAliasesArray . include? ( deref )
333-
341+ # arg transforms
342+ filter = Net ::LDAP ::Filter . construct ( filter ) if filter . is_a? ( String )
343+ ber_attrs = attrs . map { |attr | attr . to_s . to_ber }
344+ ber_sort = encode_sort_controls ( sort )
334345
335346 # An interesting value for the size limit would be close to A/D's
336347 # built-in page limit of 1000 records, but openLDAP newer than version
@@ -357,35 +368,35 @@ def search(args = {})
357368 n_results = 0
358369
359370 instrument "search.net_ldap_connection" ,
360- : filter => search_filter ,
361- : base => search_base ,
362- : scope => scope ,
363- : limit => sizelimit ,
364- : sort => sort_control ,
365- : referrals => return_referrals ,
366- : deref => deref ,
367- : attributes => search_attributes do |payload |
371+ filter : filter ,
372+ base : base ,
373+ scope : scope ,
374+ limit : size ,
375+ sort : sort ,
376+ referrals : refs ,
377+ deref : deref ,
378+ attributes : attrs do |payload |
368379 loop do
369380 # should collect this into a private helper to clarify the structure
370381 query_limit = 0
371- if sizelimit > 0
372- if paged_searches_supported
373- query_limit = ( ( ( sizelimit - n_results ) < 126 ) ? ( sizelimit -
382+ if size > 0
383+ if paged
384+ query_limit = ( ( ( size - n_results ) < 126 ) ? ( size -
374385 n_results ) : 0 )
375386 else
376- query_limit = sizelimit
387+ query_limit = size
377388 end
378389 end
379390
380391 request = [
381- search_base . to_ber ,
392+ base . to_ber ,
382393 scope . to_ber_enumerated ,
383394 deref . to_ber_enumerated ,
384395 query_limit . to_ber , # size limit
385396 0 . to_ber ,
386- attributes_only . to_ber ,
387- search_filter . to_ber ,
388- search_attributes . to_ber_sequence
397+ attrs_only . to_ber ,
398+ filter . to_ber ,
399+ ber_attrs . to_ber_sequence
389400 ] . to_ber_appsequence ( 3 )
390401
391402 # rfc2696_cookie sometimes contains binary data from Microsoft Active Directory
@@ -399,8 +410,8 @@ def search(args = {})
399410 # Criticality MUST be false to interoperate with normal LDAPs.
400411 false . to_ber ,
401412 rfc2696_cookie . map { |v | v . to_ber } . to_ber_sequence . to_s . to_ber
402- ] . to_ber_sequence if paged_searches_supported
403- controls << sort_control if sort_control
413+ ] . to_ber_sequence if paged
414+ controls << ber_sort if ber_sort
404415 controls = controls . empty? ? nil : controls . to_ber_contextspecific ( 0 )
405416
406417 write ( request , controls )
@@ -414,7 +425,7 @@ def search(args = {})
414425 n_results += 1
415426 yield pdu . search_entry if block_given?
416427 when Net ::LDAP ::PDU ::SearchResultReferral
417- if return_referrals
428+ if refs
418429 if block_given?
419430 se = Net ::LDAP ::Entry . new
420431 se [ :search_referrals ] = ( pdu . search_referrals || [ ] )
@@ -424,7 +435,7 @@ def search(args = {})
424435 when Net ::LDAP ::PDU ::SearchResult
425436 result_pdu = pdu
426437 controls = pdu . result_controls
427- if return_referrals && pdu . result_code == 10
438+ if refs && pdu . result_code == 10
428439 if block_given?
429440 se = Net ::LDAP ::Entry . new
430441 se [ :search_referrals ] = ( pdu . search_referrals || [ ] )
0 commit comments