Skip to content

Added private recursive_delete as alternative to DELETE_TREE #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/net/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1170,14 +1170,22 @@ def delete(args)
# 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 = "[email protected], 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.
Expand Down Expand Up @@ -1330,4 +1338,19 @@ def normalize_encryption(args)
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.has_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, self.get_operation_result[:error_message].to_s
end
true
end

end # class LDAP