@@ -1182,14 +1182,22 @@ def delete(args)
1182
1182
# entries. This method sends an extra control code to tell the LDAP server
1183
1183
# to do a tree delete. ('1.2.840.113556.1.4.805')
1184
1184
#
1185
+ # If the LDAP server does not support the DELETE_TREE control code, subordinate
1186
+ # entries are deleted recursively instead.
1187
+ #
1185
1188
# Returns True or False to indicate whether the delete succeeded. Extended
1186
1189
# status information is available by calling #get_operation_result.
1187
1190
#
1188
1191
# dn = "mail=deleteme@example.com, ou=people, dc=example, dc=com"
1189
1192
# ldap.delete_tree :dn => dn
1190
1193
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
1192
1199
end
1200
+
1193
1201
# This method is experimental and subject to change. Return the rootDSE
1194
1202
# record from the LDAP server as a Net::LDAP::Entry, or an empty Entry if
1195
1203
# the server doesn't return the record.
@@ -1340,4 +1348,19 @@ def normalize_encryption(args)
1340
1348
end
1341
1349
end
1342
1350
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
+
1343
1366
end # class LDAP
0 commit comments