Method: Net::LDAP#open

Defined in:
lib/net/ldap.rb

#openObject

Opens a network connection to the server and then passes self to the caller-supplied block. The connection is closed when the block completes. Used for executing multiple LDAP operations without requiring a separate network connection (and authentication) for each one. Note: You do not need to log-in or “bind” to the server. This will be done for you automatically. For an even simpler approach, see the class method Net::LDAP#open.

# (PSEUDOCODE)
auth = { :method => :simple, :username => username, :password => password }
ldap = Net::LDAP.new(:host => ipaddress, :port => 389, :auth => auth)
ldap.open do |ldap|
  ldap.search(...)
  ldap.add(...)
  ldap.modify(...)
end


707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/net/ldap.rb', line 707

def open
  # First we make a connection and then a binding, but we don't do
  # anything with the bind results. We then pass self to the caller's
  # block, where he will execute his LDAP operations. Of course they will
  # all generate auth failures if the bind was unsuccessful.
  raise Net::LDAP::AlreadyOpenedError, "Open already in progress" if @open_connection

  instrument "open.net_ldap" do |payload|
    begin
      @open_connection = new_connection
      payload[:connection] = @open_connection
      payload[:bind]       = @result = @open_connection.bind(@auth)
      yield self
    ensure
      @open_connection.close if @open_connection
      @open_connection = nil
    end
  end
end