Jifty/API.pm

filter: Statementstmt 12.5% 13 / 104 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 15.7% 3 / 19 Podpod 100% 16 / 16 totaltotal 15.9% 32 / 201 CC 29 SCAR 38.0
CC
29
Cov
8%
restrict
90.0
explain
72.0
decide_action_polarity
56.0
CRAP 44.5
« Jifty/Test.pm ↑ Summary Jifty/TestServer.pm »
1 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::API;
5  
6 =head1 NAME
7  
8 Jifty::API - Manages and allow reflection on the Jifty::Actions that
9 make up a Jifty application's API
10  
11 =head1 SYNOPSIS
12  
13 # Find the full name of an action
14 my $class = Jifty->api->qualify('SomeAction');
15  
16 # New users cannot run some actions
17 if (Jifty->web->current_user->age < 18) {
18 Jifty->api->deny(qr/Vote|PurchaseTobacco/);
19 }
20  
21 # Some users cannot even see some actions
22 if (Jifty->web->current_user->id > 10) {
23 Jifty->api->hide('Foo');
24 Jifty->api->show('FooBar');
25 Jifty->api->hide('FooBarDeleteTheWorld');
26 }
27  
28 # Fetch the class names of all actions
29 my @actions = Jifty->api->all_actions;
30  
31 # Fetch the class names of all the allowed actions
32 my @allowed = Jifty->api->actions;
33  
34 # Fetch all of the visible actions (some of which may not be allowed)
35 my @visible = Jifty->api->visible_actions;
36  
37 # Check to see if an action is allowed
38 if (Jifty->api->is_allowed('TrueFooBar')) {
39 # do something...
40 }
41  
42 # Check to see if an action is visible
43 if (Jifty->api->is_visible('SpamOurUsers')) {
44 SpamBot->be_annoying;
45 }
46  
47 # Undo all allow/deny/restrict/hide calls
48 Jifty->api->reset;
49  
50 =head1 DESCRIPTION
51  
52 You can fetch an instance of this class by calling L<Jifty/api> in
53 your application. This object can be used to examine the actions
54 available within your application and manage access to those actions.
55  
56 =cut
57  
58  
59 113 ▶use base qw/Class::Accessor::Fast Jifty::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61  
62 113 __PACKAGE__->mk_accessors(qw(action_limits));
63  
64 =head1 METHODS
65  
66 =head2 new
67  
68 Creates a new C<Jifty::API> object.
69  
70 Don't use this, see L<Jifty/api> to access a reference to
71 C<Jifty::API> in your application.
72  
73 =cut
74  
75 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
76 0 my $class = shift;
77 0 my $self = bless {}, $class;
78  
79 # Setup the basic allow/deny rules
80 0 $self->reset;
81  
82 # Find all the actions for the API reference (available at __actions)
83 Jifty::Module::Pluggable->import(
84 search_path => [
85 Jifty->app_class("Action"),
86 "Jifty::Action",
87 0 map {ref($_)."::Action"} Jifty->plugins,
88 ],
89 except => qr/\.#/,
90 sub_name => "__actions"
91 );
92  
93 0 return ($self);
94 }
95  
96 =head2 qualify ACTIONNAME
97  
98 Returns the fully qualified package name for the given provided
99 action. If the C<ACTIONNAME> starts with C<Jifty::> or
100 C<ApplicationClass::Action>, simply returns the given name; otherwise,
101 it prefixes it with the C<ApplicationClass::Action>.
102  
103 =cut
104  
105 0 ▶sub qualify {
Subroutine qualify: not called CC=3 SCAR=24.8
Pod documented
106 0 my $self = shift;
107 0 my $action = shift;
108  
109 # Get the application class name
110 0 my $base_path = Jifty->config->framework('ApplicationClass');
111  
112 # Return the class now if it's already fully qualified
113 0 ▶ return $action
Branch: 1 branch
BranchTrueFalseTotal
if $action =~ /^Jifty::/ or $action =~ /^\Q$base_path\E::/ 0 0 0
Condition0%0/3
$action =~ /^Jifty::/ or $action =~ /^\Q$base_path\E::/
l!l&&r!l&&!r
000
MC/DC0%0/2
$action =~ /^Jifty::/ or $action =~ /^\Q$base_path\E::/
$action =~ /^Jifty::/ $action =~ /^\Q$base_path\E::/
Truth table0%0/3
A or B
A= $action =~ /^Jifty::/B= $action =~ /^\Q$base_path\E::/
ABresult
1X1
011
000
114 if ($action =~ /^Jifty::/
115 or $action =~ /^\Q$base_path\E::/);
116  
117 # Otherwise qualify it
118 0 return $base_path . "::Action::" . $action;
119 }
120  
121 =head2 reset
122  
123 Resets which actions are allowed to the defaults; that is, all of the
124 application's actions, L<Jifty::Action::AboutMe>,
125 L<Jifty::Action::Autocomplete>, and L<Jifty::Action::Redirect> are allowed and
126 visible; everything else is denied and hidden. See L</restrict> for the details
127 of how limits are processed.
128  
129 =cut
130  
131 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
132 0 my $self = shift;
133  
134 # Set up defaults
135 0 my $app_actions = Jifty->app_class("Action");
136  
137 # These are the default action limits
138 0 $self->action_limits(
139 [
140 { deny => 1, hide => 1, restriction => qr/.*/ },
141 { allow => 1, show => 1, restriction => qr/^\Q$app_actions\E/ },
142 { deny => 1, hide => 1, restriction => qr/^\Q$app_actions\E::Record::(Create|Delete|Execute|Search|Update)$/ },
143 { allow => 1, show => 1, restriction => 'Jifty::Action::AboutMe' },
144 { allow => 1, show => 1, restriction => 'Jifty::Action::Autocomplete' },
145 { allow => 1, show => 1, restriction => 'Jifty::Action::Redirect' },
146 ]
147 );
148 }
149  
150 =head2 deny_for_get
151  
152 Denies all actions except L<Jifty::Action::AboutMe>,
153 L<Jifty::Action::Autocomplete> and L<Jifty::Action::Redirect>. This is to
154 protect against a common cross-site scripting hole. In your C<before>
155 dispatcher rules, you can whitelist actions that are known to be read-only.
156  
157 This is called automatically during any C<GET> request.
158  
159 =cut
160  
161 0 ▶sub deny_for_get {
Subroutine deny_for_get: not called CC=1 SCAR=6.9
Pod documented
162 0 my $self = shift;
163 0 $self->deny(qr/.*/);
164 0 $self->allow("Jifty::Action::AboutMe");
165 0 $self->allow("Jifty::Action::Autocomplete");
166 0 $self->allow("Jifty::Action::Redirect");
167 }
168  
169 =head2 allow RESTRICTIONS
170  
171 Takes a list of strings or regular expressions, and adds them in order
172 to the list of limits for the purposes of L</is_allowed>. See
173 L</restrict> for the details of how limits are processed.
174  
175 Allowing actions also L</show> them.
176  
177 =cut
178  
179 0 ▶sub allow {
Subroutine allow: not called CC=1 SCAR=6.9
Pod documented
180 0 my $self = shift;
181 0 $self->restrict( allow => @_ );
182 }
183  
184 =head2 deny RESTRICTIONS
185  
186 Takes a list of strings or regular expressions, and adds them in order
187 to the list of limits for the purposes of L</is_allowed>. See
188 L</restrict> for the details of how limits are processed.
189  
190 =cut
191  
192 0 ▶sub deny {
Subroutine deny: not called CC=1 SCAR=6.9
Pod documented
193 0 my $self = shift;
194 0 $self->restrict( deny => @_ );
195 }
196  
197 =head2 hide RESTRICTIONS
198  
199 Takes a list of strings or regular expressions, and adds them in order
200 to the list of limits for the purposes of L</is_visible>. See
201 L</restrict> for the details of how limits are processed.
202  
203 Hiding actions also L</deny> them.
204  
205 =cut
206  
207 0 ▶sub hide {
Subroutine hide: not called CC=1 SCAR=6.9
Pod documented
208 0 my $self = shift;
209 0 $self->restrict( hide => @_ );
210 }
211  
212 =head2 show RESTRICTIONS
213  
214 Takes a list of strings or regular expressions, and adds them in order
215 to the list of limits for the purposes of L</is_visible>. See
216 L</restrict> for the details of how limits are processed.
217  
218 =cut
219  
220 0 ▶sub show {
Subroutine show: not called CC=1 SCAR=6.9
Pod documented
221 0 my $self = shift;
222 0 $self->restrict( show => @_ );
223 }
224  
225 =head2 restrict POLARITY RESTRICTIONS
226  
227 Method that L</allow>, L</deny>, L</hide>, and L</show> call internally;
228 I<POLARITY> is one of C<allow>, C<deny>, C<hide>, or C<show>. Limits are
229 evaluated in the order they're called. The last limit that applies will be the
230 one which takes effect. Regexes are matched against the class; strings are
231 fully L<qualified|/qualify> and used as an exact match against the class name.
232 The base set of restrictions (which is reset every request) is set in
233 L</reset>, and usually modified by the application's L<Jifty::Dispatcher> if
234 need be.
235  
236 If you call:
237  
238 Jifty->api->deny ( qr'Foo' );
239 Jifty->api->allow ( qr'FooBar' );
240 Jifty->api->deny ( qr'FooBarDeleteTheWorld' );
241  
242 ..then:
243  
244 calls to MyApp::Action::Baz will succeed.
245 calls to MyApp::Action::Foo will fail.
246 calls to MyApp::Action::FooBar will pass.
247 calls to MyApp::Action::TrueFoo will fail.
248 calls to MyApp::Action::TrueFooBar will pass.
249 calls to MyApp::Action::TrueFooBarDeleteTheWorld will fail.
250 calls to MyApp::Action::FooBarDeleteTheWorld will fail.
251  
252 =cut
253  
254 113 my %valid_polarity = map { $_ => 1 } qw/allow deny hide show/;
255  
256 0 ▶sub restrict {
Subroutine restrict: not called CC=9 SCAR=45.0
Pod documented
257 0 my $self = shift;
258 0 my $polarity = shift;
259 0 my @restrictions = @_;
260  
261 0 my(undef, $file, $line) = (caller(1));
262  
263 # Check the sanity of the polarity
264 die "Polarity must be one of: " . join(', ', sort keys %valid_polarity)
265 0 ▶ unless $valid_polarity{$polarity};
Branch: 1 branch
BranchTrueFalseTotal
unless $valid_polarity{$polarity} 0 0 0
266  
267 0 for my $restriction (@restrictions) {
268  
269 # Don't let the user "allow .*"
270 0 ▶ die "For security reasons, Jifty won't let you allow all actions"
Branch: 1 branch
BranchTrueFalseTotal
if $polarity eq "allow" and ref $restriction and $restriction =~ /^\(\?\^?[-xismadlu]*:\^?\.\*\$?\)$/ 0 0 0
Condition0%0/6
$polarity eq "allow" and ref $restriction and $restriction =~ /^\(\?\^?[-xismadlu]*:\^?\.\*\$?\)$/
!ll&&!rl&&r
000
$polarity eq "allow" and ref $restriction
!ll&&!rl&&r
000
MC/DC0%0/3
$polarity eq "allow" and ref $restriction and $restriction =~ /^\(\?\^?[-xismadlu]*:\^?\.\*\$?\)$/
$polarity eq "allow" ref $restriction $restriction =~ /^\(\?\^?[-xismadlu]*:\^?\.\*\$?\)$/
Truth table0%0/4
A and B and C
A= $polarity eq "allow"B= ref $restrictionC= $restriction =~ /^\(\?\^?[-xismadlu]*:\^?\.\*\$?\)$/
ABCresult
0XX0
10X0
1100
1111
271 if $polarity eq "allow"
272 and ref $restriction
273 and $restriction =~ /^\(\?\^?[-xismadlu]*:\^?\.\*\$?\)$/;
274  
275 # Fully qualify it if it's a string
276 0 ▶ $restriction = $self->qualify($restriction)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $restriction 0 0 0
277 unless ref $restriction;
278  
279  
280 0 ▶ if ($polarity eq 'hide') {
Branch: 2 branches
BranchTrueFalseTotal
if ($polarity eq 'hide') { } 0 0 0
elsif ($polarity eq 'allow') { } 0 0 0
281 # Hiding an action also denies it
282 0 push @{ $self->action_limits },
283 { deny => 1, hide => 1, restriction => $restriction, from => "$file:$line" };
284 } elsif ($polarity eq 'allow') {
285 # Allowing an action also shows it
286 0 push @{ $self->action_limits },
287 { allow => 1, show => 1, restriction => $restriction, from => "$file:$line" };
288 } else {
289 # Otherwise, add to list of restrictions unmodified
290 0 push @{ $self->action_limits },
291 { $polarity => 1, restriction => $restriction, from => "$file:$line" };
292 }
293 }
294 }
295  
296 =head2 is_allowed CLASS
297  
298 Returns true if the I<CLASS> name (which is fully qualified if it is
299 not already) is allowed to be executed. See L</restrict> above for
300 the rules that the class name must pass.
301  
302 =cut
303  
304 0 ▶sub is_allowed {
Subroutine is_allowed: not called CC=1 SCAR=6.9
Pod documented
305 0 my $self = shift;
306 0 my $action = shift;
307  
308 0 $self->decide_action_polarity($action, 'allow', 'deny');
309 }
310  
311 =head2 is_visible CLASS
312  
313 Returns true if the I<CLASS> name (which is fully qualified if it is
314 not already) is allowed to be seen. See L</restrict> above for
315 the rules that the class name must pass.
316  
317 =cut
318  
319 0 ▶sub is_visible {
Subroutine is_visible: not called CC=1 SCAR=6.9
Pod documented
320 0 my $self = shift;
321 0 my $action = shift;
322  
323 0 $self->decide_action_polarity($action, 'show', 'hide');
324 }
325  
326 =head2 decide_action_polarity CLASS, ALLOW, DENY
327  
328 Returns true if the I<CLASS> name it has the ALLOW restriction, false if it has
329 the DENY restriction. This is a helper method used by L</is_allowed> and
330 L</is_visible>.
331  
332 If no restrictions apply to this action, then false will be returned.
333  
334 =cut
335  
336 0 ▶sub decide_action_polarity {
Subroutine decide_action_polarity: not called CC=7 SCAR=40.3
Pod documented
337 0 my $self = shift;
338 0 my $class = shift;
339 0 my $allow = shift;
340 0 my $deny = shift;
341  
342 # Qualify the action
343 0 $class = $self->qualify($class);
344  
345 # Assume that it doesn't pass; however, the real fallbacks are
346 # controlled by L</reset>, above.
347 0 my $valid = 0;
348  
349 # Walk all of the limits
350 0 for my $limit ( @{ $self->action_limits } ) {
351  
352 # Regexes are =~ matches, strigns are eq matches
353 0 ▶ if ( ( ref $limit->{restriction} and $class =~ $limit->{restriction} )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/ or $class eq $limit->{"restriction"}) 0 0 0
Condition0%0/6
ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/ or $class eq $limit->{"restriction"}
l!l&&r!l&&!r
000
ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/
!ll&&!rl&&r
000
MC/DC0%0/3
ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/ or $class eq $limit->{"restriction"}
ref $limit->{"restriction"} $class =~ /$limit->{"restriction"}/ $class eq $limit->{"restriction"}
Truth table0%0/5
A and B or C
A= ref $limit->{"restriction"}B= $class =~ /$limit->{"restriction"}/C= $class eq $limit->{"restriction"}
ABCresult
11X1
0X11
1011
0X00
1000
354 or ( $class eq $limit->{restriction} ) )
355 {
356  
357 # If the restriction passes, set the current $allow/$deny
358 # bit according to if this was a positive or negative
359 # limit
360 0 ▶ if ($limit->{$allow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit->{$allow}) 0 0 0
361 0 $valid = 1;
362 }
363 0 ▶ if ($limit->{$deny}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit->{$deny}) 0 0 0
364 0 $valid = 0;
365 }
366 }
367 }
368  
369 0 return $valid;
370 }
371  
372 =head2 explain CLASS
373  
374 Returns a string describing what allow, deny, show, and hide rules
375 apply to the class name.
376  
377 =cut
378  
379 0 ▶sub explain {
Subroutine explain: not called CC=8 SCAR=42.8
Pod documented
380 0 my $self = shift;
381 0 my $class = shift;
382  
383 0 $class = $self->qualify($class);
384  
385 0 my $str = "";
386 0 for my $limit ( @{$self->action_limits} ) {
387 0 ▶ next unless $limit->{from};
Branch: 1 branch
BranchTrueFalseTotal
unless $limit->{"from"} 0 0 0
388 0 ▶ if ( ( ref $limit->{restriction} and $class =~ $limit->{restriction} )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/ or $class eq $limit->{"restriction"}) 0 0 0
Condition0%0/6
ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/ or $class eq $limit->{"restriction"}
l!l&&r!l&&!r
000
ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/
!ll&&!rl&&r
000
MC/DC0%0/3
ref $limit->{"restriction"} and $class =~ /$limit->{"restriction"}/ or $class eq $limit->{"restriction"}
ref $limit->{"restriction"} $class =~ /$limit->{"restriction"}/ $class eq $limit->{"restriction"}
Truth table0%0/5
A and B or C
A= ref $limit->{"restriction"}B= $class =~ /$limit->{"restriction"}/C= $class eq $limit->{"restriction"}
ABCresult
11X1
0X11
1011
0X00
1000
389 or ( $class eq $limit->{restriction} ) )
390 {
391 0 for my $type (qw/allow deny show hide/) {
392 $str .= ucfirst($type)." at ".$limit->{from}.", matches ".$limit->{restriction}."\n"
393 0 ▶ if $limit->{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $limit->{$type} 0 0 0
394 }
395 }
396 }
397 0 return $str;
398 }
399  
400 =head2 all_actions
401  
402 Lists the class names of all actions for this Jifty application,
403 regardless of which are allowed or hidden. See also L</actions> and
404 L</visible_actions>.
405  
406 =cut
407  
408 # Plugin actions under Jifty::Plugin::*::Action are mirrored under
409 # AppName::Action by Jifty::ClassLoader; this code makes all_actions
410 # reflect this mirroring.
411 0 ▶sub all_actions {
Subroutine all_actions: not called CC=6 SCAR=37.4
Pod documented
412 0 my $self = shift;
413 0 ▶ unless ( $self->{all_actions} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"all_actions"}) 0 0 0
414 0 my @actions = $self->__actions;
415 0 my %seen;
416 0 $seen{$_}++ for @actions;
417 0 for (@actions) {
418 0 ▶ if (/^Jifty::Plugin::(.*)::Action::(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Jifty::Plugin::(.*)::Action::(.*)$/) 0 0 0
419 0 my $classname = Jifty->app_class( Action => $2 );
420 0 ▶ push @actions, $classname unless $seen{$classname};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$classname} 0 0 0
421 }
422 }
423 0 $self->{all_actions} = \@actions;
424 }
425 0 return @{ $self->{all_actions} };
426 }
427  
428 =head2 actions
429  
430 Lists the class names of all of the B<allowed> actions for this Jifty
431 application; this may include actions under the C<Jifty::Action::>
432 namespace, in addition to your application's actions. See also
433 L</all_actions> and L</visible_actions>.
434  
435 =cut
436  
437 0 ▶sub actions {
Subroutine actions: not called CC=1 SCAR=6.9
Pod documented
438 0 my $self = shift;
439 0 return sort grep { $self->is_allowed($_) } $self->all_actions;
440 }
441  
442 =head2 visible_actions
443  
444 Lists the class names of all of the B<visible> actions for this Jifty
445 application; this may include actions under the C<Jifty::Action::>
446 namespace, in addition to your application's actions. See also
447 L</all_actions> and L</actions>.
448  
449 =cut
450  
451 0 ▶sub visible_actions {
Subroutine visible_actions: not called CC=1 SCAR=6.9
Pod documented
452 0 my $self = shift;
453 0 return sort grep { $self->is_visible($_) } $self->all_actions;
454 }
455  
456 =head1 SEE ALSO
457  
458 L<Jifty>, L<Jifty::Web>, L<Jifty::Action>
459  
460 =head1 LICENSE
461  
462 Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
463 Jifty is distributed under the same terms as Perl itself.
464  
465 =cut
466  
467 113 1;
« Jifty/Test.pm ↑ Summary Jifty/TestServer.pm »