File Coverage

blib/lib/Gantry/Utils/CRUDHelp.pm
Criterion Covered Total %
statement 20 90 22.2
branch 10 56 17.8
condition 5 46 10.8
subroutine 3 7 42.8
pod 5 5 100.0
total 43 204 21.0


line stmt bran cond sub pod time code
1             package Gantry::Utils::CRUDHelp;
2 8     8   24348 use strict;
  8         16  
  8         297  
3              
4 8     8   43 use base 'Exporter';
  8         15  
  8         10984  
5              
6             our @EXPORT = qw(
7             clean_dates
8             form_profile
9             clean_params
10             write_file
11             verify_permission
12             );
13              
14             sub write_file {
15 0     0 1 0 my( $self, $field, $archive, $extra_dir, $forced_file_name ) = @_;
16            
17 0         0 my $upload = $self->file_upload( $field );
18              
19 0         0 my $id = $upload->{unique_key};
20              
21 0 0       0 if ( $forced_file_name ) {
22 0         0 $upload->{ident} = $forced_file_name . $upload->{suffix};
23             }
24             else {
25 0         0 $upload->{ident} = $id . $upload->{suffix};
26             }
27            
28 0         0 my $file = File::Spec->catfile( $archive, $extra_dir, $upload->{ident} );
29              
30 0         0 my $dir = File::Spec->catfile( $archive, $extra_dir );
31 0         0 File::Path::mkpath( $dir );
32            
33 0 0       0 open( FH, ">", $file ) or die "Error unable to open $file: $!";
34 0         0 binmode FH;
35              
36 0         0 my( $buffer, $buffer_size ) = ( '', 14096 );
37 0         0 while ( read( $upload->{filehandle}, $buffer, $buffer_size ) ) {
38 0         0 print FH $buffer;
39             }
40 0         0 close FH;
41            
42 0         0 my $h = {
43             "$field" => $file,
44             "${field}_ident" => $upload->{ident},
45             "${field}_suffix" => $upload->{suffix},
46             "${field}_mime" => $upload->{mime},
47             "${field}_name" => $upload->{name},
48             "${field}_size" => $upload->{size},
49             };
50            
51 0 0       0 $h->{"${field}_directory"} = $extra_dir if $extra_dir;
52            
53 0         0 return( $h );
54              
55             }
56              
57             # If a field is a date and its value is false, make it undef.
58             sub clean_dates {
59 0     0 1 0 my ( $params, $fields ) = @_;
60              
61 0         0 foreach my $field ( @{ $fields } ) {
  0         0  
62 0         0 my $name = $field->{name};
63              
64 0 0 0     0 if ( ( $field->{is} eq 'date' )
65             and
66             ( not $params->{ $name } )
67             )
68             {
69 0         0 $params->{ $name } = undef;
70             }
71             }
72             }
73              
74             # build the profile that Data::FormValidator wants
75             sub form_profile {
76 2     2 1 1636 my ( $form_fields ) = @_;
77 2         4 my @required;
78             my @optional;
79 0         0 my %constraints;
80              
81 2         3 foreach my $item ( @{ $form_fields } ) {
  2         8  
82 6 100 66     36 if ( defined $$item{optional} and $$item{optional} ) {
    50 33        
83 2         5 push @optional, $$item{name};
84             }
85             elsif ( defined $$item{type} and $$item{type} eq 'display' ) {
86 0         0 push @optional, $$item{name};
87             }
88             else {
89 4         8 push @required, $$item{name};
90             }
91              
92 6 100 66     31 if ( defined $$item{constraint} and $$item{constraint} ) {
93 1         5 $constraints{ $$item{name} } = $$item{constraint};
94             }
95             }
96              
97 2         4 my %retval;
98              
99 2 50       10 $retval{required} = \@required if @required;
100 2 100       7 $retval{optional} = \@optional if @optional;
101 2 100       8 $retval{constraint_methods} = \%constraints if ( keys %constraints );
102              
103 2         10 return \%retval;
104             }
105              
106             # If a field's type is not boolean, and its value is false, make that
107             # value undef.
108             sub clean_params {
109 0     0 1   my ( $params, $fields ) = @_;
110              
111 0           foreach my $p ( keys %{ $params } ) {
  0            
112 0 0         delete( $params->{$p} ) if $p =~ /^\./;
113             }
114            
115             FIELD:
116 0           foreach my $field ( @{ $fields } ) {
  0            
117 0           my $name = $field->{name};
118              
119 0 0         next FIELD unless ( defined $field->{ is } );
120 0 0         next FIELD unless ( defined $field->{ name } );
121 0 0         next FIELD unless ( defined $params->{ $name } );
122              
123 0 0 0       if ( $field->{ is } =~ /^varchar/i and $params->{ $name } eq '' ) {
    0 0        
    0 0        
124 0           $params->{ $name } = undef;
125             }
126             elsif ( $field->{ is } =~ /^int/i and $params->{ $name } eq '' ) {
127 0           $params->{ $name } = undef;
128             }
129             elsif ( ( $field->{is} !~ /^bool/i and $field->{is} !~ /^int/i )
130             and
131             ( not $params->{ $name } )
132             )
133             {
134 0           $params->{ $name } = undef;
135             }
136             }
137             }
138              
139             my %action_offset = (
140             add => 0,
141             retrieve => 1,
142             edit => 2,
143             delete => 3,
144             );
145              
146             # Full permissions bits:
147             # 123456789 1
148             # crudcrudcrud
149              
150             sub verify_permission {
151 0     0 1   my $opts = shift;
152              
153 0           my $site = $opts->{ site };
154 0           my $row = $opts->{ row };
155 0           my $permissions = $opts->{ permissions };
156 0           my $action = $opts->{ action };
157 0   0       my $params = $opts->{ params } || {}; # default for delete
158              
159 0 0         if ( not defined $action ) {
160 0           $action = $site->action();
161 0           $action =~ s/^do_//;
162             }
163              
164 0   0       $permissions ||= $site->controller_config->{ permissions };
165 0 0         return if ( not defined $permissions ); # no permissions => every body in
166              
167 0           my $offset = $action_offset{ $action };
168 0           my $action_bit = substr 'crud', $offset, 1;
169              
170 0           my $owner_bit = substr $permissions->{ bits }, $offset, 1;
171 0           my $group_bit = substr $permissions->{ bits }, $offset + 4, 1;
172 0           my $other_bit = substr $permissions->{ bits }, $offset + 8, 1;
173              
174             # there are three ways you could be allowed to add, if permissions
175             # are in use
176             # 1. You are not logged in, but the other block has perm bit
177             # 2. You are logged in and the user block has perm bit
178             # 3. You are logged in and belong to the tables group which has perm bit
179              
180 0           my $user_row = $site->auth_user_row;
181 0           my $user_id = $user_row->id;
182              
183 0 0 0       if ( $action eq 'add' ) {
    0          
184             # For add, set the id in case we need it. Anonymous users get id 0.
185 0   0       $params->{ user_id } = $user_id || 0;
186              
187             # is user logged in? if so an owner_bit will work
188 0 0 0       return if ( $user_id
189             and
190             $owner_bit eq $action_bit
191             );
192             }
193             elsif ( $action eq 'edit' or $action eq 'delete' ) {
194 0           delete $params->{ user_id }; # no form spoofing to change owner
195              
196 0 0 0       return if ( $user_id and $user_id eq $row->user_id
      0        
197             and
198             $owner_bit eq $action_bit
199             );
200             }
201              
202             # group work here
203 0           my $member_of = $site->auth_user_groups;
204              
205             return if ( $permissions->{ group }
206             and
207             $member_of->{ $permissions->{ group } }
208             and
209 0 0 0       $group_bit eq $action_bit
      0        
210             );
211              
212             # last chance, is it open to all?
213 0 0         return if $other_bit eq $action_bit;
214              
215 0 0         if ( $action eq 'add' ) {
    0          
    0          
216 0           die "You are not authorized to add records here.\n";
217             }
218             elsif ( $action eq 'edit' ) {
219 0           die "You are not authorized to edit this record.\n";
220             }
221             elsif ( $action eq 'delete' ) {
222 0           die "You are not authorzied to delete this record.\n";
223             }
224             } # end of verify_permissions
225              
226             1;
227              
228             __END__