SPOPS/Iterator/LDAP.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 165 CC 10 SCAR 41.3
CC
10
Cov
0%
fetch_object
110.0
initialize
2.0
finish
2.0
CRAP 62.0
1 package SPOPS::Iterator::LDAP;
2  
3 # $Id: LDAP.pm,v 3.3 2004/06/02 00:48:23 lachoy Exp $
4  
5 use strict;
6 use base qw( SPOPS::Iterator );
7 use Log::Log4perl qw( get_logger );
8  
9 use SPOPS;
10 use SPOPS::Iterator qw( ITER_IS_DONE ITER_FINISHED );
11 use SPOPS::Secure qw( :level );
12  
13 my $log = get_logger();
14  
15 $SPOPS::Iterator::LDAP::VERSION = sprintf("%d.%02d", q$Revision: 3.3 $ =~ /(\d+)\.(\d+)/);
16  
17 # Keys with _LDAP at the beginning are specific to this implementation;
18 # keys without _LDAP at the begining are used in all iterators.
19  
20 sub initialize {
21 my ( $self, $p ) = @_;
22 $self->{_LDAP_MSG} = $p->{ldap_msg};
23  
24 $self->{_LDAP_OFFSET} = $p->{offset};
25 $self->{_LDAP_MAX} = $p->{max};
26  
27 $self->{_LDAP_CONN} = $p->{ldap};
28 $self->{_LDAP_ID_LIST} = $p->{id_list};
29  
30 $self->{_LDAP_COUNT} = 1;
31 $self->{_LDAP_RAW_COUNT} = 0;
32 }
33  
34  
35 # TODO [fetch_object]: Put the pre_fetch_action in here for when we
36 # don't have _LDAP_ID_LIST
37  
38 sub fetch_object {
39 my ( $self ) = @_;
40  
41 # First either grab the row with the ID if we've already got it or
42 # kick the DBI statement handle for the next row
43  
44 my ( $object );
45 my $object_class = $self->{_CLASS};
46 if ( $self->{_LDAP_ID_LIST} ) {
47 my $id = $self->{_LDAP_ID_LIST}->[ $self->{_LDAP_RAW_COUNT} ];
48 $log->is_info &&
49 $log->info( "Trying to retrieve idx ($self->{_LDAP_RAW_COUNT}) with ",
50 "ID ($id) from class ($self->{_CLASS}" );
51 $object = eval { $object_class->fetch( $id,
52 { skip_security => $self->{_SKIP_SECURITY} } ) };
53  
54 # If the object doesn't exist then it's likely a security issue,
55 # in which case we bump up our internal count (but not the
56 # position!) and try again.
57  
58 if ( $@ and $@->isa( 'SPOPS::Exception::Security' ) ) {
59 $log->is_info &&
60 $log->info( "Skip to next item, caught security exception: $@" );
61 $self->{_LDAP_RAW_COUNT}++;
62 return $self->fetch_object;
63 }
64  
65 unless( $object ) {
66 $log->is_info &&
67 $log->info( "Iterator is depleted (no object fetched), notify parent" );
68 return ITER_IS_DONE;
69 }
70 }
71 else {
72 my $entry = $self->{_LDAP_MSG}->shift_entry;
73 unless ( $entry ) {
74 $log->is_info &&
75 $log->info( "Iterator is depleted (no entry returned), notify parent" );
76 return ITER_IS_DONE;
77 }
78  
79 # It's ok to create the object now
80  
81 $object = $object_class->new({ skip_default_values => 1 });
82 $object->_fetch_assign_row( undef, $entry );
83  
84 # Check security on the row unless overridden. If the security
85 # check fails that's ok, just skip the row and move on -- DO
86 # increase our internal index but DON'T increase the position.
87  
88 $object->{tmp_security_level} = SEC_LEVEL_WRITE;
89 unless ( $self->{_SKIP_SECURITY} ) {
90 $object->{tmp_security_level} =
91 eval { $object->check_action_security({
92 required => SEC_LEVEL_READ }) };
93 if ( $@ ) {
94 $log->is_info &&
95 $log->info( "Security check for ($self->{_CLASS}) failed: $@" );
96 $self->{_LDAP_RAW_COUNT}++;
97 return $self->fetch_object;
98 }
99 }
100  
101 # Now call the post_fetch callback; if it fails, fetch another row
102  
103 my $post_ok = $object->_fetch_post_process(
104 {}, $object->{tmp_security_level} );
105 unless ( $post_ok ) {
106 $log->is_info &&
107 $log->info( "Post process for object failed; get next" );
108 $self->{_LDAP_RAW_COUNT}++;
109 return $self->fetch_object;
110 }
111 }
112  
113 # Not to the offset yet, so call ourselves again to go to the next
114 # row but first bump up the count.
115  
116 # Note: this *does* need to be beneath the fetch and security stuff
117 # above b/c the count refers to the records this user *could*
118 # see. So if 25 records matched the criteria but the user couldn't
119 # see 5 of them, we only want to the user to know and iterate
120 # through 1-20, not know that we actually picked the records
121 # 1-10,12,14,16-22,24-25 and discarded the others due to security.
122  
123 if ( $self->{_LDAP_OFFSET} and
124 ( $self->{_LDAP_COUNT} < $self->{_LDAP_OFFSET} ) ) {
125 $log->is_info &&
126 $log->info( "Not reached the offset yet, get next object" );
127 $self->{_LDAP_COUNT}++;
128 $self->{_LDAP_RAW_COUNT}++;
129 return $self->fetch_object;
130 }
131  
132 # Oops, we've gone past the max. Finish up.
133  
134 if ( $self->{_LDAP_MAX} and
135 ( $self->{_LDAP_COUNT} > $self->{_LDAP_MAX} ) ) {
136 $log->is_info &&
137 $log->info( "Fetched past the MAX number of objects, done" );
138 return ITER_IS_DONE;
139 }
140  
141 # Ok, we've navigated everything we need to, which means we can
142 # actually return this record. So bump up both counts. Note that
143 # we also use this count for the ID list.
144  
145 $self->{_LDAP_COUNT}++;
146 $self->{_LDAP_RAW_COUNT}++;
147  
148 return ( $object, $self->{_LDAP_COUNT} );
149 }
150  
151  
152 # Might need to wrap this in an eval
153  
154 sub finish {
155 my ( $self ) = @_;
156 undef $self->{_LDAP_MSG};
157 return $self->{ ITER_FINISHED() } = 1;
158 }
159  
160 1;
161  
162 __END__