Skip to content

Commit 387d6e6

Browse files
authoredAug 18, 2020
Merge pull request #268 from duffyjp/recursive_delete
Added private recursive_delete as alternative to DELETE_TREE
2 parents 3f316fb + 1051592 commit 387d6e6

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed
 

‎lib/net/ldap.rb

+24-1
Original file line numberDiff line numberDiff line change
@@ -1182,14 +1182,22 @@ def delete(args)
11821182
# entries. This method sends an extra control code to tell the LDAP server
11831183
# to do a tree delete. ('1.2.840.113556.1.4.805')
11841184
#
1185+
# If the LDAP server does not support the DELETE_TREE control code, subordinate
1186+
# entries are deleted recursively instead.
1187+
#
11851188
# Returns True or False to indicate whether the delete succeeded. Extended
11861189
# status information is available by calling #get_operation_result.
11871190
#
11881191
# dn = "mail=deleteme@example.com, ou=people, dc=example, dc=com"
11891192
# ldap.delete_tree :dn => dn
11901193
def delete_tree(args)
1191-
delete(args.merge(:control_codes => [[Net::LDAP::LDAPControls::DELETE_TREE, true]]))
1194+
if search_root_dse[:supportedcontrol].include? Net::LDAP::LDAPControls::DELETE_TREE
1195+
delete(args.merge(:control_codes => [[Net::LDAP::LDAPControls::DELETE_TREE, true]]))
1196+
else
1197+
recursive_delete(args)
1198+
end
11921199
end
1200+
11931201
# This method is experimental and subject to change. Return the rootDSE
11941202
# record from the LDAP server as a Net::LDAP::Entry, or an empty Entry if
11951203
# the server doesn't return the record.
@@ -1340,4 +1348,19 @@ def normalize_encryption(args)
13401348
end
13411349
end
13421350

1351+
# Recursively delete a dn and it's subordinate children.
1352+
# This is useful when a server does not support the DELETE_TREE control code.
1353+
def recursive_delete(args)
1354+
raise EmptyDNError unless args.is_a?(Hash) && args.has_key?(:dn)
1355+
# Delete Children
1356+
search(base: args[:dn], scope: Net::LDAP::SearchScope_SingleLevel) do |entry|
1357+
recursive_delete(dn: entry.dn)
1358+
end
1359+
# Delete Self
1360+
unless delete(dn: args[:dn])
1361+
raise Net::LDAP::Error, self.get_operation_result[:error_message].to_s
1362+
end
1363+
true
1364+
end
1365+
13431366
end # class LDAP

0 commit comments

Comments
 (0)
Please sign in to comment.