blib/lib/Mojolicious/Plugin/AccessControl.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 80.0% 24 / 30 Conditioncond 71.4% 10 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 86.9% 113 / 130 CC 22 SCAR 20.2
CC
22
Cov
90%
__ANON__
13.6
_rules
7.0
__ANON__
3.2
CRAP 7.5
↑ Summary
1 package Mojolicious::Plugin::AccessControl;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION = '0.04';
5  
6 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Net::CIDR::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use constant CONDITION_NAME => 'access';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
13 1 my ( $self, $app ) = @_;
14  
15 $app->routes->add_condition(
16 54 ▶ CONDITION_NAME() => sub {
Subroutine __ANON__: called CC=13 SCAR=26.1
17 54 my ( $r, $c, $cap, $args ) = @_;
18 54 ▶ $args ||= [];
Condition50%1/2
$args ||= []
l!l
540
MC/DC0%0/1
$args ||= []
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
19  
20 54 my $opt = {};
21 54 my @rule_list = @$args;
22  
23 54 ▶ if ( ref $args->[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[0] eq "HASH") 3 51 54
24 3 $opt = $args->[0];
25  
26 # Remove option ref from rule list
27 3 @rule_list = splice(@$args, 1);
28 }
29  
30 54 ▶ if ( $opt->{on_deny} && ref $opt->{on_deny} ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"on_deny"} and ref $opt->{"on_deny"} ne "CODE") 0 54 54
Condition66%2/3
$opt->{"on_deny"} and ref $opt->{"on_deny"} ne "CODE"
!ll&&!rl&&r
5130
MC/DC0%0/2
$opt->{"on_deny"} and ref $opt->{"on_deny"} ne "CODE"
$opt->{"on_deny"} ref $opt->{"on_deny"} ne "CODE"
Truth table66%2/3
A and B
A= $opt->{"on_deny"}B= ref $opt->{"on_deny"} ne "CODE"
ABresult
0X0
100
111
31 0 Carp::croak "on_deny must be a CODEREF";
32 }
33  
34 54 ▶ $opt->{cache} = 1 unless ( defined $opt->{cache} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"cache"} 52 2 54
35 my $rules = ( $opt->{cache} )
36 54 ▶ ? ( $r->{ __PACKAGE__ . '._rules' } ||= $self->_rules(@rule_list) ) # caches to Mojolicious::Routes::Route
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'cache'} ? : 54 0 54
Condition66%2/3
$r->{"Mojolicious::Plugin::AccessControl._rules"} ||= $self->_rules(@rule_list)
l!l&&r!l&&!r
4680
MC/DC0%0/2
$r->{"Mojolicious::Plugin::AccessControl._rules"} ||= $self->_rules(@rule_list)
$r->{"Mojolicious::Plugin::AccessControl._rules"} $self->_rules(@rule_list)
Truth table66%2/3
A ||= B
A= $r->{"Mojolicious::Plugin::AccessControl._rules"}B= $self->_rules(@rule_list)
ABresult
1X1
011
000
37 : $self->_rules(@rule_list);
38  
39 54 for my $rule (@$rules) {
40 33 my ( $check, $allow ) = @{$rule};
41  
42 33 my $result = $check->($c);
43 33 ▶ if ( defined $result && $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and $result) 30 3 33
Condition66%2/3
defined $result and $result
!ll&&!rl&&r
0330
MC/DC50%1/2
defined $result and $result
defined $result $result
Truth table66%2/3
A and B
A= defined $resultB= $result
ABresult
0X0
100
111
44  
45 # denied
46 30 ▶ if ( !$allow && $opt->{on_deny} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $allow and $opt->{"on_deny"}) 3 27 30
Condition100%3/3
not $allow and $opt->{"on_deny"}
!ll&&!rl&&r
6213
MC/DC100%2/2
not $allow and $opt->{"on_deny"}
not $allow $opt->{"on_deny"}
Truth table100%3/3
not(A) and B
A= not $allowB= $opt->{"on_deny"}
ABresult
0X0
100
111
47 3 $opt->{on_deny}->($c);
48 }
49  
50 30 return $allow;
51 }
52 }
53  
54 24 return 1;
55 }
56 1 );
57 }
58  
59 8 ▶sub _rules {
Subroutine _rules: called CC=7 SCAR=19.5
60 8 my ($self, @args) = @_;
61  
62 8 my @rules;
63 8 ▶ for ( my $i = 0; $i < @args; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 11 8 19
64 11 my ( $allowing, $rule ) = ( $args[$i], $args[ $i + 1 ] );
65  
66 11 ▶ Carp::croak "must be allow or deny"
Branch: 1 branch
BranchTrueFalseTotal
unless $allowing =~ /^(allow|deny)$/u 0 11 11
67 unless $allowing =~ /^(allow|deny)$/;
68  
69 11 ▶ $allowing = ( $allowing eq 'allow' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowing eq 'allow' ? : 4 7 11
70 11 my $check = $rule;
71  
72 11 ▶ if ( $rule =~ /^ALL$/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rule =~ /^ALL$/iu) { } 3 8 11
elsif ($rule =~ /[A-Z]$/iu) { } 2 6 8
elsif (ref $rule ne 'CODE') { } 5 1 6
73 3 ▶ $check = sub {1};
Subroutine __ANON__: called CC=1 SCAR=0.0
74 }
75 elsif ( $rule =~ /[A-Z]$/i ) {
76 6 ▶ $check = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
77 6 my $host = $_[0]->req->env->{'REMOTE_HOST'};
78 6 ▶ return unless defined $host; # skip
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 6 6
79 6 return $host =~ /^(.*\.)?\Q${rule}\E$/;
80 2 };
81 }
82 elsif ( ref($rule) ne 'CODE' ) {
83 5 my $cidr = Net::CIDR::Lite->new();
84 5 $cidr->add_any($rule);
85  
86 21 ▶ $check = sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
87 21 my $addr = $_[0]->tx->remote_address;
88 21 ▶ if ( defined $addr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr) 21 0 21
89 21 ▶ return ( $cidr->find($addr) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cidr->find($addr) ? : 21 0 21
90 }
91 5 };
92 }
93  
94 11 push @rules, [ $check => $allowing ];
95 }
96  
97 8 return \@rules;
98 }
99  
100 2 1;
101 __END__
↑ Summary