Skip to content

Commit a89d094

Browse files
committed
Merge pull request #180 from ccutrer/use_connection
refactor connection establishment
2 parents ec7b5dc + 443fdb1 commit a89d094

File tree

2 files changed

+45
-94
lines changed

2 files changed

+45
-94
lines changed

Contributors.rdoc

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Contributions since:
2020
* Erik Hetzner (egh)
2121
* nowhereman
2222
* David J. Lee (DavidJLee)
23+
* Cody Cutrer (ccutrer)

lib/net/ldap.rb

+44-94
Original file line numberDiff line numberDiff line change
@@ -670,12 +670,7 @@ def open
670670

671671
instrument "open.net_ldap" do |payload|
672672
begin
673-
@open_connection =
674-
Net::LDAP::Connection.new \
675-
:host => @host,
676-
:port => @port,
677-
:encryption => @encryption,
678-
:instrumentation_service => @instrumentation_service
673+
@open_connection = new_connection
679674
payload[:connection] = @open_connection
680675
payload[:bind] = @open_connection.bind(@auth)
681676
yield self
@@ -745,27 +740,11 @@ def search(args = {})
745740
result_set = return_result_set ? [] : nil
746741

747742
instrument "search.net_ldap", args do |payload|
748-
if @open_connection
749-
@result = @open_connection.search(args) { |entry|
743+
@result = use_connection(args) do |conn|
744+
conn.search(args) { |entry|
750745
result_set << entry if result_set
751746
yield entry if block_given?
752747
}
753-
else
754-
begin
755-
conn = Net::LDAP::Connection.new \
756-
:host => @host,
757-
:port => @port,
758-
:encryption => @encryption,
759-
:instrumentation_service => @instrumentation_service
760-
if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
761-
@result = conn.search(args) { |entry|
762-
result_set << entry if result_set
763-
yield entry if block_given?
764-
}
765-
end
766-
ensure
767-
conn.close if conn
768-
end
769748
end
770749

771750
if return_result_set
@@ -844,11 +823,7 @@ def bind(auth = @auth)
844823
payload[:bind] = @result = @open_connection.bind(auth)
845824
else
846825
begin
847-
conn = Connection.new \
848-
:host => @host,
849-
:port => @port,
850-
:encryption => @encryption,
851-
:instrumentation_service => @instrumentation_service
826+
conn = new_connection
852827
payload[:connection] = conn
853828
payload[:bind] = @result = conn.bind(auth)
854829
ensure
@@ -946,22 +921,8 @@ def bind_as(args = {})
946921
# end
947922
def add(args)
948923
instrument "add.net_ldap", args do |payload|
949-
if @open_connection
950-
@result = @open_connection.add(args)
951-
else
952-
@result = 0
953-
begin
954-
conn = Connection.new \
955-
:host => @host,
956-
:port => @port,
957-
:encryption => @encryption,
958-
:instrumentation_service => @instrumentation_service
959-
if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
960-
@result = conn.add(args)
961-
end
962-
ensure
963-
conn.close if conn
964-
end
924+
@result = use_connection(args) do |conn|
925+
conn.add(args)
965926
end
966927
@result.success?
967928
end
@@ -1050,24 +1011,9 @@ def add(args)
10501011
# does _not_ imply transactional atomicity, which LDAP does not provide.
10511012
def modify(args)
10521013
instrument "modify.net_ldap", args do |payload|
1053-
if @open_connection
1054-
@result = @open_connection.modify(args)
1055-
else
1056-
@result = 0
1057-
begin
1058-
conn = Connection.new \
1059-
:host => @host,
1060-
:port => @port,
1061-
:encryption => @encryption,
1062-
:instrumentation_service => @instrumentation_service
1063-
if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
1064-
@result = conn.modify(args)
1065-
end
1066-
ensure
1067-
conn.close if conn
1068-
end
1014+
@result = use_connection(args) do |conn|
1015+
conn.modify(args)
10691016
end
1070-
10711017
@result.success?
10721018
end
10731019
end
@@ -1127,22 +1073,8 @@ def delete_attribute(dn, attribute)
11271073
# _Documentation_ _stub_
11281074
def rename(args)
11291075
instrument "rename.net_ldap", args do |payload|
1130-
if @open_connection
1131-
@result = @open_connection.rename(args)
1132-
else
1133-
@result = 0
1134-
begin
1135-
conn = Connection.new \
1136-
:host => @host,
1137-
:port => @port,
1138-
:encryption => @encryption,
1139-
:instrumentation_service => @instrumentation_service
1140-
if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
1141-
@result = conn.rename(args)
1142-
end
1143-
ensure
1144-
conn.close if conn
1145-
end
1076+
@result = use_connection(args) do |conn|
1077+
conn.rename(args)
11461078
end
11471079
@result.success?
11481080
end
@@ -1160,22 +1092,8 @@ def rename(args)
11601092
# ldap.delete :dn => dn
11611093
def delete(args)
11621094
instrument "delete.net_ldap", args do |payload|
1163-
if @open_connection
1164-
@result = @open_connection.delete(args)
1165-
else
1166-
@result = 0
1167-
begin
1168-
conn = Connection.new \
1169-
:host => @host,
1170-
:port => @port,
1171-
:encryption => @encryption,
1172-
:instrumentation_service => @instrumentation_service
1173-
if (@result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
1174-
@result = conn.delete(args)
1175-
end
1176-
ensure
1177-
conn.close
1178-
end
1095+
@result = use_connection(args) do |conn|
1096+
conn.delete(args)
11791097
end
11801098
@result.success?
11811099
end
@@ -1277,4 +1195,36 @@ def paged_searches_supported?
12771195
@server_caps ||= search_root_dse
12781196
@server_caps[:supportedcontrol].include?(Net::LDAP::LDAPControls::PAGED_RESULTS)
12791197
end
1198+
1199+
private
1200+
1201+
# Yields an open connection if there is one, otherwise establishes a new
1202+
# connection, binds, and yields it. If binding fails, it will return the
1203+
# result from that, and :use_connection: will not yield at all. If not
1204+
# the return value is whatever is returned from the block.
1205+
def use_connection(args)
1206+
if @open_connection
1207+
yield @open_connection
1208+
else
1209+
begin
1210+
conn = new_connection
1211+
if (result = conn.bind(args[:auth] || @auth)).result_code == Net::LDAP::ResultCodeSuccess
1212+
yield conn
1213+
else
1214+
return result
1215+
end
1216+
ensure
1217+
conn.close if conn
1218+
end
1219+
end
1220+
end
1221+
1222+
# Establish a new connection to the LDAP server
1223+
def new_connection
1224+
Net::LDAP::Connection.new \
1225+
:host => @host,
1226+
:port => @port,
1227+
:encryption => @encryption,
1228+
:instrumentation_service => @instrumentation_service
1229+
end
12801230
end # class LDAP

0 commit comments

Comments
 (0)