blib/lib/OpusVL/AppKit/Plugin/AppKit.pm

filter: Statementstmt 84.1% 270 / 321 Branchbran 69.8% 109 / 156 Conditioncond 82.1% 23 / 28 MC/DCmcdc 68.7% 11 / 16 Subroutinesub 91.4% 32 / 35 Podpod 69.2% 9 / 13 totaltotal 79.7% 454 / 569 CC 104 SCAR 25.4
CC
104
Cov
80%
_appkit_stash_searches
42.0
_appkit_stash_navigation
30.0
can_access
26.0
CRAP 12.7
« blib/lib/OpusVL/AppKit/RolesFor/Schema/DataInitialisation.pm ↑ Summary blib/lib/OpusVL/AppKit/RolesFor/Schema/AppKitAuthDB/Result/Role.pm »
1 package OpusVL::AppKit::Plugin::AppKit;
2  
3  
4 ###########################################################################################################################
5 # use lines.
6 ###########################################################################################################################
7 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Tree::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Tree::Simple::Visitor::FindByPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use OpusVL::AppKit::Plugin::AppKit::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use OpusVL::AppKit::Plugin::AppKit::FeatureList;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 with 'Catalyst::ClassData';
16  
17 ###########################################################################################################################
18 # moose calls.
19 ###########################################################################################################################
20  
21 12 has appkit_controllers => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1 );
22 sub _build_appkit_controllers
23 {
24 56 ▶ my ( $c ) = shift;
Subroutine _build_appkit_controllers: called CC=4 SCAR=13.9
25  
26 56 my @controllers;
27  
28 # Get all the components for this app... sorted by length of the name of the componant so they are in hierarchical order (bit hacky, but think it should work)
29 56 foreach my $comp ( sort { length($a) <=> length($b) } values %{ $c->components } )
30 {
31 # Check this is a controller for AppKit.... (not sure if we need to ignore others, but it just seems cleaner)..
32 2184 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->isa("Catalyst::Controller") and $comp->can("appkit")) 672 1512 2184
Condition100%3/3
$comp->isa("Catalyst::Controller") and $comp->can("appkit")
!ll&&!rl&&r
145656672
MC/DC100%2/2
$comp->isa("Catalyst::Controller") and $comp->can("appkit")
$comp->isa("Catalyst::Controller") $comp->can("appkit")
Truth table100%3/3
A and B
A= $comp->isa("Catalyst::Controller")B= $comp->can("appkit")
ABresult
0X0
100
111
33 ( $comp->isa('Catalyst::Controller') ) &&
34 ( $comp->can('appkit') )
35 )
36 {
37 672 push( @controllers, $comp );
38 }
39 }
40 56 return \@controllers;
41 }
42  
43 sub apps_allowed
44 {
45 51 ▶ my $self = shift;
Subroutine apps_allowed: called CC=2 SCAR=6.9
Pod documented
46 # return a sorted list of appkit controllers the user can use.
47 113 return sort { $a->appkit_order <=> $b->appkit_order }
48 51 ▶ grep { $_->home_action && $self->can_access($_->home_action->{actionpath}) } @{$self->appkit_controllers};
Branch: 1 branch
BranchTrueFalseTotal
$_->home_action and $self->can_access($_->home_action->{"actionpath"}) 153 459 612
49 }
50  
51 sub menu_data
52 {
53 # rip through the apps and construct an array of apps containing the
54 # group info too.
55 2 ▶ my $self = shift;
Subroutine menu_data: called CC=10 SCAR=23.0
Pod documented
56  
57 64 ▶ my @apps = sort { ($a->appkit_shared_module || '') cmp ($b->appkit_shared_module || '') || $b->appkit_order <=> $a->appkit_order }
Branch: 1 branch
BranchTrueFalseTotal
($a->appkit_shared_module || "") cmp ($b->appkit_shared_module || "") or $b->appkit_order <=> $a->appkit_order 38 26 64
Condition100%4/4
$a->appkit_shared_module || ''
l!l
1846
$b->appkit_shared_module || ''
l!l
1648
MC/DC100%1/1
$a->appkit_shared_module || ''
$a->appkit_shared_module
MC/DC100%1/1
$b->appkit_shared_module || ''
$b->appkit_shared_module
Truth table100%2/2
A
A= $a->appkit_shared_module
Aresult
11
00
Truth table100%2/2
A
A= $b->appkit_shared_module
Aresult
11
00
58 2 @{$self->appkit_controllers};
59 # now merge together the grouped controllers.
60 2 my $i = 0;
61 2 ▶ while($i + 1 < scalar @apps)
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < scalar @apps) 22 2 24
62 {
63 22 ▶ if($apps[$i]->appkit_shared_module && $apps[$i+1]->appkit_shared_module &&
Branch: 1 branch
BranchTrueFalseTotal
if ($apps[$i]->appkit_shared_module and $apps[$i + 1]->appkit_shared_module and $apps[$i]->appkit_shared_module eq $apps[$i + 1]->appkit_shared_module) { } 2 20 22
Condition83%5/6
$apps[$i]->appkit_shared_module and $apps[$i + 1]->appkit_shared_module and $apps[$i]->appkit_shared_module eq $apps[$i + 1]->appkit_shared_module
!ll&&!rl&&r
1822
$apps[$i]->appkit_shared_module and $apps[$i + 1]->appkit_shared_module
!ll&&!rl&&r
1804
MC/DC66%2/3
$apps[$i]->appkit_shared_module and $apps[$i + 1]->appkit_shared_module and $apps[$i]->appkit_shared_module eq $apps[$i + 1]->appkit_shared_module
$apps[$i]->appkit_shared_module $apps[$i + 1]->appkit_shared_module $apps[$i]->appkit_shared_module eq $apps[$i + 1]->appkit_shared_module
Truth table75%3/4
A and B and C
A= $apps[$i]->appkit_shared_moduleB= $apps[$i + 1]->appkit_shared_moduleC= $apps[$i]->appkit_shared_module eq $apps[$i + 1]->appkit_shared_module
ABCresult
0XX0
10X0
1100
1111
64 ($apps[$i]->appkit_shared_module eq $apps[$i+1]->appkit_shared_module))
65 {
66 2 splice @apps, $i + 1, 1;
67 }
68 else
69 {
70 20 $i++;
71 }
72 }
73 2 @apps = sort { $a->appkit_order <=> $b->appkit_order } @apps;
74 2 my $menu = [];
75  
76 2 for my $app (@apps)
77 {
78 22 my $actions = $app->application_action_list($self);
79 22 ▶ if(@$actions)
Branch: 1 branch
BranchTrueFalseTotal
if (@$actions) 1 21 22
80 {
81 1 push @$menu, { controller => $app, actions => $actions };
82 }
83 }
84 2 return $menu;
85 }
86  
87  
88 has appkit_actiontree_visitor => ( is => 'ro', isa => 'Tree::Simple::Visitor::FindByPath', default => sub
89 {
90 87 ▶ my $visitor = Tree::Simple::Visitor::FindByPath->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
91 87 ▶ $visitor->setNodeFilter( sub { my ($t) = @_; return $t->getNodeValue()->node_name } );
Subroutine __ANON__: called CC=1 SCAR=0.0
92 87 return $visitor;
93 12 } );
94  
95  
96 # FIXME: is there any real reason to keep this a property?
97 sub is_unrestricted_action_name
98 {
99 2159 ▶ my ($self, $name) = @_;
Subroutine is_unrestricted_action_name: called CC=12 SCAR=24.8
Pod documented
100 2159 ▶ return 1 if $name =~ /(^|\/)_/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)_] 862 1297 2159
101 1297 ▶ return 1 if $name =~ /(^|\/)begin$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)begin$] 14 1283 1297
102 1283 ▶ return 1 if $name =~ /(^|\/)end$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)end$] 102 1181 1283
103 1181 ▶ return 1 if $name =~ /(^|\/)auto$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)auto$] 240 941 1181
104 941 ▶ return 1 if $name =~ /(^|\/)default$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)default$] 12 929 941
105 929 ▶ return 1 if $name =~ /(^|\/)login$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)login$] 20 909 929
106 909 ▶ return 1 if $name =~ /(^|\/)logout$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)logout$] 6 903 909
107 903 ▶ return 1 if $name =~ /(^|\/)login\/not_required$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)login/not_required$] 19 884 903
108 884 ▶ return 1 if $name =~ /View\:\:/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /View\:\:/ 53 831 884
109 831 ▶ return 1 if $name =~ /(^|\/)access_denied$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)access_denied$] 15 816 831
110 816 ▶ return 1 if $name =~ /(^|\/)not_found$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[(^|/)not_found$] 9 807 816
111 807 return 0;
112 }
113  
114 ###########################################################################################################################
115 # catalyst hook.
116 ###########################################################################################################################
117  
118  
119 sub execute
120 {
121 904 ▶ my ( $c, $class, $action ) = @_;
Subroutine execute: called CC=8 SCAR=21.5
Pod documented
122  
123 $c->stash->{version} = eval '$' . $c->config->{name}. '::VERSION'
124 904 ▶ if $c->config->{appkit_display_app_version};
Branch: 1 branch
BranchTrueFalseTotal
if $c->config->{"appkit_display_app_version"} 904 0 904
125 # to check roles we need the plugin!
126 904 ▶ $c->isa("Catalyst::Plugin::Authorization::Roles") or die "Please use the Authorization::Roles plugin.";
Branch: 1 branch
BranchTrueFalseTotal
$c->isa("Catalyst::Plugin::Authorization::Roles") or die "Please use the Authorization::Roles plugin." 0 904 904
127  
128 904 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($action)) 904 0 904
129 Scalar::Util::blessed($action)
130 )
131 {
132 # ensure the user is logged in...
133 904 ▶ if ( $c->can_access( $action->reverse ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can_access($action->reverse)) { } 888 16 904
134 {
135 # do nothing..
136 888 ▶ $c->log->debug("************** AppKit - Allows Access to - " . $action->reverse ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 888 888
137 }
138 else
139 {
140 16 ▶ $c->log->debug("************** AppKit - DENIED Access to - " . $action->reverse ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 16 16
141 16 ▶ $c->detach_to_appkit_access_denied( $action ) if !$c->user;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->user 11 5 16
142  
143 5 $c->detach('/access_denied');
144 }
145 }
146 888 $c->maybe::next::method( $class, $action );
147 }
148  
149 ###########################################################################################################################
150 # plugin methods.
151 ###########################################################################################################################
152  
153 12 __PACKAGE__->mk_classdata('_appkit_features');
154 sub appkit_features
155 {
156 # NOTE: this property is setup when the appkit_actiontree is setup.
157 461 ▶ my $self = shift;
Subroutine appkit_features: called CC=2 SCAR=7.9
Pod documented
158 461 my $features = $self->_appkit_features;
159 461 ▶ unless($features)
Branch: 1 branch
BranchTrueFalseTotal
unless ($features) 0 461 461
160 {
161 0 my $tree = $self->appkit_actiontree(1);
162 0 $features = $self->_appkit_features;
163 }
164 461 return $features;
165 }
166  
167  
168 sub appkit_actiontree
169 {
170 395 ▶ my ($c, $rebuild) = @_;
Subroutine appkit_actiontree: called CC=4 SCAR=13.9
Pod documented
171  
172 # 'state' var means this will only be called once .. if Perl encounters this line again it knows not to run again..
173 395 state $appkit_actiontree = $c->_build_appkit_actiontree;
174 395 state $created = time;
175 # force a re-read of the tree.. (for example, if access control changes)...
176 395 ▶ if($rebuild || $c->_are_permissions_modified($created))
Branch: 1 branch
BranchTrueFalseTotal
if ($rebuild or $c->_are_permissions_modified($created)) 2 393 395
Condition66%2/3
$rebuild or $c->_are_permissions_modified($created)
l!l&&r!l&&!r
20393
MC/DC50%1/2
$rebuild or $c->_are_permissions_modified($created)
$rebuild $c->_are_permissions_modified($created)
Truth table66%2/3
A or B
A= $rebuildB= $c->_are_permissions_modified($created)
ABresult
1X1
011
000
177 {
178 2 $appkit_actiontree = $c->_build_appkit_actiontree;
179 2 $created = time;
180 2 ▶ if($rebuild)
Branch: 1 branch
BranchTrueFalseTotal
if ($rebuild) 2 0 2
181 {
182 2 $c->_set_permissions_modified($created);
183 }
184 }
185  
186 395 return $appkit_actiontree;
187 }
188  
189 sub _set_permissions_modified
190 {
191 2 ▶ my $c = shift;
Subroutine _set_permissions_modified: called CC=1 SCAR=0.0
192 2 my $updated = shift;
193 2 state $key = $c->config->{name} . 'permissions_modified';
194 2 $c->cache->set($key, $updated);
195 }
196  
197 sub _are_permissions_modified
198 {
199 393 ▶ my $c = shift;
Subroutine _are_permissions_modified: called CC=2 SCAR=6.9
200 393 my $updated = shift;
201  
202 393 state $key = $c->config->{name} . 'permissions_modified';
203 393 my $ts = $c->cache->get($key);
204 393 ▶ unless($ts)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ts) 316 77 393
205 {
206 316 $ts = 0;
207 316 $c->cache->set($key, $ts); # is this a good idea?
208 }
209 393 return $ts > $updated;
210 }
211  
212  
213 sub _build_appkit_actiontree
214 {
215 7 ▶ my ( $c ) = shift;
Subroutine _build_appkit_actiontree: called CC=16 SCAR=27.8
216  
217 # get the vistor..
218 7 my $visitor = $c->appkit_actiontree_visitor;
219  
220 # make a tree root (based on code from Catalyst::Plugin::Authorization::ACL::Engine)
221 7 my $root = Tree::Simple->new('AppKit', Tree::Simple->ROOT);
222 7 my $features = OpusVL::AppKit::Plugin::AppKit::FeatureList->new;
223  
224 7 AKCONTROLLERS: foreach my $cont ( @{ $c->appkit_controllers } )
225 {
226 # Loop through all this AppKit controllers actionmethods...
227 84 AKACTIONS: foreach my $action_method ( $cont->get_action_methods )
228 {
229 # skip internal type action names...
230 854 ▶ next if $c->is_unrestricted_action_name( $action_method->name );
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_unrestricted_action_name($action_method->name) 511 343 854
231  
232 343 my $action = $cont->action_for( $action_method->name );
233 343 ▶ next unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 0 343 343
234  
235 # Deal with path...
236 343 my $action_path = $action->reverse;
237 343 my @path = split '/', $action_path;
238 343 my $name = pop @path;
239  
240 # build AppKit::Node object...
241 my $appkit_action_object = OpusVL::AppKit::Plugin::AppKit::Node->new
242 (
243 node_name => $name,
244 action_path => $action_path,
245 action_attrs => $action->attributes,
246 access_only => [], # default to "no roles allowed"
247 in_feature => defined $action->attributes->{AppKitFeature},
248 343 );
249 343 $features->add_action($cont->appkit_name, $action);
250  
251 ## look for any ACL rules for this action_path...
252 343 ▶ if ( my $allowed_roles = $c->_allowed_roles_from_db( $action_path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed_roles = $c->_allowed_roles_from_db($action_path)) 323 20 343
253 {
254 323 $appkit_action_object->access_only( $allowed_roles );
255 }
256  
257 # If this is deeper than a top level action_path...
258 # See if we have already added it into the tree..
259 # ..if so, add a child to it and loop..
260 343 ▶ if (@path)
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 336 7 343
261 {
262  
263 336 $visitor->setResults; # clear any results.
264 336 $visitor->setSearchPath(@path);
265 336 $root->accept($visitor);
266  
267 336 ▶ if ( my $namespace_node = $visitor->getResult )
Branch: 1 branch
BranchTrueFalseTotal
if (my $namespace_node = $visitor->getResult) 266 70 336
268 {
269  
270 # final 'belt and braces' check to see if we have already added it..
271 266 foreach my $kid ( $namespace_node->getAllChildren )
272 {
273 1309 ▶ if ( $kid->getNodeValue->node_name eq $appkit_action_object->node_name )
Branch: 1 branch
BranchTrueFalseTotal
if ($kid->getNodeValue->node_name eq $appkit_action_object->node_name) 0 1309 1309
274 {
275 0 $c->debug->error("Action path $action_path, already in tree!!!??.. not a massive problem, but strange that it is happening");
276 next AKACTIONS
277 0 }
278 }
279  
280 # add a child to the already created tree node....
281 266 $namespace_node->addChild( Tree::Simple->new( $appkit_action_object ) );
282 266 next;
283 }
284 }
285  
286 # here we add all of the required path for the action we are adding to the tree..
287 77 my $node = $root;
288 77 for my $path_part ( @path )
289 {
290 119 my $found;
291 119 foreach my $kid ( @{ $node->getAllChildren } )
292 {
293 410 my $kidnode = $kid->getNodeValue;
294 410 ▶ if ( $path_part eq $kidnode->node_name )
Branch: 1 branch
BranchTrueFalseTotal
if ($path_part eq $kidnode->node_name) 42 368 410
295 {
296 42 $found = $kid;
297 }
298 }
299  
300 119 ▶ if ( defined $found )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) { } 42 77 119
301 {
302 42 $node = $found;
303 }
304 else
305 {
306  
307 # build AppKit::Node object...
308 77 my $branch_appkit_action_object = OpusVL::AppKit::Plugin::AppKit::Node->new
309 (
310 node_name => $path_part,
311 in_feature => 0,
312 );
313  
314 # add tree branch..
315 77 $node = Tree::Simple->new( $branch_appkit_action_object, $node);
316 }
317 }
318  
319 # add the child/action name to the node we have found/created..
320 77 $node->addChild( Tree::Simple->new( $appkit_action_object ) );
321 }
322 }
323 7 my $feature_list = $features->feature_names_with_app;
324 7 for my $feature (@$feature_list)
325 {
326 77 ▶ if( my $roles = $c->_allowed_feature_roles_from_db( $c, $feature ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $roles = $c->_allowed_feature_roles_from_db($c, $feature)) 75 2 77
327 {
328 75 $features->set_roles_allowed($feature, $roles);
329 }
330 }
331  
332 7 $c->_appkit_features($features);
333  
334 # finished :)
335 7 return $root;
336 }
337  
338 sub _allowed_feature_roles_from_db
339 {
340 77 ▶ my $self = shift;
Subroutine _allowed_feature_roles_from_db: called CC=3 SCAR=11.1
341 77 my $c = shift;
342 77 my $feature = shift;
343  
344 77 my $aclfeature = $c->model('AppKitAuthDB::Aclfeature')->find( { feature => $feature } );
345 # return undef if not match found..
346 77 ▶ return undef unless $aclfeature;
Branch: 1 branch
BranchTrueFalseTotal
unless $aclfeature 2 75 77
347  
348 75 ▶ $c->log->debug("AppKit Feature ACL : Matched Rule: " . $aclfeature->id . " FOR: $feature ") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 75 75
349  
350 #.. pull out all the allowed roles for this rule..
351 75 my $allowed_roles = [ map { $_->role } $aclfeature->roles ];
352  
353 # return array ref of roles..
354 75 return $allowed_roles;
355 }
356  
357  
358 sub can_access
359 {
360 1291 ▶ my $c = shift;
Subroutine can_access: called CC=21 SCAR=32.6
Pod documented
361 1291 my ($action_path) = @_;
362  
363 # did we get passed a path?...
364 1291 ▶ return undef unless defined $action_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action_path 0 1291 1291
365  
366 1291 ▶ if ( ref $action_path )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $action_path) 166 1125 1291
367 {
368 166 ▶ $c->log->debug("can_access called with a non-string action path: $action_path .. converting..") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 166 166
369 166 $action_path = $action_path->reverse;
370 }
371 1291 ▶ if ($c->is_unrestricted_action_name( $action_path ))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->is_unrestricted_action_name($action_path)) 831 460 1291
372 {
373 831 ▶ $c->log->debug("Unrestricted action name: $action_path") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 831 831
374 831 return 1
375 }
376  
377 # check if action path matches that of the 'access denied' action path.. in which case, we must allow access..
378 460 ▶ if ( $action_path eq $c->config->{'appkit_access_denied'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($action_path eq $c->config->{"appkit_access_denied"}) 0 460 460
379 {
380 0 ▶ $c->log->debug("Access denied path: $action_path") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
381 0 return 1
382 }
383  
384 # find this actions node in the tree ...
385 460 my $action_node = $c->_find_node_in_appkit_actiontree( $action_path );
386 460 ▶ if ( ! $action_node )
Branch: 1 branch
BranchTrueFalseTotal
unless ($action_node) 1 459 460
387 {
388 # NOTE: this should fix cache issues.
389 1 $c->appkit_actiontree(1);
390 1 $action_node = $c->_find_node_in_appkit_actiontree( $action_path );
391 1 ▶ unless($action_node)
Branch: 1 branch
BranchTrueFalseTotal
unless ($action_node) 1 0 1
392 {
393 1 $c->log->warn("Could not find ::Node in tree for: $action_path ");
394 1 return 0;
395 }
396 }
397  
398 # Have we been told to "NOT APPLY ACCESS CONTROL" ?? ...
399 459 ▶ if ( exists $action_node->action_attrs->{AppKitAllAccess} || exists $action_node->action_attrs->{Public} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $action_node->action_attrs->{"AppKitAllAccess"} or exists $action_node->action_attrs->{"Public"}) 2 457 459
Condition66%2/3
exists $action_node->action_attrs->{"AppKitAllAccess"} or exists $action_node->action_attrs->{"Public"}
l!l&&r!l&&!r
20457
MC/DC50%1/2
exists $action_node->action_attrs->{"AppKitAllAccess"} or exists $action_node->action_attrs->{"Public"}
exists $action_node->action_attrs->{"AppKitAllAccess"} exists $action_node->action_attrs->{"Public"}
Truth table66%2/3
A or B
A= exists $action_node->action_attrs->{"AppKitAllAccess"}B= exists $action_node->action_attrs->{"Public"}
ABresult
1X1
011
000
400 {
401 2 ▶ $c->log->debug("The following action has AppKitAllAccess / Public for $action_path . No access control being applied ") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 2 2
402 2 return 1;
403 }
404  
405 # check if we have list of actionpaths to allow (regardless of rules)...
406 457 ▶ if( $c->user )
Branch: 1 branch
BranchTrueFalseTotal
if ($c->user) 377 80 457
407 {
408 377 ▶ if ( $c->config->{'appkit_can_access_actionpaths'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($c->config->{"appkit_can_access_actionpaths"}) 377 0 377
409 {
410 377 foreach my $allowed_path ( @{ $c->config->{'appkit_can_access_actionpaths'} } )
411 {
412 # FIXME: this logic looks broken.
413 377 ▶ if($action_path eq $allowed_path)
Branch: 1 branch
BranchTrueFalseTotal
if ($action_path eq $allowed_path) 2 375 377
414 {
415 2 ▶ $c->log->debug("Hit an action path that's automatically allowed - $allowed_path") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 2 2
416 2 return 1
417 }
418 }
419 }
420  
421 # check if we have been told to allow everything...
422 375 ▶ if ( $c->config->{'appkit_can_access_everything'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($c->config->{"appkit_can_access_everything"}) 0 375 375
423 {
424 0 ▶ $c->log->debug("Allowing Access to EVERYTHING! - Turn off in the config if you do not want this!") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
425 0 return 1;
426 }
427 }
428  
429 # -- above here we see if we are blindly allowing access ---
430  
431 # find all allowed roles for this action path...
432 455 my $allowed_roles = $c->_allowed_roles_from_tree( $action_path );
433 455 my @allowed;
434 455 push @allowed, @$allowed_roles;
435 455 push @allowed, @{$c->appkit_features->roles_allowed_for_action( $action_path )};
436  
437 # if none found.. do NOT allow access..
438 455 ▶ unless (@allowed)
Branch: 1 branch
BranchTrueFalseTotal
unless (@allowed) 51 404 455
439 {
440 #$c->log->debug("************** can_access - DENIED Access to - " . $action_path ) if $c->debug;
441 51 return 0;
442 }
443  
444 # return a test that will check for the roles
445 404 my $allow = $c->check_roles(\@allowed);
446 #$c->log->debug("************** can_access - DENIED Access to - " . $action_path ) if !$allow && $c->debug;
447 404 return $allow;
448 }
449  
450 sub check_roles
451 {
452 404 ▶ my ($c, $allowed) = @_;
Subroutine check_roles: called CC=3 SCAR=11.0
Pod undocumented
453 404 ▶ my $allow = $c->user && $c->check_any_user_role( @$allowed )
Condition83%5/6
$c->user && $c->check_any_user_role(@$allowed) || 'PUBLIC' ~~ \@$allowed
l!l&&r!l&&!r
318086
$c->user and $c->check_any_user_role(@$allowed)
!ll&&!rl&&r
788318
MC/DC66%2/3
$c->user && $c->check_any_user_role(@$allowed) || 'PUBLIC' ~~ \@$allowed
$c->user $c->check_any_user_role(@$allowed) 'PUBLIC' ~~ \@$allowed
Truth table60%3/5
A and B || C
A= $c->userB= $c->check_any_user_role(@$allowed)C= 'PUBLIC' ~~ \@$allowed
ABCresult
11X1
0X11
1011
0X00
1000
454 || 'PUBLIC' ~~ @$allowed;
455 404 return $allow;
456 }
457  
458  
459 sub who_can_access
460 {
461 1 ▶ my $c = shift;
Subroutine who_can_access: called CC=2 SCAR=7.0
Pod documented
462 1 my ($action_path) = @_;
463  
464 # find all allowed roles for this action path...
465 1 my $allowed_roles = $c->_allowed_roles_from_tree( $action_path );
466 1 ▶ return undef unless defined $allowed_roles;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allowed_roles 0 1 1
467  
468 # get an resultset of user_id's...
469 1 my $inside_rs = $c->model('AppKitAuthDB::UsersRole')->search
470 (
471 {
472 'role.role' => { 'IN' => $allowed_roles },
473 },
474 {
475 select => ['users_id'],
476 as => ['users_id'],
477 join => ['role']
478 }
479 );
480  
481 # return all users with the roles..
482 1 return $c->model('AppKitAuthDB::User')->search(
483 { 'id' =>
484 { 'IN' => $inside_rs->get_column('users_id')->as_query } },
485 { distinct => 1 } );
486  
487 }
488  
489  
490  
491 12 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
492 12 memoize('_find_node_in_appkit_actiontree');
493  
494 sub _find_node_in_appkit_actiontree
495 {
496 388 ▶ my $c = shift;
Subroutine _find_node_in_appkit_actiontree: called CC=3 SCAR=11.0
497 388 my ($action_path) = @_;
498  
499 # get the important bits...
500 388 my $visitor = $c->appkit_actiontree_visitor;
501 388 my $root = $c->appkit_actiontree;
502  
503 # look for action path in the tree...
504 388 my @path = split '/', $action_path;
505 388 $visitor->setResults; # clear any results.
506 388 $visitor->setSearchPath(@path);
507 388 $root->accept($visitor);
508  
509 388 ▶ if ( my $node = $visitor->getResult )
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $visitor->getResult) 387 1 388
510 {
511 # got it, return it...
512 387 return $node->getNodeValue;
513 }
514  
515 # .. can't find it!
516 1 ▶ $c->log->debug("AppKit ACL : Could not find node for: " . $action_path ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
517 1 return undef;
518 }
519  
520  
521 sub _allowed_roles_from_tree
522 {
523 456 ▶ my $c = shift;
Subroutine _allowed_roles_from_tree: called CC=2 SCAR=7.0
524 456 my ($action_path) = @_;
525  
526 # find the node in the tree..
527 456 my $node = $c->_find_node_in_appkit_actiontree( $action_path );
528  
529 456 ▶ return [] if ( ! defined $node );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 456 456
530  
531 #.. pull out the array ref of roles the allowed for this node (action path)..
532 456 my $roles = $node->access_only;
533  
534 456 return $node->access_only;
535 }
536  
537  
538 sub _allowed_roles_from_db
539 {
540 343 ▶ my $c = shift;
Subroutine _allowed_roles_from_db: called CC=5 SCAR=16.2
541 343 my ($action_path) = @_;
542  
543 # did we get passed a path?...
544 343 ▶ return undef unless defined $action_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action_path 0 343 343
545  
546 343 my @action_path = grep { $_ ne "" } split( "/", $action_path );
547 343 my $action_name = pop @action_path;
548  
549 # always allow to method starting with underscore (_) ... typically they are internal methods..
550 343 ▶ return undef if $action_name =~ /^_/;
Branch: 1 branch
BranchTrueFalseTotal
if $action_name =~ /^_/ 0 343 343
551  
552 # we are looking for an exact match for this action path..
553 343 my $aclrule = $c->model('AppKitAuthDB::Aclrule')->find( { actionpath => $action_path } );
554  
555 # return undef if not match found..
556 343 ▶ return undef unless $aclrule;
Branch: 1 branch
BranchTrueFalseTotal
unless $aclrule 20 323 343
557  
558 323 ▶ $c->log->debug("AppKit ACL : Matched Rule: " . $aclrule->id . " FOR: $action_path ") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 323 323
559  
560 #.. pull out all the allowed roles for this rule..
561 323 my $allowed_roles = [ map { $_->role } $aclrule->roles ];
562  
563 # return array ref of roles..
564 323 return $allowed_roles;
565 }
566  
567  
568 sub _appkit_stash_portlets
569 {
570 4 ▶ my ( $c ) = @_;
Subroutine _appkit_stash_portlets: called CC=7 SCAR=24.7
571 4 my @portlets;
572 4 foreach my $apc ( @{ $c->appkit_controllers } )
573 {
574 48 ▶ next unless $apc->portlet_actions;
Branch: 1 branch
BranchTrueFalseTotal
unless $apc->portlet_actions 0 48 48
575  
576 48 ▶ $c->log->debug("AppKit - RREALLY LOOKING FOR PORTLETS IN : " . $apc ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 48 48
577  
578 48 foreach my $portlet ( @{ $apc->portlet_actions } )
579 {
580 4 my $portlet_action = $apc->action_for( $portlet->{actionname} );
581  
582 # dont stash if we can't access it..
583 4 ▶ next unless $c->can_access( $portlet_action->reverse );
Branch: 1 branch
BranchTrueFalseTotal
unless $c->can_access($portlet_action->reverse) 4 0 4
584  
585 # forward to the portlet action..
586 {
587 0 local $c->stash->{breadcrumbs};
588 0 local $c->stash->{output_type} = 'plain';
589 0 $c->visit( $portlet_action );
590 }
591  
592 # take things from the stash (that the action should have just filled out)
593 push
594 (
595 @portlets,
596 {
597 name => $portlet->{value},
598 0 ▶ html => $c->res->body,
Branch: 1 branch
BranchTrueFalseTotal
if $c->res->status == 200 0 0 0
599 }
600 ) if($c->res->status == 200);
601 0 $c->res->status(200);
602 0 $c->res->body(undef);
603 }
604 }
605 4 $c->stash->{portlets} = \@portlets;
606 }
607  
608  
609 sub _appkit_stash_searches
610 {
611 0 ▶ my ( $c, $q ) = @_;
Subroutine _appkit_stash_searches: not called CC=6 SCAR=37.4
612  
613 0 my @search_results;
614 0 foreach my $apc ( @{ $c->appkit_controllers } )
615 {
616 0 ▶ next unless $apc->search_actions;
Branch: 1 branch
BranchTrueFalseTotal
unless $apc->search_actions 0 0 0
617 0 foreach my $search ( @{ $apc->search_actions } )
618 {
619 0 my $search_action = $apc->action_for( $search->{actionname} );
620  
621 # dont stash if we can't access it..
622 0 ▶ next unless $c->can_access( $search_action->reverse );
Branch: 1 branch
BranchTrueFalseTotal
unless $c->can_access($search_action->reverse) 0 0 0
623  
624 # run the search action..
625 {
626 0 local $c->stash->{breadcrumbs};
627 0 local $c->stash->{output_type} = 'plain';
628 0 undef $c->stash->{search_results};
629
630 # Stop the default end action from rendering a view
631 0 $c->res->body('do_not_render');
632 0 $c->visit( $search_action, [], [$q] );
633 0 $c->res->body(undef);
634 }
635  
636 # take things from the stash (that the action should have just filled out)
637 0 ▶ if ($c->stash->{search_results})
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash->{"search_results"}) 0 0 0
638 {
639 push @search_results,
640 {
641 name => $search->{value},
642 results => $c->stash->{search_results},
643 0 };
644 }
645 }
646 }
647  
648 0 $c->stash->{search_results} = \@search_results;
649 }
650  
651  
652 sub _appkit_stash_navigation
653 {
654 0 ▶ my ( $c ) = @_;
Subroutine _appkit_stash_navigation: not called CC=5 SCAR=34.0
655 0 my @navigations;
656 0 foreach my $apc ( @{ $c->appkit_controllers } )
657 {
658 0 ▶ next unless $apc->navigation_actions;
Branch: 1 branch
BranchTrueFalseTotal
unless $apc->navigation_actions 0 0 0
659 0 foreach my $nav ( @{ $apc->navigation_actions } )
660 {
661 0 my $nav_action = $apc->action_for( $nav->{actionname} );
662  
663 # test we can access this action..
664 0 ▶ next unless $c->can_access( $nav_action->reverse );
Branch: 1 branch
BranchTrueFalseTotal
unless $c->can_access($nav_action->reverse) 0 0 0
665  
666 # build an array of navigation information...
667 push
668 (
669 @navigations,
670 {
671 text => $nav->{value},
672 0 uri => $c->uri_for( $nav_action ),
673 }
674 );
675 }
676 }
677 0 $c->stash->{navigation} = \@navigations;
678 }
679  
680 sub REST_403
681 {
682 2 ▶ my ($c) = @_;
Subroutine REST_403: called CC=1 SCAR=0.0
Pod undocumented
683 2 $c->response->status(403);
684 2 $c->stash->{rest} = { message => 'Access Denied' };
685 2 $c->detach;
686 }
687  
688 sub in_REST_action
689 {
690 16 ▶ my ($c) = @_;
Subroutine in_REST_action: called CC=2 SCAR=7.1
Pod undocumented
691  
692 16 ▶ return $c->action && $c->action->isa('Catalyst::Action::REST');
Condition66%2/3
$c->action && $c->action->isa('Catalyst::Action::REST')
!ll&&!rl&&r
0142
MC/DC50%1/2
$c->action && $c->action->isa('Catalyst::Action::REST')
$c->action $c->action->isa('Catalyst::Action::REST')
Truth table66%2/3
A && B
A= $c->actionB= $c->action->isa('Catalyst::Action::REST')
ABresult
0X0
100
111
693 }
694  
695 sub detach_to_appkit_access_denied
696 {
697 11 ▶ my ( $c, $denied_access_to_action ) = @_;
Subroutine detach_to_appkit_access_denied: called CC=5 SCAR=16.6
Pod undocumented
698  
699 11 ▶ if($c->in_REST_action)
Branch: 1 branch
BranchTrueFalseTotal
if ($c->in_REST_action) 1 10 11
700 {
701 1 ▶ $c->log->debug("AppKit - Not Allowed Access to " . $denied_access_to_action->reverse . " - part of REST controller so sending plain 403.") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
702 1 $c->REST_403;
703 }
704  
705 10 my $access_denied_action_path = $c->config->{'appkit_access_denied'};
706 10 ▶ $c->log->debug("AppKit - Not Allowed Access to " . $denied_access_to_action->reverse . " - Detaching to $access_denied_action_path ") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 10 10
707 10 my $message = "Access denied - Please login with an account that has permissions to access the requested area";
708 $message = $c->config->{AppKit}->{login_message}
709 10 ▶ if exists $c->config->{AppKit}->{login_message};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->config->{"AppKit"}{"login_message"} 0 10 10
710 10 $c->controller('Login')->login_redirect($c, $message);
711 0 $c->detach();
712 }
713  
714  
715 sub generate_class_name
716 {
717 0 ▶ my $self = shift;
Subroutine generate_class_name: not called CC=1 SCAR=6.9
Pod documented
718 0 my $var = shift;
719 0 $var =~ s/\W+//; # remove all non word chars
720 0 return lc $var;
721 }
722  
723 __END__
724  
725 =pod
726  
727 =encoding UTF-8
728  
729 =head1 NAME
730  
731 OpusVL::AppKit::Plugin::AppKit
732  
733 =head1 VERSION
734  
735 version 2.24
736  
737 =head1 DESCRIPTION
738  
739 People not developing the actual AppKit should not really need to know much about this plugin.
740  
741 It is used by the OpusVL::AppKit which intended to be inherited by another Catalyst App using
742 CatalystX::AppBuilder..
743  
744 =head1 NAME
745  
746 OpusVL::AppKit::Plugin::AppKit - Common functions to get OpusVL::AppKit working.
747  
748 =head1 METHODS
749  
750 =head2 apps_allowed
751  
752 Returns a list of the appkit controllers the user has access to sorted as the app config
753 specifies. Generally used for building up the menu.
754  
755 =head2 menu_data
756  
757 List of apps and the group data for the full blown menu
758  
759 [
760 {
761 'controller' => bless( {
762 'appkit_method_group' => 'Leads',
763 'appkit_name' => 'Customers',
764 }, 'Aquarius::Controller::Leads' ),
765 'actions' => [
766 {
767 'actions' => [
768 {
769 'controller' => $controller,
770 'actionname' => 'index',
771 'value' => 'PO Approvers',
772 'sort_index' => 1,
773 'actionpath' => 'accounts/auth/index'
774 },
775 {
776 'controller' => $controller,
777 'actionname' => 'index',
778 'value' => 'Next Item',
779 'sort_index' => 1,
780 'actionpath' => 'accounts/auth/index'
781 },
782 $VAR1->[4]{'controller'}{'navigation_actions'}[3],
783 $VAR1->[4]{'controller'}{'navigation_actions'}[5],
784 $VAR1->[4]{'controller'}{'navigation_actions'}[4]
785 ],
786 'group' => 'Leads'
787 },
788 {
789 'actions' => [
790 $VAR1->[4]{'controller'}{'navigation_actions'}[0],
791 $VAR1->[4]{'controller'}{'navigation_actions'}[2],
792 $VAR1->[4]{'controller'}{'navigation_actions'}[1]
793 ],
794 'group' => 'Customers'
795 }
796 ]
797 }
798 ];
799  
800 =head2 appkit_actiontree_visitor
801 Use for find node in the appkit_actiontree...
802  
803 =head2 is_unrestricted_action_name
804 Little helper to ascertain if an action's name is one we dont apply access control to.
805  
806 =head2 execute
807 The method hooks into the catalyst despatch path.
808 If the current logged in used is denied access to the action this will detach to the 'access_denied' action.
809  
810 =head2 appkit_features
811  
812 Returns a OpusVL::AppKit::Plugin::AppKit::FeatureList object that allows the querying of the features permissions
813 that sit on top of our roles management.
814  
815 =head2 appkit_actiontree
816 This returns a Tree::Simple of OpusVL::AppKit::Plugin::AppKit::Node's.
817 Based on code from Catalyst::Plugin::Authorization::ACL::Engine, it is basically a Tree of this apps actions.
818 This attribute is used to define access to actions.(but could be used for many more things)
819  
820 Arguments:
821 $_[0] - Self
822 $_[1] - Optional flag to say "re-read the tree"
823  
824 =head2 _build_appkit_actiontree
825 internal only method that supports the appkit_actiontree routine.
826 This basically builds a tree of actions/controllers in the current catalyst app.
827  
828 =head2 can_access
829 Checks the ACL structure against the current user and action.
830 return 1 or 0 depending on if the user can access the action_path
831  
832 Used like so:
833 if ( $c->can_access( 'controller/action/path' ) )
834 {
835 # $c->user must have the correct roles.
836 }
837  
838 THIS NEEDS A MASSIVE CLEAN UP ONCE WE ARE HAPPY APPKIT WORKS WELL.
839  
840 =head2 who_can_access
841 Checks the ACL structure to see who can access a passed action_path.
842 Returns:
843 undef - if not allowed roles
844 resultset - of users that can access the otherwise returns a resultset
845  
846 =head2 _find_node_in_appkit_actiontree
847 Returns OpusVL::AppKit::Plugin::AppKit::Node that represents the action_path.
848 .. or undef if not found.
849  
850 =head2 _allowed_roles_from_tree
851 Returns ArrayRef of roles that can access the passed action path.
852 This checks the 'appkit_actiontree' .. so should be quick.
853  
854 =head2 _allowed_roles_from_db
855 Returns ArrayRef of roles that can access the passed action path.
856 This checks the database (model)
857  
858 =head2 _appkit_stash_portlets
859 Put all the AppKit Controller Portlets data in the stash
860 If you forward to this action, you should end up with a stash value keyed by 'portlets'.
861 eg. $c->forward('stash_portlets');
862 The value of 'portlets' is an ArrayRef of HashRefs.
863 The HashRefs contain 2 keys:
864 name = The name of the portlet
865 html = The HTML content of the portlet
866  
867 =head2 _appkit_stash_navigation
868 Put all the AppKit Controller Navigation data in the stash
869 If you forward to this action, you should end up with a stash value keyed by 'navigation'.
870 eg. $c->forward('stash_navigation');
871 The value of 'navigation' is an ArrayRef of HashRefs.
872 The HashRefs contain 2 keys:
873 text = The text of the navigation item
874 uri = The uri if the action the navigation relates to.
875  
876 =head2 generate_class_name
877  
878 A simple utility function to turn a string into a css class by removing whitespace and non word characters, then lower casing it.
879  
880 This should provide a way to get predictable and legitimate css class names from data.
881  
882 =head1 AUTHOR
883  
884 OpusVL - www.opusvl.com
885  
886 =head1 COPYRIGHT AND LICENSE
887  
888 This software is copyright (c) 2010 by OpusVL - www.opusvl.com.
889  
890 This is free software; you can redistribute it and/or modify it under
891 the same terms as the Perl 5 programming language system itself.
892  
893 =cut
« blib/lib/OpusVL/AppKit/RolesFor/Schema/DataInitialisation.pm ↑ Summary blib/lib/OpusVL/AppKit/RolesFor/Schema/AppKitAuthDB/Result/Role.pm »