blib/lib/Authorize/Rule.pm

filter: Statementstmt 93.4% 99 / 106 Branchbran 79.6% 51 / 64 Conditioncond 86.9% 20 / 23 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 81.8% 9 / 11 Podpod 75.0% 6 / 8 totaltotal 86.6% 194 / 224 CC 54 SCAR 26.9
CC
54
Cov
88%
allowed
18.1
match_ruleset
18.1
new
16.2
CRAP 14.8
↑ Summary
1 package Authorize::Rule;
2 # ABSTRACT: Rule-based authorization mechanism
3 20 $Authorize::Rule::VERSION = '0.009';
4 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶sub new {
Subroutine new: called CC=16 SCAR=27.9
Pod undocumented
9 20 my $class = shift;
10 20 my %opts = @_;
11  
12 20 ▶ defined $opts{'rules'}
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{"rules"} or croak("You must provide rules") 19 1 20
13 or croak 'You must provide rules';
14  
15 19 my $rules = $opts{'rules'};
16 19 ▶ ref($rules) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $rules eq "HASH" or croak("attribute rules must be a hashref") 19 0 19
17 or croak 'attribute rules must be a hashref';
18  
19 # expand entity groups to their entities
20 19 ▶ if ( $opts{'entity_groups'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"entity_groups"}) 1 18 19
21 1 ▶ ref( $opts{'entity_groups'} ) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{"entity_groups"} eq "HASH" or croak("attribute entity_groups must be a hashref") 1 0 1
22 or croak 'attribute entity_groups must be a hashref';
23  
24 1 foreach my $group ( keys %{ $opts{'entity_groups'} } ) {
25 1 ▶ my $group_rules = delete $rules->{$group}
Branch: 1 branch
BranchTrueFalseTotal
my $group_rules = delete $rules->{$group} or next 1 0 1
26 or next;
27  
28 $rules->{$_} = $group_rules
29 1 for @{ $opts{'entity_groups'}{$group} };
30 }
31 }
32  
33 # expand resource groups to their entities
34 19 ▶ if ( $opts{'resource_groups'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"resource_groups"}) 1 18 19
35 1 ▶ ref( $opts{'resource_groups'} ) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{"resource_groups"} eq "HASH" or croak("attribute resource_groups must be a hashref") 1 0 1
36 or croak 'attribute resource_groups must be a hashref';
37  
38 # populate
39 1 foreach my $entity ( keys %{$rules} ) {
40 1 foreach my $resource ( keys %{ $rules->{$entity} } ) {
41 2 ▶ my $in_rsrc = $opts{'resource_groups'}{$resource}
Branch: 1 branch
BranchTrueFalseTotal
my $in_rsrc = $opts{"resource_groups"}{$resource} or next 1 1 2
42 or next;
43  
44 $rules->{$entity}{$_} = $rules->{$entity}{$resource}
45 1 for @{$in_rsrc};
46 }
47 }
48  
49 # delete
50 1 foreach my $entity ( keys %{$rules} ) {
51 delete $rules->{$entity}{$_}
52 1 for keys %{ $opts{'resource_groups'} };
53 }
54 }
55  
56 19 return bless {
57 default => 0, # deny by default
58 %opts,
59 }, $class;
60 }
61  
62 115 ▶sub default {
Subroutine default: called CC=2 SCAR=7.1
Pod documented
63 115 my $self = shift;
64 115 ▶ @_ and croak 'default() is a ro attribute';
Branch: 1 branch
BranchTrueFalseTotal
@_ and croak("default() is a ro attribute") 0 115 115
65 115 return $self->{'default'};
66 }
67  
68 0 ▶sub entity_groups {
Subroutine entity_groups: not called CC=2 SCAR=17.9
Pod documented
69 0 my $self = shift;
70 0 ▶ @_ and croak 'entity_groups() is a ro attribute';
Branch: 1 branch
BranchTrueFalseTotal
@_ and croak("entity_groups() is a ro attribute") 0 0 0
71 0 return $self->{'entity_groups'};
72 }
73  
74 0 ▶sub resource_groups {
Subroutine resource_groups: not called CC=2 SCAR=17.9
Pod documented
75 0 my $self = shift;
76 0 ▶ @_ and croak 'resource_groups() is a ro attribute';
Branch: 1 branch
BranchTrueFalseTotal
@_ and croak("resource_groups() is a ro attribute") 0 0 0
77 0 return $self->{'resource_groups'};
78 }
79  
80 115 ▶sub rules {
Subroutine rules: called CC=2 SCAR=7.1
Pod documented
81 115 my $self = shift;
82 115 ▶ @_ and croak 'rules() is a ro attribute';
Branch: 1 branch
BranchTrueFalseTotal
@_ and croak("rules() is a ro attribute") 0 115 115
83 115 return $self->{'rules'};
84 }
85  
86 110 ▶sub is_allowed {
Subroutine is_allowed: called CC=1 SCAR=0.0
Pod documented
87 110 my $self = shift;
88 110 return $self->allowed(@_)->{'action'};
89 }
90  
91 115 ▶sub allowed {
Subroutine allowed: called CC=18 SCAR=28.9
Pod documented
92 115 my $self = shift;
93 115 my $entity = shift;
94 115 my $req_resource = shift;
95 115 ▶ my $req_params = shift || {};
Condition100%2/2
shift() || {}
l!l
5164
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
96 115 my $default = $self->default;
97 115 my $rules = $self->rules;
98 115 ▶ my %result = (
Condition50%1/2
$req_resource || ''
l!l
1150
MC/DC0%0/1
$req_resource || ''
$req_resource
Truth table50%1/2
A
A= $req_resource
Aresult
11
00
99 entity => $entity,
100 resource => ($req_resource || ''),
101 params => $req_params,
102 );
103  
104 115 ▶ my $perms = $rules->{$entity} || {};
Condition100%2/2
$rules->{$entity} || {}
l!l
1123
MC/DC100%1/1
$rules->{$entity} || {}
$rules->{$entity}
Truth table100%2/2
A
A= $rules->{$entity}
Aresult
11
00
105 115 ▶ my $all_entities_perms = $rules->{''} || {};
Condition100%2/2
$rules->{''} || {}
l!l
5110
MC/DC100%1/1
$rules->{''} || {}
$rules->{''}
Truth table100%2/2
A
A= $rules->{''}
Aresult
11
00
106  
107 # deny entities that aren't in the rules
108 115 ▶ $perms || $all_entities_perms
Branch: 1 branch
BranchTrueFalseTotal
$perms or $all_entities_perms or return {%result, "action", $default} 115 0 115
Condition33%1/3
$perms or $all_entities_perms
l!l&&r!l&&!r
11500
MC/DC0%0/2
$perms or $all_entities_perms
$perms $all_entities_perms
Truth table33%1/3
A or B
A= $permsB= $all_entities_perms
ABresult
1X1
011
000
109 or return { %result, action => $default };
110  
111 # the requested and default
112 115 ▶ my $main_ruleset = $perms->{$req_resource} || [];
Condition100%2/2
$perms->{$req_resource} || []
l!l
6154
MC/DC100%1/1
$perms->{$req_resource} || []
$perms->{$req_resource}
Truth table100%2/2
A
A= $perms->{$req_resource}
Aresult
11
00
113 115 ▶ my $def_ruleset = $perms->{''} || [];
Condition100%2/2
$perms->{''} || []
l!l
8827
MC/DC100%1/1
$perms->{''} || []
$perms->{''}
Truth table100%2/2
A
A= $perms->{''}
Aresult
11
00
114  
115 # perm for all the entities. Lower priority than main&def ruleset
116 # we don't need to check $all_entities_perms->{''} because we have $default
117 115 ▶ my $all_entities_ruleset = $all_entities_perms->{$req_resource} || [];
Condition100%2/2
$all_entities_perms->{$req_resource} || []
l!l
2113
MC/DC100%1/1
$all_entities_perms->{$req_resource} || []
$all_entities_perms->{$req_resource}
Truth table100%2/2
A
A= $all_entities_perms->{$req_resource}
Aresult
11
00
118  
119 # if neither, return default action
120 115 ▶ @{ $main_ruleset } || @{ $def_ruleset } || @{ $all_entities_ruleset }
Branch: 1 branch
BranchTrueFalseTotal
@{$main_ruleset;} or @{$def_ruleset;} or @{$all_entities_ruleset;} or return {%result, "action", $default} 111 4 115
Condition100%6/6
@{$main_ruleset;} or @{$def_ruleset;} or @{$all_entities_ruleset;}
l!l&&r!l&&!r
10924
@{$main_ruleset;} or @{$def_ruleset;}
l!l&&r!l&&!r
61486
MC/DC100%3/3
@{$main_ruleset;} or @{$def_ruleset;} or @{$all_entities_ruleset;}
@{$main_ruleset;} @{$def_ruleset;} @{$all_entities_ruleset;}
Truth table100%4/4
A or B or C
A= @{$main_ruleset;}B= @{$def_ruleset;}C= @{$all_entities_ruleset;}
ABCresult
1XX1
01X1
0011
0000
121 or return { %result, action => $default };
122  
123 111 foreach my $rulesets ( $main_ruleset, $def_ruleset, $all_entities_ruleset) {
124 175 my $ruleset_idx = 0;
125 175 my $label;
126  
127 175 R_SET: foreach my $ruleset ( @{$rulesets} ) {
128 146 ▶ if ( ! ref $ruleset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ruleset) 11 135 146
129 11 $label = $ruleset;
130 11 next R_SET;
131 }
132  
133 135 $ruleset_idx++;
134  
135 135 my $action = $self->match_ruleset( $ruleset, $req_params );
136  
137 134 ▶ if ( defined $action ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action) 105 29 134
138 105 my %full_result = (
139 %result,
140 ruleset_idx => $ruleset_idx,
141 ( label => $label )x!! $label,
142 );
143  
144 105 ▶ $full_result{'action'} = ref $action eq 'CODE' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $action eq 'CODE' ? : 1 104 105
145 $action->( \%full_result ) :
146 $action;
147  
148 105 return \%full_result;
149 }
150  
151 29 undef $label;
152 }
153 }
154  
155 5 return { %result, action => $default };
156 }
157  
158 135 ▶sub match_ruleset {
Subroutine match_ruleset: called CC=18 SCAR=28.9
Pod undocumented
159 135 my $self = shift;
160 135 my $ruleset = shift;
161 135 my $req_params = shift;
162  
163 135 ▶ my ( $action, @rules ) = @{$ruleset}
Branch: 1 branch
BranchTrueFalseTotal
() = @{$ruleset;} or return 135 0 135
164 or return;
165  
166 # an empty return() is a failure to match
167 # if matching of a rule succeeds, we just move to the next rule
168 135 foreach my $rule (@rules) {
169 64 ▶ if ( ref $rule eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $rule eq 'HASH') { } 49 15 64
elsif (ref $rule eq 'CODE') { } 2 13 15
elsif (not ref $rule) { } 13 0 13
170 # check defined params by rule against requested params
171 49 KEY: foreach my $key ( keys %{$rule} ) {
172 50 ▶ if ( defined $rule->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->{$key}) { } 48 2 50
173 # check if key is missing
174 48 ▶ defined $req_params->{$key}
Branch: 1 branch
BranchTrueFalseTotal
defined $req_params->{$key} or return 37 11 48
175 or return;
176 } else {
177 # check the key exists and value is defined
178 2 ▶ exists $req_params->{$key}
Branch: 1 branch
BranchTrueFalseTotal
exists $req_params->{$key} and return 1 1 2
179 and return;
180  
181 # don't continue checking the value in this case
182 # because it's undefined
183 1 next KEY;
184 }
185  
186 # check matching against a code reference
187 37 ▶ if ( ref $rule->{$key} eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $rule->{$key} eq 'CODE') { } 2 35 37
elsif (ref $rule->{$key} eq 'Regexp') { } 3 32 35
elsif (ref $rule->{$key}) { } 1 31 32
188 2 ▶ $req_params->{$key} eq $rule->{$key}->($req_params)
Branch: 1 branch
BranchTrueFalseTotal
$req_params->{$key} eq $rule->{$key}->($req_params) or return 1 1 2
189 or return;
190 } elsif ( ref $rule->{$key} eq 'Regexp' ) {
191 3 ▶ $req_params->{$key} =~ $rule->{$key}
Branch: 1 branch
BranchTrueFalseTotal
$req_params->{$key} =~ /$rule->{$key}/ or return 2 1 3
192 or return;
193 } elsif ( ref $rule->{$key} ) {
194 1 croak 'Rule keys can only be strings, regexps, or code';
195 } else {
196 # check matching against a simple string
197 31 ▶ $req_params->{$key} eq $rule->{$key}
Branch: 1 branch
BranchTrueFalseTotal
$req_params->{$key} eq $rule->{$key} or return 22 9 31
198 or return; # no match
199 }
200 }
201 } elsif ( ref $rule eq 'CODE' ) {
202 2 ▶ $rule->($req_params)
Branch: 1 branch
BranchTrueFalseTotal
&$rule($req_params) or return 1 1 2
203 or return;
204 } elsif ( ! ref $rule ) {
205 13 ▶ defined $req_params->{$rule}
Branch: 1 branch
BranchTrueFalseTotal
defined $req_params->{$rule} or return 8 5 13
206 or return; # no match
207 } else {
208 0 croak 'Unknown rule type';
209 }
210 }
211  
212 105 return $action;
213 }
214  
215 20 1;
216  
217 __END__
↑ Summary