From 2b259db59a5b199b82a0b6b9d3b2c72dd505df6d Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Wed, 17 Dec 2014 14:49:21 -0700 Subject: [PATCH] refactor connection establishment reduces duplicated code, and makes it easier for future changes to connection establishment because it's all in one place now --- Contributors.rdoc | 1 + lib/net/ldap.rb | 132 +++++++++++++--------------------------------- 2 files changed, 39 insertions(+), 94 deletions(-) diff --git a/Contributors.rdoc b/Contributors.rdoc index bef012a9..b3b25ff6 100644 --- a/Contributors.rdoc +++ b/Contributors.rdoc @@ -20,3 +20,4 @@ Contributions since: * Erik Hetzner (egh) * nowhereman * David J. Lee (DavidJLee) +* Cody Cutrer (ccutrer) diff --git a/lib/net/ldap.rb b/lib/net/ldap.rb index b181d83d..1727eb3b 100644 --- a/lib/net/ldap.rb +++ b/lib/net/ldap.rb @@ -670,12 +670,7 @@ def open instrument "open.net_ldap" do |payload| begin - @open_connection = - Net::LDAP::Connection.new \ - :host => @host, - :port => @port, - :encryption => @encryption, - :instrumentation_service => @instrumentation_service + @open_connection = new_connection payload[:connection] = @open_connection payload[:bind] = @open_connection.bind(@auth) yield self @@ -745,27 +740,11 @@ def search(args = {}) result_set = return_result_set ? [] : nil instrument "search.net_ldap", args do |payload| - if @open_connection - @result = @open_connection.search(args) { |entry| + use_connection(args[:auth]) do |conn| + @result = conn.search(args) { |entry| result_set << entry if result_set yield entry if block_given? } - else - begin - conn = Net::LDAP::Connection.new \ - :host => @host, - :port => @port, - :encryption => @encryption, - :instrumentation_service => @instrumentation_service - if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess - @result = conn.search(args) { |entry| - result_set << entry if result_set - yield entry if block_given? - } - end - ensure - conn.close if conn - end end if return_result_set @@ -844,11 +823,7 @@ def bind(auth = @auth) payload[:bind] = @result = @open_connection.bind(auth) else begin - conn = Connection.new \ - :host => @host, - :port => @port, - :encryption => @encryption, - :instrumentation_service => @instrumentation_service + conn = new_connection payload[:connection] = conn payload[:bind] = @result = conn.bind(auth) ensure @@ -946,22 +921,8 @@ def bind_as(args = {}) # end def add(args) instrument "add.net_ldap", args do |payload| - if @open_connection - @result = @open_connection.add(args) - else - @result = 0 - begin - conn = Connection.new \ - :host => @host, - :port => @port, - :encryption => @encryption, - :instrumentation_service => @instrumentation_service - if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess - @result = conn.add(args) - end - ensure - conn.close if conn - end + use_connection(args[:auth]) do |conn| + @result = conn.add(args) end @result.success? end @@ -1050,24 +1011,9 @@ def add(args) # does _not_ imply transactional atomicity, which LDAP does not provide. def modify(args) instrument "modify.net_ldap", args do |payload| - if @open_connection - @result = @open_connection.modify(args) - else - @result = 0 - begin - conn = Connection.new \ - :host => @host, - :port => @port, - :encryption => @encryption, - :instrumentation_service => @instrumentation_service - if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess - @result = conn.modify(args) - end - ensure - conn.close if conn - end + use_connection(args[:auth]) do |conn| + @result = conn.modify(args) end - @result.success? end end @@ -1127,22 +1073,8 @@ def delete_attribute(dn, attribute) # _Documentation_ _stub_ def rename(args) instrument "rename.net_ldap", args do |payload| - if @open_connection - @result = @open_connection.rename(args) - else - @result = 0 - begin - conn = Connection.new \ - :host => @host, - :port => @port, - :encryption => @encryption, - :instrumentation_service => @instrumentation_service - if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess - @result = conn.rename(args) - end - ensure - conn.close if conn - end + use_connection(args[:auth]) do |conn| + @result = conn.rename(args) end @result.success? end @@ -1160,22 +1092,8 @@ def rename(args) # ldap.delete :dn => dn def delete(args) instrument "delete.net_ldap", args do |payload| - if @open_connection - @result = @open_connection.delete(args) - else - @result = 0 - begin - conn = Connection.new \ - :host => @host, - :port => @port, - :encryption => @encryption, - :instrumentation_service => @instrumentation_service - if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess - @result = conn.delete(args) - end - ensure - conn.close - end + use_connection(args[:auth]) do |conn| + @result = conn.delete(args) end @result.success? end @@ -1277,4 +1195,30 @@ def paged_searches_supported? @server_caps ||= search_root_dse @server_caps[:supportedcontrol].include?(Net::LDAP::LDAPControls::PAGED_RESULTS) end + + private + + def use_connection(auth) + if @open_connection + yield @open_connection + else + @result = 0 + begin + conn = new_connection + if (@result = conn.bind(auth || @auth)).result_code == Net::LDAP::ResultCodeSuccess + yield conn + end + ensure + conn.close if conn + end + end + end + + def new_connection + Net::LDAP::Connection.new \ + :host => @host, + :port => @port, + :encryption => @encryption, + :instrumentation_service => @instrumentation_service + end end # class LDAP