Skip to content

Commit 463ac43

Browse files
author
Chris Dwan
committed
added the ability to do a delete_tree
1 parent 6bb9fa6 commit 463ac43

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source :rubygems
2+
gemspec

Gemfile.lock

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
PATH
2+
remote: .
3+
specs:
4+
net-ldap (0.2.20110317223538)
5+
6+
GEM
7+
remote: http://rubygems.org/
8+
specs:
9+
diff-lcs (1.1.3)
10+
flexmock (0.9.0)
11+
hoe (2.12.3)
12+
rake (~> 0.8)
13+
hoe-gemspec (1.0.0)
14+
hoe (>= 2.2.0)
15+
hoe-git (1.4.1)
16+
hoe (>= 2.2.0)
17+
metaid (1.0)
18+
rake (0.9.2.2)
19+
rspec (2.7.0)
20+
rspec-core (~> 2.7.0)
21+
rspec-expectations (~> 2.7.0)
22+
rspec-mocks (~> 2.7.0)
23+
rspec-core (2.7.1)
24+
rspec-expectations (2.7.0)
25+
diff-lcs (~> 1.1.2)
26+
rspec-mocks (2.7.0)
27+
28+
PLATFORMS
29+
ruby
30+
31+
DEPENDENCIES
32+
flexmock (~> 0.9.0)
33+
hoe (>= 2.9.1)
34+
hoe-gemspec (~> 1)
35+
hoe-git (~> 1)
36+
metaid (~> 1)
37+
net-ldap!
38+
rspec (~> 2.0)

lib/net/ber/core_ext/array.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ def to_ber_oid
7979
oid = ary.pack("w*")
8080
[6, oid.length].pack("CC") + oid
8181
end
82+
83+
##
84+
# Converts an array into a set of ber control codes
85+
# The expected format is [[control_oid, criticality, control_value(optional)]]
86+
# [['1.2.840.113556.1.4.805',true]]
87+
#
88+
def to_ber_control
89+
ary = self.collect do |control_sequence|
90+
control_sequence.collect{|element| element.to_ber}.to_ber_sequence
91+
end
92+
ary.to_ber_sequence #putting this on a new line to make it more readable.
93+
end
8294
end

lib/net/ldap.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,19 @@ def delete(args)
10221022
@result == 0
10231023
end
10241024

1025+
# Delete an entry from the LDAP directory along with all subordinate entries.
1026+
# the regular delete method will fail to delete an entry if it has subordinate
1027+
# entries. This method sends an extra control code to tell the LDAP server
1028+
# to do a tree delete. ('1.2.840.113556.1.4.805')
1029+
#
1030+
# Returns True or False to indicate whether the delete succeeded. Extended
1031+
# status information is available by calling #get_operation_result.
1032+
#
1033+
# dn = "[email protected], ou=people, dc=example, dc=com"
1034+
# ldap.delete_tree :dn => dn
1035+
def delete_tree(args)
1036+
delete(args.merge(:control_codes => [['1.2.840.113556.1.4.805',true]]))
1037+
end
10251038
# This method is experimental and subject to change. Return the rootDSE
10261039
# record from the LDAP server as a Net::LDAP::Entry, or an empty Entry if
10271040
# the server doesn't return the record.
@@ -1545,9 +1558,9 @@ def rename args
15451558
#++
15461559
def delete(args)
15471560
dn = args[:dn] or raise "Unable to delete empty DN"
1548-
1561+
controls = args.include?(:control_codes) ? args[:control_codes].to_ber_control : nil #use nil so we can compact later
15491562
request = dn.to_s.to_ber_application_string(10)
1550-
pkt = [next_msgid.to_ber, request].to_ber_sequence
1563+
pkt = [next_msgid.to_ber, request, controls].compact.to_ber_sequence
15511564
@conn.write pkt
15521565

15531566
(be = @conn.read_ber(Net::LDAP::AsnSyntax)) && (pdu = Net::LDAP::PDU.new(be)) && (pdu.app_tag == 11) or raise Net::LDAP::LdapError, "response missing or invalid"

spec/unit/ber/core_ext/array_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
require 'metaid'
3+
4+
describe Array, "when extended with BER core extensions" do
5+
6+
it "should correctly convert a control code array" do
7+
control_codes = []
8+
control_codes << ['1.2.3'.to_ber, true.to_ber].to_ber_sequence
9+
control_codes << ['1.7.9'.to_ber, false.to_ber].to_ber_sequence
10+
control_codes = control_codes.to_ber_sequence
11+
res = [['1.2.3', true],['1.7.9',false]].to_ber_control
12+
res.should eq(control_codes)
13+
end
14+
end

0 commit comments

Comments
 (0)