File Coverage

blib/lib/Net/LDAP/Batch/Action/Delete.pm
Criterion Covered Total %
statement 31 46 67.3
branch 6 20 30.0
condition 1 3 33.3
subroutine 6 7 85.7
pod 3 3 100.0
total 47 79 59.4


line stmt bran cond sub pod time code
1             package Net::LDAP::Batch::Action::Delete;
2 3     3   24 use strict;
  3         7  
  3         146  
3 3     3   19 use warnings;
  3         9  
  3         106  
4 3     3   20 use Carp;
  3         7  
  3         304  
5 3     3   20 use base qw( Net::LDAP::Batch::Action );
  3         8  
  3         2317  
6              
7             our $VERSION = '0.02';
8              
9             __PACKAGE__->mk_accessors(qw( search ));
10              
11             =head1 NAME
12              
13             Net::LDAP::Batch::Action::Delete - delete entry from LDAP server
14              
15             =head1 SYNOPSIS
16              
17             use Net::LDAP::Batch::Action::Delete;
18             my $action = Net::LDAP::Batch::Action::Delete->new(
19             {
20             ldap => $net_ldap_object,
21             search => [
22             base => 'name=foo,dc=company,dc=com'
23             scope => 'base'
24             ],
25             });
26             $action->execute or $action->rollback;
27            
28              
29             =head1 DESCRIPTION
30              
31             Deletes an entry from a LDAP server, restoring it on failure of any kind.
32              
33             =head1 METHODS
34              
35             =head2 init
36              
37             Override base method to check that search() param is set to an array ref.
38              
39             =cut
40              
41             sub init {
42 1     1 1 2 my $self = shift;
43 1         7 $self->SUPER::init(@_);
44 1 50 33     5 if ( $self->search
45             and ref( $self->search ) ne 'ARRAY' )
46             {
47 0         0 croak "search must be an ARRAY ref";
48             }
49 1         25 return $self;
50             }
51              
52             =head2 execute
53              
54             Perform the action. Will croak() if search() fails to match.
55              
56             =cut
57              
58             sub execute {
59 1     1 1 2 my $self = shift;
60 1         4 my $entry;
61 1 50       11 if ( $self->entry ) {
62 0         0 $entry = $self->entry;
63             }
64             else {
65              
66 1 50       14 my $search = $self->search or croak "search required";
67 1         15 my $where = Data::Dump::dump($search);
68              
69 1 50       615 carp "deleting $where" if $self->debug;
70              
71 1         13 my $msg = $self->ldap->search(@$search);
72 1 50       47376 if ( $msg->count > 0 ) {
73 1         18 $entry = $msg->entry(0);
74             }
75             else {
76              
77             # no match for search.
78             # in SQL, this would just be a no-op, since WHERE failed.
79             # but here we assume that caller expects the object to exist.
80 0         0 croak "delete search failed to match $where:\n"
81             . $self->get_ldap_err($msg);
82             }
83              
84             }
85 1         40 $self->entry( $entry->clone );
86 1         749 $entry->delete;
87 1         57 my $msg = $entry->update( $self->ldap );
88 1 50       2575 if ( $msg->code ) {
89 0         0 croak "failed to delete entry: " . $self->get_ldap_err($msg);
90             }
91              
92 1         28 $self->complete(1);
93 1         3704 return 1;
94             }
95              
96             =head2 rollback
97              
98             Revert the deletion by calling ldap->add for the original Net::LDAP::Entry
99             object.
100              
101             =cut
102              
103             sub rollback {
104 0     0 1   my $self = shift;
105 0 0         return 0 unless $self->complete;
106              
107 0 0         if ( !$self->entry ) {
108 0           croak "cannot rollback deleted entry - no entry cached";
109             }
110              
111 0 0         carp "rolling back delete" if $self->debug;
112              
113 0           my $entry = $self->entry;
114 0           my $msg = $self->ldap->add($entry);
115 0 0         if ( $msg->code ) {
116 0           croak "failed to rollback deletion of entry: "
117             . $self->get_ldap_err($msg);
118             }
119              
120 0           $self->complete(0);
121 0           return 1;
122             }
123              
124             1;
125              
126             __END__