Refactored the Net::LDAP methods to prepare for

the implementation of Net::LDAP::open
This commit is contained in:
blackhedd 2006-04-18 20:55:55 +00:00
parent 7a0da7c9bc
commit 36526cd66a
3 changed files with 114 additions and 25 deletions

View file

@ -136,30 +136,79 @@ module Net
# #
# open # open
# #
def LDAP::open def LDAP::open args
#ldap = LDAP.new args
#ldap.connect
#yield ldap
#ldap.disconnect
end end
# This method opens a network connection to the server and then
# passes self to the caller-supplied block. The connection is
# closed when the block completes. It's for executing multiple
# LDAP operations without requiring a separate network connection
# (and authentication) for each one.
#
#
def open
conn = connect
yield self
disconnect
end
# #
# search # search
#--
# If an open call is in progress (@open_connection will be non-nil),
# then ASSUME a bind has been performed and accepted, and just
# execute the search.
# If @open_connection is nil, then we have to connect, bind,
# search, and then disconnect. (The disconnect is not strictly
# necessary but it's friendlier to the network to do it here
# rather than waiting for Ruby's GC.)
# Note that in the standalone case, we're permitting the caller
# to modify the auth parms.
# #
def search args def search args
conn = Connection.new( :host => @host, :port => @port ) if @open_connection
# TODO, hardcoded Ldap result code in next line result_code = @open_connection.search( args ) {|values|
(rc = conn.bind @auth) == 0 or return rc block_given? and yield( values )
result_code = conn.search( args ) {|values| }
block_given? and yield( values ) result_code
} else
result_code result_code = 0
conn = Connection.new( :host => @host, :port => @port )
if (result_code = conn.bind( args[:auth] || @auth )) == 0
result_code = conn.search( args ) {|values|
block_given? and yield( values )
}
end
conn.close
result_code
end
end end
# #
# bind # bind
# Bind and unbind. # Bind and unbind.
# Can serve as a connectivity test as well as an auth test. # Can serve as a connectivity test as well as an auth test.
#--
# If there is an @open_connection, then perform the bind
# on it. Otherwise, connect, bind, and disconnect.
# The latter operation is obviously useful only as an auth check.
# #
def bind def bind
conn = Connection.new( :host => @host, :port => @port ) if @open_connection
conn.bind @auth @open_connection.bind @auth
else
conn = Connection.new( :host => @host, :port => @port )
result = conn.bind @auth
conn.close
result
end
end end
# #
@ -180,10 +229,17 @@ module Net
# Add a full RDN to the remote DIS. # Add a full RDN to the remote DIS.
# #
def add args def add args
conn = Connection.new( :host => @host, :port => @port ) if @open_connection
# TODO, hardcoded Ldap result code in next line @open_connection.add( args )
(rc = conn.bind @auth) == 0 or return rc else
conn.add( args ) result_code = 0
conn = Connection.new( :host => @host, :port => @port )
if (result_code = conn.bind( args[:auth] || @auth )) == 0
result_code = conn.add( args )
end
conn.close
result_code
end
end end
@ -192,10 +248,17 @@ module Net
# Modify the attributes of an entry on the remote DIS. # Modify the attributes of an entry on the remote DIS.
# #
def modify args def modify args
conn = Connection.new( :host => @host, :port => @port ) if @open_connection
# TODO, hardcoded Ldap result code in next line @open_connection.modify( args )
(rc = conn.bind @auth) == 0 or return rc else
conn.modify( args ) result_code = 0
conn = Connection.new( :host => @host, :port => @port )
if (result_code = conn.bind( args[:auth] || @auth )) == 0
result_code = conn.modify( args )
end
conn.close
result_code
end
end end
# #
@ -203,10 +266,17 @@ module Net
# Rename an entry on the remote DIS by changing the last RDN of its DN. # Rename an entry on the remote DIS by changing the last RDN of its DN.
# #
def rename args def rename args
conn = Connection.new( :host => @host, :port => @port ) if @open_connection
# TODO, hardcoded Ldap result code in next line @open_connection.rename( args )
(rc = conn.bind @auth) == 0 or return rc else
conn.rename( args ) result_code = 0
conn = Connection.new( :host => @host, :port => @port )
if (result_code = conn.bind( args[:auth] || @auth )) == 0
result_code = conn.rename( args )
end
conn.close
result_code
end
end end
end # class LDAP end # class LDAP
@ -232,6 +302,18 @@ module Net
block_given? and yield self block_given? and yield self
end end
#
# close
# This is provided as a convenience method to make
# sure a connection object gets closed without waiting
# for a GC to happen. Clients shouldn't have to call it,
# but perhaps it will come in handy someday.
def close
@conn.close
@conn = nil
end
# #
# next_msgid # next_msgid
# #

View file

@ -5,6 +5,8 @@
$:.unshift "lib" $:.unshift "lib"
require 'test/unit'
require 'net/ldap' require 'net/ldap'
require 'stringio' require 'stringio'
@ -151,6 +153,14 @@ class TestLdapClient < Test::Unit::TestCase
def test_open
Net::LDAP.open( :host => @host, :port => @port, :auth => @auth ) {
p "NO TESTS!!!"
}
end
end end

View file

@ -6,9 +6,6 @@
$:.unshift "lib" $:.unshift "lib"
require 'test/unit' require 'test/unit'
require 'tests/testber'
require 'tests/testldif'
require 'tests/testldap'
require 'net/ldap' require 'net/ldap'
require 'net/ldif' require 'net/ldif'