@@ -441,6 +441,10 @@ def self.result2string(code) #:nodoc:
441
441
# described below. The following arguments are supported:
442
442
# * :host => the LDAP server's IP-address (default 127.0.0.1)
443
443
# * :port => the LDAP server's TCP port (default 389)
444
+ # * :connect_cb => a Proc that will be called when a new connection is
445
+ # needed. This should return an actual Ruby IO object. Useful for
446
+ # manually handling connecting, like if you want to go through a proxy
447
+ # server. It will receive :host: and :port: as arguments.
444
448
# * :auth => a Hash containing authorization parameters. Currently
445
449
# supported values include: {:method => :anonymous} and {:method =>
446
450
# :simple, :username => your_user_name, :password => your_password }
@@ -469,6 +473,7 @@ def self.result2string(code) #:nodoc:
469
473
def initialize ( args = { } )
470
474
@host = args [ :host ] || DefaultHost
471
475
@port = args [ :port ] || DefaultPort
476
+ @connect_cb = args [ :connect_cb ]
472
477
@verbose = false # Make this configurable with a switch on the class.
473
478
@auth = args [ :auth ] || DefaultAuth
474
479
@base = args [ :base ] || DefaultTreebase
@@ -670,12 +675,7 @@ def open
670
675
671
676
instrument "open.net_ldap" do |payload |
672
677
begin
673
- @open_connection =
674
- Net ::LDAP ::Connection . new \
675
- :host => @host ,
676
- :port => @port ,
677
- :encryption => @encryption ,
678
- :instrumentation_service => @instrumentation_service
678
+ @open_connection = new_connection
679
679
payload [ :connection ] = @open_connection
680
680
payload [ :bind ] = @open_connection . bind ( @auth )
681
681
yield self
@@ -745,27 +745,11 @@ def search(args = {})
745
745
result_set = return_result_set ? [ ] : nil
746
746
747
747
instrument "search.net_ldap" , args do |payload |
748
- if @open_connection
749
- @result = @open_connection . search ( args ) { |entry |
748
+ use_connection ( args [ :auth ] ) do | conn |
749
+ @result = conn . search ( args ) { |entry |
750
750
result_set << entry if result_set
751
751
yield entry if block_given?
752
752
}
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
753
end
770
754
771
755
if return_result_set
@@ -844,11 +828,7 @@ def bind(auth = @auth)
844
828
payload [ :bind ] = @result = @open_connection . bind ( auth )
845
829
else
846
830
begin
847
- conn = Connection . new \
848
- :host => @host ,
849
- :port => @port ,
850
- :encryption => @encryption ,
851
- :instrumentation_service => @instrumentation_service
831
+ conn = new_connection
852
832
payload [ :connection ] = conn
853
833
payload [ :bind ] = @result = conn . bind ( auth )
854
834
ensure
@@ -946,22 +926,8 @@ def bind_as(args = {})
946
926
# end
947
927
def add ( args )
948
928
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
929
+ use_connection ( args [ :auth ] ) do |conn |
930
+ @result = conn . add ( args )
965
931
end
966
932
@result . success?
967
933
end
@@ -1050,24 +1016,9 @@ def add(args)
1050
1016
# does _not_ imply transactional atomicity, which LDAP does not provide.
1051
1017
def modify ( args )
1052
1018
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
1019
+ use_connection ( args [ :auth ] ) do |conn |
1020
+ @result = conn . modify ( args )
1069
1021
end
1070
-
1071
1022
@result . success?
1072
1023
end
1073
1024
end
@@ -1127,22 +1078,8 @@ def delete_attribute(dn, attribute)
1127
1078
# _Documentation_ _stub_
1128
1079
def rename ( args )
1129
1080
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
1081
+ use_connection ( args [ :auth ] ) do |conn |
1082
+ @result = conn . rename ( args )
1146
1083
end
1147
1084
@result . success?
1148
1085
end
@@ -1160,22 +1097,8 @@ def rename(args)
1160
1097
# ldap.delete :dn => dn
1161
1098
def delete ( args )
1162
1099
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
1100
+ use_connection ( args [ :auth ] ) do |conn |
1101
+ @result = conn . delete ( args )
1179
1102
end
1180
1103
@result . success?
1181
1104
end
@@ -1277,4 +1200,32 @@ def paged_searches_supported?
1277
1200
@server_caps ||= search_root_dse
1278
1201
@server_caps [ :supportedcontrol ] . include? ( Net ::LDAP ::LDAPControls ::PAGED_RESULTS )
1279
1202
end
1203
+
1204
+ private
1205
+
1206
+ def use_connection ( auth )
1207
+ if @open_connection
1208
+ yield @open_connection
1209
+ else
1210
+ @result = 0
1211
+ begin
1212
+ conn = new_connection
1213
+ if ( @result = conn . bind ( auth || @auth ) ) . result_code == Net ::LDAP ::ResultCodeSuccess
1214
+ yield conn
1215
+ end
1216
+ ensure
1217
+ conn . close if conn
1218
+ end
1219
+ end
1220
+ end
1221
+
1222
+ def new_connection
1223
+ socket = @connect_cb . call ( @host , @port ) if @connect_cb
1224
+ Net ::LDAP ::Connection . new \
1225
+ :socket => socket ,
1226
+ :host => @host ,
1227
+ :port => @port ,
1228
+ :encryption => @encryption ,
1229
+ :instrumentation_service => @instrumentation_service
1230
+ end
1280
1231
end # class LDAP
0 commit comments