lib/net/ldap.rb in net-ldap-0.16.3 vs lib/net/ldap.rb in net-ldap-0.17.0

- old
+ new

@@ -1180,18 +1180,26 @@ # Delete an entry from the LDAP directory along with all subordinate entries. # the regular delete method will fail to delete an entry if it has subordinate # entries. This method sends an extra control code to tell the LDAP server # to do a tree delete. ('1.2.840.113556.1.4.805') # + # If the LDAP server does not support the DELETE_TREE control code, subordinate + # entries are deleted recursively instead. + # # Returns True or False to indicate whether the delete succeeded. Extended # status information is available by calling #get_operation_result. # # dn = "mail=deleteme@example.com, ou=people, dc=example, dc=com" # ldap.delete_tree :dn => dn def delete_tree(args) - delete(args.merge(:control_codes => [[Net::LDAP::LDAPControls::DELETE_TREE, true]])) + if search_root_dse[:supportedcontrol].include? Net::LDAP::LDAPControls::DELETE_TREE + delete(args.merge(:control_codes => [[Net::LDAP::LDAPControls::DELETE_TREE, true]])) + else + recursive_delete(args) + end end + # This method is experimental and subject to change. Return the rootDSE # record from the LDAP server as a Net::LDAP::Entry, or an empty Entry if # the server doesn't return the record. #-- # cf. RFC4512 graf 5.1. @@ -1318,11 +1326,11 @@ :connect_timeout => @connect_timeout # Force connect to see if there's a connection error connection.socket connection - rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, Net::LDAP::ConnectionRefusedError => e + rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e @result = { :resultCode => 52, :errorMessage => ResultStrings[ResultCodeUnavailable], } raise e @@ -1336,8 +1344,23 @@ case method = args.to_sym when :simple_tls, :start_tls { :method => method, :tls_options => {} } end + end + + # Recursively delete a dn and it's subordinate children. + # This is useful when a server does not support the DELETE_TREE control code. + def recursive_delete(args) + raise EmptyDNError unless args.is_a?(Hash) && args.key?(:dn) + # Delete Children + search(base: args[:dn], scope: Net::LDAP::SearchScope_SingleLevel) do |entry| + recursive_delete(dn: entry.dn) + end + # Delete Self + unless delete(dn: args[:dn]) + raise Net::LDAP::Error, get_operation_result[:error_message].to_s + end + true end end # class LDAP