Added new_superior arg to Net::LDAP rename method. This enables moving an entry in the tree by specifying the new parent container in addition to the rename entry functionality. Added a unit test file test_rename.rb for this method.

This commit is contained in:
tonyheadford 2010-07-19 20:52:10 +01:00
parent cc55227083
commit 1761db89e9
2 changed files with 80 additions and 4 deletions

View file

@ -1507,16 +1507,19 @@ class Net::LDAP::Connection #:nodoc:
#--
# TODO: need to support a time limit, in case the server fails to respond.
#++
def rename(args)
def rename args
old_dn = args[:olddn] or raise "Unable to rename empty DN"
new_rdn = args[:newrdn] or raise "Unable to rename to empty RDN"
delete_attrs = args[:delete_attributes] ? true : false
new_superior = args[:new_superior]
request = [old_dn.to_ber, new_rdn.to_ber, delete_attrs.to_ber].to_ber_appsequence(12)
pkt = [next_msgid.to_ber, request].to_ber_sequence
request = [old_dn.to_ber, new_rdn.to_ber, delete_attrs.to_ber]
request << new_superior.to_ber unless new_superior == nil
pkt = [next_msgid.to_ber, request.to_ber_appsequence(12)].to_ber_sequence
@conn.write pkt
(be = @conn.read_ber(Net::LDAP::AsnSyntax)) && (pdu = Net::LdapPdu.new(be)) && (pdu.app_tag == 13) or raise Net::LDAP::LdapError, "response missing or invalid"
(be = @conn.read_ber(AsnSyntax)) && (pdu = LdapPdu.new( be )) && (pdu.app_tag == 13) or raise LdapError.new( "response missing or invalid" )
pdu.result_code
end

73
test/test_rename.rb Normal file
View file

@ -0,0 +1,73 @@
require 'common'
class TestRename < Test::Unit::TestCase
HOST= '10.10.10.71'
PORT = 389
BASE = "o=test"
AUTH = { :method => :simple, :username => "cn=testadmin,#{BASE}", :password => 'password' }
BASIC_USER = "cn=jsmith,ou=sales,#{BASE}"
RENAMED_USER = "cn=jbrown,ou=sales,#{BASE}"
MOVED_USER = "cn=jsmith,ou=marketing,#{BASE}"
RENAMED_MOVED_USER = "cn=jjones,ou=marketing,#{BASE}"
def setup
# create the entries we're going to manipulate
Net::LDAP::open(:host => HOST, :port => PORT, :auth => AUTH) do |ldap|
if ldap.add(:dn => "ou=sales,#{BASE}", :attributes => { :ou => "sales", :objectclass => "organizationalUnit" })
puts "Add failed: #{ldap.get_operation_result.message} - code: #{ldap.get_operation_result.code}"
end
ldap.add(:dn => "ou=marketing,#{BASE}", :attributes => { :ou => "marketing", :objectclass => "organizationalUnit" })
ldap.add(:dn => BASIC_USER, :attributes => { :cn => "jsmith", :objectclass => "inetOrgPerson", :sn => "Smith" })
end
end
def test_rename_entry
dn = nil
Net::LDAP::open(:host => HOST, :port => PORT, :auth => AUTH) do |ldap|
ldap.rename(:olddn => BASIC_USER, :newrdn => "cn=jbrown")
ldap.search(:base => RENAMED_USER) do |entry|
dn = entry.dn
end
end
assert_equal(RENAMED_USER, dn)
end
def test_move_entry
dn = nil
Net::LDAP::open(:host => HOST, :port => PORT, :auth => AUTH) do |ldap|
ldap.rename(:olddn => BASIC_USER, :newrdn => "cn=jsmith", :new_superior => "ou=marketing,#{BASE}")
ldap.search(:base => MOVED_USER) do |entry|
dn = entry.dn
end
end
assert_equal(MOVED_USER, dn)
end
def test_move_and_rename_entry
dn = nil
Net::LDAP::open(:host => HOST, :port => PORT, :auth => AUTH) do |ldap|
ldap.rename(:olddn => BASIC_USER, :newrdn => "cn=jjones", :new_superior => "ou=marketing,#{BASE}")
ldap.search(:base => RENAMED_MOVED_USER) do |entry|
dn = entry.dn
end
end
assert_equal(RENAMED_MOVED_USER, dn)
end
def teardown
# delete the entries
# note: this doesn't always completely clear up on eDirectory as objects get locked while
# the rename/move is being completed on the server and this prevents the delete from happening
Net::LDAP::open(:host => HOST, :port => PORT, :auth => AUTH) do |ldap|
ldap.delete(:dn => BASIC_USER)
ldap.delete(:dn => RENAMED_USER)
ldap.delete(:dn => MOVED_USER)
ldap.delete(:dn => RENAMED_MOVED_USER)
ldap.delete(:dn => "ou=sales,#{BASE}")
ldap.delete(:dn => "ou=marketing,#{BASE}")
end
end
end