PlugAuth/Role/Flat.pm

filter: Statementstmt 33.3% 38 / 114 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 63.1% 12 / 19 Podpod 0.0% 0 / 7 totaltotal 24.8% 50 / 201 CC 28 SCAR 46.0
CC
28
Cov
23%
read_file
272.0
has_changed
20.0
lock_and_update_file
15.2
CRAP 99.8
« PlugAuth.pm ↑ Summary PlugAuth/Plugin/FlatAuthz.pm »
1 package PlugAuth::Role::Flat;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Log::Log4perl qw( :easy );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use File::stat qw( stat );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use File::Touch qw( touch );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: private role used by L<FlatAuth|PlugAuth::Plugin::FlatAuth> and L<FlatAuthz|PlugAuth::Plugin::FlatAuthz>.
15 9 our $VERSION = '0.39'; # VERSION
16  
17 9 my %MTimes;
18  
19 0 ▶sub has_changed {
Subroutine has_changed: not called CC=4 SCAR=30.0
Pod undocumented
20 0 my $filename = shift;
21 0 ▶ -e $filename or LOGDIE "File $filename does not exist";
Branch: 1 branch
BranchTrueFalseTotal
-e $filename or LOGDIE("File $filename does not exist") 0 0 0
22 0 my $mtime = stat($filename)->mtime;
23 0 ▶ return 0 if $MTimes{$filename} && $MTimes{$filename}==$mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $MTimes{$filename} and $MTimes{$filename} == $mtime 0 0 0
Condition0%0/3
$MTimes{$filename} and $MTimes{$filename} == $mtime
!ll&&!rl&&r
000
MC/DC0%0/2
$MTimes{$filename} and $MTimes{$filename} == $mtime
$MTimes{$filename} $MTimes{$filename} == $mtime
Truth table0%0/3
A and B
A= $MTimes{$filename}B= $MTimes{$filename} == $mtime
ABresult
0X0
100
111
24 0 $MTimes{$filename} = $mtime;
25 0 return 1;
26 }
27  
28 0 ▶sub mark_changed {
Subroutine mark_changed: not called CC=2 SCAR=17.9
Pod undocumented
29 0 delete $MTimes{$_} for @_;
30 }
31  
32 0 ▶sub read_file { # TODO: cache w/ mtime
Subroutine read_file: not called CC=16 SCAR=56.1
Pod undocumented
33 0 my($class, $filename, %args) = @_;
34 0 ▶ $args{nest} ||= 0;
Condition0%0/2
$args{"nest"} ||= 0
l!l
00
MC/DC0%0/1
$args{"nest"} ||= 0
$args{"nest"}
Truth table0%0/2
A
A= $args{"nest"}
Aresult
11
00
35 #
36 # _read_file:
37 # x : y
38 # z : q
39 # returns ( x => y, z => q )
40 #
41 # _read_file(nest => 1):
42 # a : b,c
43 # d : e,f
44 # returns ( x => { b => 1, c => 1 },
45 # d => { e => 1, f => 1 } )
46 #
47 # _read_file(nest => 2):
48 # a : (b) c,d
49 # a : (g) h,i
50 # d : (e) f,g
51 # returns ( a => { b => { c => 1, d => 1 },
52 # { g => { h => 1, i => 1 },
53 # d => { e => { f => 1, g => 1 } );
54 # Lines beginning with a # are ignored.
55 # All spaces are silently squashed.
56 #
57 0 TRACE "reading $filename";
58 0 my %h;
59 0 open my $fh, '<', $filename;
60 0 ▶ flock($fh, LOCK_SH) or WARN "Cannot lock $filename - $!\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 1 or WARN("Cannot lock $filename - $!\n") 0 0 0
61 0 for my $line ($fh->getlines)
62 {
63 0 chomp $line;
64 0 $line =~ s/\s//g;
65 0 ▶ next if $line =~ /^#/ || !length($line);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ or not length $line 0 0 0
Condition0%0/3
$line =~ /^#/ or not length $line
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^#/ or not length $line
$line =~ /^#/ not length $line
Truth table0%0/3
A or not(B)
A= $line =~ /^#/B= not length $line
ABresult
1X1
011
000
66 0 my ($k,$v) = split /:/, $line;
67 0 my $p;
68 # commenting this out because it puts the password salt in
69 # the log file if TRACE is on
70 #TRACE "parsing $v";
71 0 ▶ ($k,$p) = ( $k =~ m/^(.*)\(([^)]*)\)$/) if $args{nest}==2;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"nest"} == 2 0 0 0
72 0 ▶ $k = lc $k if $args{lc_keys};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"lc_keys"} 0 0 0
73 0 ▶ $v = lc $v if $args{lc_values};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"lc_values"} 0 0 0
74 0 ▶ my %m = ( map { $_ => 1 } split /,/, $v ) if $args{nest};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"nest"} 0 0 0
75 0 ▶ if ($args{nest}==0)
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'nest'} == 0) { } 0 0 0
elsif ($args{'nest'} == 1) { } 0 0 0
elsif ($args{'nest'} == 2) { } 0 0 0
76 {
77 0 $h{$k} = $v;
78 }
79 elsif ($args{nest}==1)
80 {
81 0 ▶ $h{$k} ||= {};
Condition0%0/2
$h{$k} ||= {}
l!l
00
MC/DC0%0/1
$h{$k} ||= {}
$h{$k}
Truth table0%0/2
A
A= $h{$k}
Aresult
11
00
82 0 @{ $h{$k} }{keys %m} = values %m;
83 }
84 elsif ($args{nest}==2)
85 {
86 0 ▶ $h{$k} ||= {};
Condition0%0/2
$h{$k} ||= {}
l!l
00
MC/DC0%0/1
$h{$k} ||= {}
$h{$k}
Truth table0%0/2
A
A= $h{$k}
Aresult
11
00
87 0 ▶ $h{$k}{$p} ||= {};
Condition0%0/2
$h{$k}{$p} ||= {}
l!l
00
MC/DC0%0/1
$h{$k}{$p} ||= {}
$h{$k}{$p}
Truth table0%0/2
A
A= $h{$k}{$p}
Aresult
11
00
88 0 @{ $h{$k}{$p} }{keys %m} = values %m;
89 }
90 }
91 0 return %h;
92 }
93  
94 sub temp_dir
95 0 ▶{
Subroutine temp_dir: not called CC=2 SCAR=17.9
Pod undocumented
96 0 state $dir;
97 0 ▶ unless(defined $dir)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
98 {
99 0 $dir = File::Temp::tempdir( CLEANUP => 1);
100 }
101 0 return $dir;
102 }
103  
104 sub flat_init
105 0 ▶{
Subroutine flat_init: not called CC=3 SCAR=24.8
Pod undocumented
106 0 my($self) = @_;
107 0 my $config = $self->global_config;
108
109 0 foreach my $file (qw( group_file resource_file user_file ))
110 {
111 0 ▶ $config->{$file} //= do {
Condition0%0/3
$config->{$file} //= do { my $fn = "File::Spec"->catfile($self->temp_dir, $file); WARN("$file not defined in configuration, using temp $fn, modifiations will be lost on exit"); touch($fn); $fn }
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{$file} //= do { my $fn = "File::Spec"->catfile($self->temp_dir, $file); WARN("$file not defined in configuration, using temp $fn, modifiations will be lost on exit"); touch($fn); $fn }
$config->{$file} do { my $fn = "File::Spec"->catfile($self->temp_dir, $file); WARN("$file not defined in configuration, using temp $fn, modifiations will be lost on exit"); touch($fn); $fn }
Truth table0%0/3
A //= B
A= $config->{$file}B= do { my $fn = "File::Spec"->catfile($self->temp_dir, $file); WARN("$file not defined in configuration, using temp $fn, modifiations will be lost on exit"); touch($fn); $fn }
ABresult
1X1
011
000
112 0 my $fn = File::Spec->catfile($self->temp_dir, $file);
113 0 WARN "$file not defined in configuration, using temp $fn, modifiations will be lost on exit";
114 0 touch $fn;
115 0 $fn;
116 };
117 }
118 }
119  
120 sub lock_and_update_file
121 0 ▶{
Subroutine lock_and_update_file: not called CC=4 SCAR=27.2
Pod undocumented
122 9 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 0 my($self, $filename, $cb) = @_;
124  
125 0 my $buffer;
126
127 0 eval {
128 0 open my $fh, '+<', $filename;
129 0 eval { flock $fh, LOCK_EX };
130 0 ▶ WARN "cannot lock $filename - $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
131
132 0 $buffer = $cb->($fh);
133
134 0 ▶ if(defined $buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer) 0 0 0
135 {
136 0 TRACE "updating $filename";
137 0 seek $fh, 0, 0;
138 0 truncate $fh, 0;
139 0 print $fh $buffer;
140 }
141
142 0 mark_changed($filename);
143 0 close $fh;
144 };
145  
146 0 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
147 {
148 0 ERROR "update $filename: $error";
149 }
150  
151 0 return defined $buffer;
152 }
153  
154 sub lock_and_read_file
155 0 ▶{
Subroutine lock_and_read_file: not called CC=3 SCAR=21.3
Pod undocumented
156 0 my($self, $filename, $cb) = @_;
157
158 9 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
159
160 0 my $ok = eval {
161
162 0 open my $fh, '<', $filename;
163 0 eval { flock $fh, LOCK_SH };
164 0 ▶ WARN "cannot lock $filename - $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
165
166 0 my $ret = $cb->($fh);
167
168 0 close $fh;
169
170 0 $ret;
171 };
172
173 0 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
174 {
175 0 ERROR "reading $filename: $error";
176 }
177
178 0 $ok;
179 }
180  
181 9 1;
182  
183 __END__
« PlugAuth.pm ↑ Summary PlugAuth/Plugin/FlatAuthz.pm »