ACL/Engine.pm

filter: Statementstmt 96.7% 148 / 153 Branchbran 75.9% 41 / 54 Conditioncond 59.2% 16 / 27 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 100% 30 / 30 Podpod 100% 10 / 10 totaltotal 86.1% 249 / 289 CC 42 SCAR 18.0
CC
42
Cov
88%
fudge_condition
12.0
add_rule
11.6
check_action_rules
9.8
CRAP 6.1
↑ Summary ACL.pm »
1 package Catalyst::Plugin::Authorization::ACL::Engine;
2 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 $Catalyst::Plugin::Authorization::ACL::Engine::AUTHORITY = 'cpan:RKITOVER';
4 }
5 4 $Catalyst::Plugin::Authorization::ACL::Engine::VERSION = '0.16';
6 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 extends qw/Moose::Object Exporter/;
9  
10 # I heart stevan
11 4 ▶use Class::Throwable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Tree::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Tree::Simple::Visitor::FindByPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Tree::Simple::Visitor::GetAllDescendents;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 has app => (is => 'rw');
19 2 ▶has actions => (is => 'ro', isa => 'HashRef', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
20 4 has _app_actions_tree => (is => 'ro', isa => 'Tree::Simple', lazy_build => 1);
21  
22 4 our $DENIED = bless {}, __PACKAGE__ . "::Denied";
23 4 our $ALLOWED = bless {}, __PACKAGE__ . "::Allowed";
24  
25 4 our @EXPORT_OK = qw/$DENIED $ALLOWED/;
26  
27 2 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
28 2 my ($self, $c) = @_;
29 2 return +{ app => $c };
30 }
31  
32 2 ▶sub _build__app_actions_tree {
Subroutine _build__app_actions_tree: called CC=6 SCAR=17.9
33 2 my $self = shift;
34 2 my $root = Tree::Simple->new('/', Tree::Simple->ROOT);
35 2 my $app = $self->app;
36  
37 my @actions = grep defined, map {
38 8 my $controller = $_;
39 8 map $controller->action_for($_->name), $controller->get_action_methods
40 2 } grep $_->isa('Catalyst::Controller'), values %{ $app->components };
41  
42 2 for my $action (@actions) {
43 59 my @path = split '/', $action->reverse;
44 59 my $name = pop @path;
45  
46 59 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 30 29 59
47 30 my $by_path = Tree::Simple::Visitor::FindByPath->new;
48 30 $by_path->setSearchPath(@path);
49 30 $root->accept($by_path);
50  
51 30 ▶ if (my $namespace_node = $by_path->getResult) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $namespace_node = $by_path->getResult) 26 4 30
52 26 $namespace_node->addChild(Tree::Simple->new($action));
53 26 next;
54 }
55 }
56  
57 33 my $node = $root;
58 33 for my $el (@path) {
59 5 ▶ if (my $found = first { $_->getNodeValue eq $el }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $found = &first(sub { $_->getNodeValue eq $el; } , @{$node->getAllChildren;})) { } 1 4 5
60 5 @{ $node->getAllChildren }) {
61 1 $node = $found;
62 }
63 else {
64 4 $node = Tree::Simple->new($el, $node);
65 }
66 }
67  
68 33 $node->addChild(Tree::Simple->new($action));
69 }
70  
71 2 return $root;
72 }
73  
74 6 ▶sub add_deny {
Subroutine add_deny: called CC=1 SCAR=0.0
Pod documented
75 6 my ( $self, $spec, $condition ) = @_;
76  
77 6 my $test = $self->fudge_condition($condition);
78  
79 $self->add_rule(
80 $spec,
81 41 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
82 41 my $c = shift;
83 41 ▶ die $DENIED unless $c->$test(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless $c->$test(@_) 29 12 41
84 },
85 6 );
86 }
87  
88 4 ▶sub add_allow {
Subroutine add_allow: called CC=1 SCAR=0.0
Pod documented
89 4 my ( $self, $spec, $condition ) = @_;
90  
91 4 my $test = $self->fudge_condition($condition);
92  
93 $self->add_rule(
94 $spec,
95 73 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 73 my $c = shift;
97 73 ▶ die $ALLOWED if $c->$test(@_);
Branch: 1 branch
BranchTrueFalseTotal
if $c->$test(@_) 70 3 73
98 },
99 4 );
100 }
101  
102 10 ▶sub fudge_condition {
Subroutine fudge_condition: called CC=10 SCAR=24.9
Pod documented
103 10 my ( $self, $condition ) = @_;
104  
105 # make almost anything into a code ref/method name
106  
107 10 ▶ if (!defined($condition)
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $condition or $condition eq '1' or $condition eq '0' or $condition eq '') { } 4 6 10
elsif (my $reftype = ref $condition) { } 6 0 6
elsif ($self->app->can($condition)) { } 0 0 0
Condition77%7/9
not defined $condition or $condition eq 1 or $condition eq 0 or $condition eq ""
l!l&&r!l&&!r
406
not defined $condition or $condition eq 1 or $condition eq 0
l!l&&r!l&&!r
406
not defined $condition or $condition eq 1
l!l&&r!l&&!r
226
MC/DC50%2/4
not defined $condition or $condition eq 1 or $condition eq 0 or $condition eq ""
not defined $condition $condition eq 1 $condition eq 0 $condition eq ""
Truth table60%3/5
not(A) or B or C or D
A= not defined $conditionB= $condition eq 1C= $condition eq 0D= $condition eq ""
ABCDresult
1XXX1
01XX1
001X1
00011
00000
108 # no warnings
109 or $condition eq '1'
110 or $condition eq '0'
111 or $condition eq "" )
112 {
113 4 ▶ return sub { $condition };
Subroutine __ANON__: called CC=1 SCAR=0.0
114 }
115 elsif ( my $reftype = ref $condition ) {
116 6 ▶ $reftype eq "CODE" and return $condition;
Branch: 1 branch
BranchTrueFalseTotal
$reftype eq "CODE" and return $condition 4 2 6
117  
118 # if it's not a code ref and it's a ref, we only know
119 # how to deal with it if it's an array of roles
120 2 ▶ $reftype ne "ARRAY"
Branch: 1 branch
BranchTrueFalseTotal
$reftype ne "ARRAY" and die "Can't interpret '${condition}' as an ACL condition" 0 2 2
121 and die "Can't interpret '$condition' as an ACL condition";
122  
123 # but to check roles we need the appropriate plugin
124 2 ▶ $self->app->isa("Catalyst::Plugin::Authorization::Roles")
Branch: 1 branch
BranchTrueFalseTotal
$self->app->isa("Catalyst::Plugin::Authorization::Roles") or die "Can't use role list as an ACL condition unless the Authorization::Roles plugin is also loaded." 2 0 2
125 or die "Can't use role list as an ACL condition unless "
126 . "the Authorization::Roles plugin is also loaded.";
127  
128 # return a test that will check for the roles
129 10 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 10 my $c = shift;
131 10 $c->check_user_roles(@$condition);
132 2 };
133 }
134 elsif ( $self->app->can($condition) ) {
135 0 return $condition; # just a method name
136 }
137 else {
138 0 croak "Can't use '$condition' as an ACL "
139 . "condition unless \$c->can('$condition').";
140 }
141 }
142  
143 11 ▶sub add_rule {
Subroutine add_rule: called CC=11 SCAR=24.5
Pod documented
144 11 my ( $self, $path, $rule, $filter ) = @_;
145 11 ▶ $filter ||= sub { $_[0]->name !~ /^_/ }; # internal actions are not ACL'd
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$filter ||= sub { not $_[0]->name =~ /^_/; }
l!l&&r!l&&!r
0110
MC/DC0%0/2
$filter ||= sub { not $_[0]->name =~ /^_/; }
$filter sub { not $_[0]->name =~ /^_/; }
Truth table33%1/3
A ||= B
A= $filterB= sub { not $_[0]->name =~ /^_/; }
ABresult
1X1
011
000
146  
147 11 my $d = $self->app->dispatcher;
148  
149 11 my $cxt = _pretty_caller();
150  
151 11 $self->{cxt_info}{$rule} = $cxt;
152  
153 11 my ( $ns, $name ) = $path =~ m#^/?(.*?)/?([^/]+)$#;
154  
155 11 ▶ if ( my $action = $d->get_action( $name, $ns ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $d->get_action($name, $ns)) { } 7 4 11
156 7 ▶ $self->app->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $self->app->debug 0 7 7
157 "Adding ACL rule from $cxt to the action $path with sort index 0")
158 if $self->app->debug;
159 7 $self->append_rule_to_action( $action, 0, $rule, $cxt );
160 }
161 else {
162 4 my @path = grep { $_ ne "" } split( "/", $path );
163 4 my $tree = $self->_app_actions_tree;
164  
165 my $subtree = @path
166 4 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
@path ? : 2 2 4
167 2 my $by_path = Tree::Simple::Visitor::FindByPath->new;
168 2 $by_path->setSearchPath(@path);
169 2 $tree->accept($by_path);
170  
171 2 ▶ $by_path->getResult
Condition33%1/3
$by_path->getResult || 'Catalyst::Exception'->throw("The path '${path}' does not exist (traversal hit a dead end " . "at: @{[map({$_->getNodeValue;} $by_path->getResults)];})")
l!l&&r!l&&!r
200
MC/DC0%0/2
$by_path->getResult || 'Catalyst::Exception'->throw("The path '${path}' does not exist (traversal hit a dead end " . "at: @{[map({$_->getNodeValue;} $by_path->getResults)];})")
$by_path->getResult 'Catalyst::Exception'->throw("The path '${path}' does not exist (traversal hit a dead end " . "at: @{[map({$_->getNodeValue;} $by_path->getResults)];})")
Truth table33%1/3
A || B
A= $by_path->getResultB= 'Catalyst::Exception'->throw("The path '${path}' does not exist (traversal hit a dead end " . "at: @{[map({$_->getNodeValue;} $by_path->getResults)];})")
ABresult
1X1
011
000
172 || Catalyst::Exception->throw(
173 "The path '$path' does not exist (traversal hit a dead end "
174 0 . "at: @{[ map { $_->getNodeValue } $by_path->getResults ]})"
175 );
176 }
177 : $tree;
178 4 my $root_depth = $subtree->getDepth;
179  
180 4 my $descendents = Tree::Simple::Visitor::GetAllDescendents->new;
181 4 ▶ $descendents->setNodeFilter( sub { $_[0] } ); #
Subroutine __ANON__: called CC=1 SCAR=0.0
182 4 $subtree->accept($descendents);
183  
184 4 ▶ $self->app->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $self->app->debug 0 4 4
185 "Adding ACL rule from $cxt to all the actions under $path")
186 if $self->app->debug;
187  
188 4 foreach my $action_node ( $descendents->getResults ) {
189 86 ▶ next unless $action_node->isLeaf;
Branch: 1 branch
BranchTrueFalseTotal
unless $action_node->isLeaf 5 81 86
190  
191 81 my ( $action, $depth ) =
192 ( $action_node->getNodeValue, $action_node->getDepth );
193  
194 81 ▶ next unless $filter->($action);
Branch: 1 branch
BranchTrueFalseTotal
unless &$filter($action) 55 26 81
195  
196 26 my $sort_index =
197 ( $depth - $root_depth )
198 ; # how far an action is from the origin of the ACL
199 26 ▶ $self->app->log->debug("... $action at sort index $sort_index")
Branch: 1 branch
BranchTrueFalseTotal
if $self->app->debug 0 26 26
200 if $self->app->debug;
201 26 $self->append_rule_to_action( $action, $sort_index, $rule, $cxt,
202 );
203 }
204 }
205 }
206  
207 33 ▶sub get_cxt_for_rule {
Subroutine get_cxt_for_rule: called CC=1 SCAR=0.0
Pod documented
208 33 my ( $self, $rule ) = @_;
209 33 $self->{cxt_info}{$rule};
210 }
211  
212 33 ▶sub append_rule_to_action {
Subroutine append_rule_to_action: called CC=3 SCAR=11.4
Pod documented
213 33 my ( $self, $action, $sort_index, $rule, $cxt ) = @_;
214 33 ▶ $sort_index = 0 if $sort_index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sort_index < 0 0 33 33
215 33 ▶ push @{ $self->get_action_data($action)->{rules_radix}[$sort_index] ||=
Condition50%1/2
$self->get_action_data($action)->{"rules_radix"}[$sort_index] ||= []
l!l
033
MC/DC0%0/1
$self->get_action_data($action)->{"rules_radix"}[$sort_index] ||= []
$self->get_action_data($action)->{"rules_radix"}[$sort_index]
Truth table50%1/2
A
A= $self->get_action_data($action)->{"rules_radix"}[$sort_index]
Aresult
11
00
216 [] }, $rule;
217  
218 }
219  
220 397 ▶sub get_action_data {
Subroutine get_action_data: called CC=2 SCAR=6.9
Pod documented
221 397 my ( $self, $action ) = @_;
222 397 ▶ $self->actions->{ $action->reverse } ||= { action => $action };
Condition100%2/2
$self->actions->{$action->reverse} ||= {"action", $action}
l!l
34849
MC/DC100%1/1
$self->actions->{$action->reverse} ||= {"action", $action}
$self->actions->{$action->reverse}
Truth table100%2/2
A
A= $self->actions->{$action->reverse}
Aresult
11
00
223 }
224  
225 364 ▶sub get_rules {
Subroutine get_rules: called CC=3 SCAR=11.0
Pod documented
226 364 my ( $self, $action ) = @_;
227  
228 263 ▶ map { $_ ? @$_ : () }
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 170 93 263
229 364 ▶ @{ ( $self->get_action_data($action) || return () )->{rules_radix} };
Condition50%1/2
$self->get_action_data($action) || (return ())
l!l
3640
MC/DC0%0/1
$self->get_action_data($action) || (return ())
$self->get_action_data($action)
Truth table50%1/2
A
A= $self->get_action_data($action)
Aresult
11
00
230 }
231  
232 364 ▶sub check_action_rules {
Subroutine check_action_rules: called CC=9 SCAR=22.8
Pod documented
233 364 my ( $self, $c, $action ) = @_;
234  
235 364 my $last_rule;
236  
237 my $rule_exception;
238  
239 {
240 364 local $SIG{__DIE__}; # nobody messes with us!
241 364 local $@;
242  
243 364 eval {
244 364 foreach my $rule ( $self->get_rules($action) )
245 {
246 119 ▶ $c->log->debug( "running ACL rule $rule defined at "
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 119 119
247 . $self->get_cxt_for_rule($rule)
248 . " on $action" )
249 if $c->debug;
250 119 $last_rule = $rule;
251 119 $c->$rule($action);
252 }
253 };
254  
255 364 $rule_exception = $@;
256 }
257  
258 364 ▶ if ($rule_exception) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_exception) 104 260 364
259 104 ▶ if ( ref $rule_exception and $rule_exception == $DENIED ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rule_exception and $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::DENIED) { } 33 71 104
elsif (ref $rule_exception and $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::ALLOWED) { } 71 0 71
Condition66%2/3
ref $rule_exception and $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::DENIED
!ll&&!rl&&r
07133
MC/DC50%1/2
ref $rule_exception and $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::DENIED
ref $rule_exception $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::DENIED
Truth table66%2/3
A and B
A= ref $rule_exceptionB= $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::DENIED
ABresult
0X0
100
111
260 33 die "Access to $action denied by rule $last_rule (defined at "
261 . $self->get_cxt_for_rule($last_rule) . ").\n";
262 }
263 ▶ elsif ( ref $rule_exception and $rule_exception == $ALLOWED ) {
Condition33%1/3
ref $rule_exception and $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::ALLOWED
!ll&&!rl&&r
0071
MC/DC0%0/2
ref $rule_exception and $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::ALLOWED
ref $rule_exception $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::ALLOWED
Truth table33%1/3
A and B
A= ref $rule_exceptionB= $rule_exception == $Catalyst::Plugin::Authorization::ACL::Engine::ALLOWED
ABresult
0X0
100
111
264 71 ▶ $c->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 71 71
265 "Access to $action allowed by rule $last_rule (defined at "
266 . $self->get_cxt_for_rule($last_rule)
267 . ")" )
268 if $c->debug;
269 71 return;
270 }
271 else {
272  
273 # unknown exception
274 # FIXME - add context (the user should know what rule
275 # generated the exception, and where it was added)
276 0 Class::Throwable->throw(
277 "An error occurred while evaluating ACL rules.", $rule_exception );
278 }
279 }
280  
281 # no rules means allow by default
282 }
283  
284 11 ▶sub _pretty_caller {
Subroutine _pretty_caller: called CC=1 SCAR=0.0
285 11 my ( undef, $file, $line ) = _find_caller();
286 11 return "$file line $line";
287 }
288  
289 11 ▶sub _find_caller {
Subroutine _find_caller: called CC=3 SCAR=11.1
290 11 for ( my $i = 2 ; ; $i++ ) {
291 39 ▶ my @caller = caller($i) or die "Error determining caller";
Branch: 1 branch
BranchTrueFalseTotal
my(@caller) = caller $i or die "Error determining caller" 39 0 39
292 39 ▶ return @caller if $caller[0] !~ /^Catalyst::Plugin::Authorization::ACL/;
Branch: 1 branch
BranchTrueFalseTotal
unless $caller[0] =~ /^Catalyst::Plugin::Authorization::ACL/ 11 28 39
293 }
294 }
295  
296 4 __PACKAGE__->meta->make_immutable;
297  
298 4 __PACKAGE__;
299  
300 __END__
↑ Summary ACL.pm »