Method: Net::LDAP#open
- Defined in:
- lib/net/ldap.rb
#open {|_self| ... } ⇒ Object
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
– 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.
540 541 542 543 544 545 546 547 |
# File 'lib/net/ldap.rb', line 540 def open raise LdapError.new( "open already in progress" ) if @open_connection @open_connection = Connection.new( :host => @host, :port => @port, :encryption => @encryption ) @open_connection.bind @auth yield self @open_connection.close @open_connection = nil end |