@@ -1170,14 +1170,22 @@ def delete(args)
1170
1170
# entries. This method sends an extra control code to tell the LDAP server
1171
1171
# to do a tree delete. ('1.2.840.113556.1.4.805')
1172
1172
#
1173
+ # If the LDAP server does not support the DELETE_TREE control code, subordinate
1174
+ # entries are deleted recursively instead.
1175
+ #
1173
1176
# Returns True or False to indicate whether the delete succeeded. Extended
1174
1177
# status information is available by calling #get_operation_result.
1175
1178
#
1176
1179
# dn = "mail=deleteme@example.com, ou=people, dc=example, dc=com"
1177
1180
# ldap.delete_tree :dn => dn
1178
1181
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
1180
1187
end
1188
+
1181
1189
# This method is experimental and subject to change. Return the rootDSE
1182
1190
# record from the LDAP server as a Net::LDAP::Entry, or an empty Entry if
1183
1191
# the server doesn't return the record.
@@ -1330,4 +1338,19 @@ def normalize_encryption(args)
1330
1338
end
1331
1339
end
1332
1340
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
+
1333
1356
end # class LDAP
0 commit comments