Permalink
Browse files

extend Net::LDAP::Entry->update() to take LDIF objects

Extend Net::LDAP::Entry.pm's update() method so that it can take
Net::LDAP::LDIF objects as handles too, and append the entry's
data to the LDIF.

For LDIF arguments also return a Net::LDAP::Message argument:
on success return LDAP_SUCCESS, on error return LDAP_OTHER.

One might argue that tis is against the standards, but
Net::LDAP::Entry->update() is not covered in any standard.

The big plus is, it allows clients to have one common code path for
directory servers and LDIF files (except for startup/shutdown)
which makes using LDIF as "dry-run" output for servers really simple.

Especially for the result value, it s a lot more convenient than any other
combination of Net::LDAP::entry->update() and Net::LDAP::Entry->ldif().

Signed-off-by: Peter Marschall <peter@adpm.de>
  • Loading branch information...
1 parent 6c81ac3 commit 0a55dd15710d6d853adbe763cf17abd0e0253a55 @marschap marschap committed with gbarr Jan 9, 2011
Showing with 31 additions and 23 deletions.
  1. +29 −21 lib/Net/LDAP/Entry.pm
  2. +2 −2 lib/Net/LDAP/Message.pm
View
@@ -6,7 +6,7 @@ package Net::LDAP::Entry;
use strict;
use Net::LDAP::ASN qw(LDAPEntry);
-use Net::LDAP::Constant qw(LDAP_LOCAL_ERROR);
+use Net::LDAP::Constant qw(LDAP_LOCAL_ERROR LDAP_OTHER);
use vars qw($VERSION);
use constant CHECK_UTF8 => $] > 5.007;
@@ -258,33 +258,41 @@ sub delete {
sub update {
my $self = shift;
- my $ldap = shift;
+ my $target = shift; # a Net::LDAP or a Net::LDAP::LDIF object
my %opt = @_;
my $mesg;
my $user_cb = delete $opt{callback};
my $cb = sub { $self->changetype('modify') unless $_[0]->code;
$user_cb->(@_) if $user_cb };
- if ($self->{'changetype'} eq 'add') {
- $mesg = $ldap->add($self, 'callback' => $cb, %opt);
- }
- elsif ($self->{'changetype'} eq 'delete') {
- $mesg = $ldap->delete($self, 'callback' => $cb, %opt);
- }
- elsif ($self->{'changetype'} =~ /modr?dn/o) {
- my @args = (newrdn => $self->get_value('newrdn') || undef,
- deleteoldrdn => $self->get_value('deleteoldrdn') || undef);
- my $newsuperior = $self->get_value('newsuperior');
- push(@args, newsuperior => $newsuperior) if $newsuperior;
- $mesg = $ldap->moddn($self, @args, 'callback' => $cb, %opt);
- }
- elsif (@{$self->{'changes'}}) {
- $mesg = $ldap->modify($self, 'changes' => $self->{'changes'}, 'callback' => $cb, %opt);
+ if (ref($target) && UNIVERSAL::isa($target, 'Net::LDAP')) {
+ if ($self->{'changetype'} eq 'add') {
+ $mesg = $target->add($self, 'callback' => $cb, %opt);
+ }
+ elsif ($self->{'changetype'} eq 'delete') {
+ $mesg = $target->delete($self, 'callback' => $cb, %opt);
+ }
+ elsif ($self->{'changetype'} =~ /modr?dn/o) {
+ my @args = (newrdn => $self->get_value('newrdn') || undef,
+ deleteoldrdn => $self->get_value('deleteoldrdn') || undef);
+ my $newsuperior = $self->get_value('newsuperior');
+ push(@args, newsuperior => $newsuperior) if $newsuperior;
+ $mesg = $target->moddn($self, @args, 'callback' => $cb, %opt);
+ }
+ elsif (@{$self->{'changes'}}) {
+ $mesg = $target->modify($self, 'changes' => $self->{'changes'}, 'callback' => $cb, %opt);
+ }
+ else {
+ require Net::LDAP::Message;
+ $mesg = Net::LDAP::Message->new( $target );
+ $mesg->set_error(LDAP_LOCAL_ERROR,"No attributes to update");
+ }
}
- else {
- require Net::LDAP::Message;
- $mesg = Net::LDAP::Message->new( $ldap );
- $mesg->set_error(LDAP_LOCAL_ERROR,"No attributes to update");
+ elsif (ref($target) && UNIVERSAL::isa($target, 'Net::LDAP::LDIF')) {
+ $target->write_entry($self);
+ $mesg = Net::LDAP::Message::Dummy->new();
+ $mesg->set_error(LDAP_OTHER, $target->error())
+ if ($target->error());
}
return $mesg;
View
@@ -254,8 +254,8 @@ sub Net::LDAP::Compare::is_error {
sub sync { shift }
sub decode { shift }
sub abandon { shift }
- sub code { 0 }
- sub error { "" }
+ sub code { $self->{resultcode} || LDAP_SUCCESS }
+ sub error { $self->{errorMessage} || "" }
sub dn { "" }
sub done { 1 }
}

0 comments on commit 0a55dd1

Please sign in to comment.