@@ -1170,14 +1170,22 @@ def delete(args)
11701170 # entries. This method sends an extra control code to tell the LDAP server
11711171 # to do a tree delete. ('1.2.840.113556.1.4.805')
11721172 #
1173+ # If the LDAP server does not support the DELETE_TREE control code, subordinate
1174+ # entries are deleted recursively instead.
1175+ #
11731176 # Returns True or False to indicate whether the delete succeeded. Extended
11741177 # status information is available by calling #get_operation_result.
11751178 #
11761179 # dn = "[email protected] , ou=people, dc=example, dc=com" 11771180 # ldap.delete_tree :dn => dn
11781181 def delete_tree ( args )
1179- delete ( args . merge ( :control_codes => [ [ Net ::LDAP ::LDAPControls ::DELETE_TREE , true ] ] ) )
1182+ if search_root_dse [ :supportedcontrol ] . include? Net ::LDAP ::LDAPControls ::DELETE_TREE
1183+ delete ( args . merge ( :control_codes => [ [ Net ::LDAP ::LDAPControls ::DELETE_TREE , true ] ] ) )
1184+ else
1185+ recursive_delete ( args )
1186+ end
11801187 end
1188+
11811189 # This method is experimental and subject to change. Return the rootDSE
11821190 # record from the LDAP server as a Net::LDAP::Entry, or an empty Entry if
11831191 # the server doesn't return the record.
@@ -1330,4 +1338,19 @@ def normalize_encryption(args)
13301338 end
13311339 end
13321340
1341+ # Recursively delete a dn and it's subordinate children.
1342+ # This is useful when a server does not support the DELETE_TREE control code.
1343+ def recursive_delete ( args )
1344+ raise EmptyDNError unless args . is_a? ( Hash ) && args . has_key? ( :dn )
1345+ # Delete Children
1346+ search ( base : args [ :dn ] , scope : Net ::LDAP ::SearchScope_SingleLevel ) do |entry |
1347+ recursive_delete ( dn : entry . dn )
1348+ end
1349+ # Delete Self
1350+ unless delete ( dn : args [ :dn ] )
1351+ raise Net ::LDAP ::Error , self . get_operation_result [ :error_message ] . to_s
1352+ end
1353+ true
1354+ end
1355+
13331356end # class LDAP
0 commit comments