@@ -670,12 +670,7 @@ def open
670
670
671
671
instrument "open.net_ldap" do |payload |
672
672
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
679
674
payload [ :connection ] = @open_connection
680
675
payload [ :bind ] = @open_connection . bind ( @auth )
681
676
yield self
@@ -745,27 +740,11 @@ def search(args = {})
745
740
result_set = return_result_set ? [ ] : nil
746
741
747
742
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 |
750
745
result_set << entry if result_set
751
746
yield entry if block_given?
752
747
}
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
769
748
end
770
749
771
750
if return_result_set
@@ -844,11 +823,7 @@ def bind(auth = @auth)
844
823
payload [ :bind ] = @result = @open_connection . bind ( auth )
845
824
else
846
825
begin
847
- conn = Connection . new \
848
- :host => @host ,
849
- :port => @port ,
850
- :encryption => @encryption ,
851
- :instrumentation_service => @instrumentation_service
826
+ conn = new_connection
852
827
payload [ :connection ] = conn
853
828
payload [ :bind ] = @result = conn . bind ( auth )
854
829
ensure
@@ -946,22 +921,8 @@ def bind_as(args = {})
946
921
# end
947
922
def add ( args )
948
923
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 )
965
926
end
966
927
@result . success?
967
928
end
@@ -1050,24 +1011,9 @@ def add(args)
1050
1011
# does _not_ imply transactional atomicity, which LDAP does not provide.
1051
1012
def modify ( args )
1052
1013
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 )
1069
1016
end
1070
-
1071
1017
@result . success?
1072
1018
end
1073
1019
end
@@ -1127,22 +1073,8 @@ def delete_attribute(dn, attribute)
1127
1073
# _Documentation_ _stub_
1128
1074
def rename ( args )
1129
1075
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 )
1146
1078
end
1147
1079
@result . success?
1148
1080
end
@@ -1160,22 +1092,8 @@ def rename(args)
1160
1092
# ldap.delete :dn => dn
1161
1093
def delete ( args )
1162
1094
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 )
1179
1097
end
1180
1098
@result . success?
1181
1099
end
@@ -1277,4 +1195,36 @@ def paged_searches_supported?
1277
1195
@server_caps ||= search_root_dse
1278
1196
@server_caps [ :supportedcontrol ] . include? ( Net ::LDAP ::LDAPControls ::PAGED_RESULTS )
1279
1197
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
1280
1230
end # class LDAP
0 commit comments