Skip to content
Browse files

Add ldif method to Net::LDAP::Entry

  • Loading branch information...
1 parent 7b4f3c7 commit 895e0a1b18772597ecfed6e8183c7b68921e9fe8 @gbarr gbarr committed Mar 11, 2010
Showing with 137 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 Makefile.PL
  3. +11 −0 lib/Net/LDAP/Entry.pm
  4. +15 −0 lib/Net/LDAP/Entry.pod
  5. +109 −1 t/00ldif-entry.t
View
1 .gitignore
@@ -3,3 +3,4 @@ Makefile
_test
temp
*.bak
+.*.swp
View
2 Makefile.PL
@@ -2,7 +2,7 @@
use inc::Module::Install;
-perl_version 5.004;
+perl_version 5.008;
name ('perl-ldap');
module_name ('Net::LDAP');
View
11 lib/Net/LDAP/Entry.pm
@@ -290,6 +290,17 @@ sub update {
return $mesg;
}
+sub ldif {
+ my $self = shift;
+ my %opt = @_;
+
+ require Net::LDAP::LDIF;
+ open(my $fh, ">", \my $buffer);
+ my $change = exists $opt{change} ? $opt{change} : $self->changes ? 1 : 0;
+ my $ldif = Net::LDAP::LDIF->new($fh, "w", change => $change);
+ $ldif->write_entry($self);
+ return $buffer;
+}
# Just for debugging
View
15 lib/Net/LDAP/Entry.pod
@@ -217,6 +217,21 @@ Set the DN for the entry, and return the previous value.
B<NOTE>: these changes are local to the client and will not appear on
the directory server until the C<update> method is called.
+=item ldif ( OPTION =E<gt> VALUE, ... )
+
+Returns the entry as an LDIF string. possible options
+
+=over
+
+=item change =E<gt> VALUE
+
+If given a true value then the LDIF will be generated as a change record.
+If flase, then the LDIF generated will represent the entry content. If
+unspecified then it will fefault to true if the entry has changes and
+false if no changes have been applied to the entry.
+
+=back
+
=item dump ( [ FILEHANDLE ] )
Dump the entry to the given filehandle.
View
110 t/00ldif-entry.t
@@ -5,7 +5,7 @@ BEGIN {
}
-print "1..11\n";
+print "1..15\n";
use Net::LDAP::LDIF;
@@ -30,13 +30,121 @@ ok(!compare($cmpfile2,$outfile2), $cmpfile2);
$e = $entry[0];
+is($e->ldif, <<'LDIF', "ldif method");
+
+dn: o=University of Michigan, c=US
+objectclass: top
+objectclass: organization
+objectclass: domainRelatedObject
+objectclass: quipuObject
+objectclass: quipuNonLeafObject
+l: Ann Arbor, Michigan
+st: Michigan
+streetaddress: 535 West William St.
+o: University of Michigan
+o: UMICH
+o: UM
+o: U-M
+o: U of M
+description: The University of Michigan at Ann Arbor
+postaladdress: University of Michigan $ 535 W. William St. $ Ann Arbor, MI 481
+ 09 $ USpostalcode: 48109
+telephonenumber: +1 313 764-1817
+lastmodifiedtime: 930106182800Z
+lastmodifiedby: cn=manager, o=university of michigan, c=US
+associateddomain: umich.edu
+LDIF
+
+is($e->ldif(change => 1), <<'LDIF', "ldif method");
+
+dn: o=University of Michigan, c=US
+changetype: add
+objectclass: top
+objectclass: organization
+objectclass: domainRelatedObject
+objectclass: quipuObject
+objectclass: quipuNonLeafObject
+l: Ann Arbor, Michigan
+st: Michigan
+streetaddress: 535 West William St.
+o: University of Michigan
+o: UMICH
+o: UM
+o: U-M
+o: U of M
+description: The University of Michigan at Ann Arbor
+postaladdress: University of Michigan $ 535 W. William St. $ Ann Arbor, MI 481
+ 09 $ USpostalcode: 48109
+telephonenumber: +1 313 764-1817
+lastmodifiedtime: 930106182800Z
+lastmodifiedby: cn=manager, o=university of michigan, c=US
+associateddomain: umich.edu
+LDIF
+
+
$e->changetype('modify');
$e->delete('objectclass');
$e->delete('o',['UM']);
$e->add('counting',[qw(one two three)]);
$e->add('first',[qw(1 2 3)], 'second',[qw(a b c)]);
$e->replace('telephonenumber' => ['911']);
+is($e->ldif, <<'LDIF',"changes ldif");
+
+dn: o=University of Michigan, c=US
+changetype: modify
+delete: objectclass
+-
+delete: o
+o: UM
+-
+add: counting
+counting: one
+counting: two
+counting: three
+-
+add: first
+first: 1
+first: 2
+first: 3
+-
+add: second
+second: a
+second: b
+second: c
+-
+replace: telephonenumber
+telephonenumber: 911
+LDIF
+
+is($e->ldif(change => 0), <<'LDIF',"changes ldif");
+
+dn: o=University of Michigan, c=US
+l: Ann Arbor, Michigan
+st: Michigan
+streetaddress: 535 West William St.
+o: University of Michigan
+o: UMICH
+o: U-M
+o: U of M
+description: The University of Michigan at Ann Arbor
+postaladdress: University of Michigan $ 535 W. William St. $ Ann Arbor, MI 481
+ 09 $ USpostalcode: 48109
+telephonenumber: 911
+lastmodifiedtime: 930106182800Z
+lastmodifiedby: cn=manager, o=university of michigan, c=US
+associateddomain: umich.edu
+counting: one
+counting: two
+counting: three
+first: 1
+first: 2
+first: 3
+second: a
+second: b
+second: c
+LDIF
+
$outfile = "$TEMPDIR/00-out3.ldif";
$cmpfile = "data/00-cmp2.ldif";

0 comments on commit 895e0a1

Please sign in to comment.
Something went wrong with that request. Please try again.