Skip to content

Commit

Permalink
Add ldif method to Net::LDAP::Entry
Browse files Browse the repository at this point in the history
  • Loading branch information
gbarr committed Mar 12, 2010
1 parent 7b4f3c7 commit 895e0a1
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ Makefile
_test
temp
*.bak
.*.swp
2 changes: 1 addition & 1 deletion Makefile.PL
Expand Up @@ -2,7 +2,7 @@

use inc::Module::Install;

perl_version 5.004;
perl_version 5.008;

name ('perl-ldap');
module_name ('Net::LDAP');
Expand Down
11 changes: 11 additions & 0 deletions lib/Net/LDAP/Entry.pm
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions lib/Net/LDAP/Entry.pod
Expand Up @@ -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.
Expand Down
110 changes: 109 additions & 1 deletion t/00ldif-entry.t
Expand Up @@ -5,7 +5,7 @@ BEGIN {
}


print "1..11\n";
print "1..15\n";

use Net::LDAP::LDIF;

Expand All @@ -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";

Expand Down

0 comments on commit 895e0a1

Please sign in to comment.