File Coverage

blib/lib/Catalyst/Plugin/StrongParameters.pm
Criterion Covered Total %
statement 9 12 75.0
branch 0 4 0.0
condition n/a
subroutine 5 6 83.3
pod 4 4 100.0
total 18 26 69.2


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::StrongParameters;
2              
3             our $VERSION = '0.001';
4              
5 1     1   3189 use Moose::Role;
  1         2  
  1         8  
6 1     1   5064 use Scalar::Util;
  1         3  
  1         261  
7              
8 1     1 1 1255 sub strong_body { return shift->req->strong_body(@_) }
9 1     1 1 761 sub strong_data { return shift->req->strong_data(@_) }
10 2     2 1 76399 sub strong_query { return shift->req->strong_query(@_) }
11              
12             sub isa_strong_parameter_exception {
13 0     0 1   my ($self, $obj) = @_;
14 0 0         return 0 unless Scalar::Util::blessed($obj);
15 0 0         return $obj->isa('Catalyst::Exception::StrongParameter') ? 1:0;
16             }
17              
18             around request_class_traits => sub {
19             my ($orig, $self, @args) = @_;
20             my $traits = $self->$orig(@args);
21             return [ @{$traits||[]}, 'Catalyst::TraitFor::Request::StrongParameters' ];
22             };
23              
24             1;
25              
26             =head1 NAME
27              
28             Catalyst::Plugin::StrongParameters - Plug to add tge strong parameter request trait plus proxy methods
29              
30             =head1 SYNOPSIS
31              
32             package MyApp;
33             use Catalyst 'StrongParameters';
34            
35             MyApp->setup;
36              
37             package MyApp::Controller::Root;
38              
39             sub body :Local {
40             my ($self, $c) = @_;
41             my %clean = $c->strong_body
42             ->permitted(['person'], +{'email' => []})
43             ->namespace(['person'])
44             ->permitted(
45             'name',
46             'age',
47             'address' => ['street' => ['number', 'zip'],
48             +{'credit_cards' => [
49             'number',
50             'exp' => [qw/year month day/],
51             ]},
52             )->to_hash;
53              
54             ## Do something with the sanitized body parameters
55             }
56              
57             ## Don't forget to add code to handle any exceptions
58              
59             sub end :Action {
60             my ($self, $c) = @_;
61             if(my $error = $c->last_error) {
62             $c->clear_errors; ## Clear the error stack unless you want the default Catalyst error
63             if($c->isa_strong_parameter_exception($error)) {
64             ## Something here like return a Bad Request 4xx view or similar.
65             }
66             }
67             }
68              
69             You should review L<Catalyst::TraitFor::Request::StrongParameters> for a more detailed SYNOPSIS and
70             explanation of how all this works.
71              
72             =head1 DESCRIPTION
73              
74             This plugin will add in the L<Catalyst::TraitFor::Request::StrongParameters> request class trait
75             and proxy some of its methods to the context. You might find this a bit less typing.
76              
77             All the main documentation is in L<Catalyst::TraitFor::Request::StrongParameters>.
78              
79             NOTE: This plugin only works with For L<Catalyst> v5.90090 or greater. If you must use an older
80             version of L<Catalyst> you'll need to use the workaround described in the SYNOPSIS of
81             L<Catalyst::TraitFor::Request::StrongParameters>.
82              
83             =head1 METHODS
84              
85             This role defines the following methods:
86              
87             =head2 strong_body
88              
89             =head2 strong_data
90              
91             =head2 strong_query
92              
93             These just proxy to the same methods under the L<Catalyst::Request> object.
94              
95             =head2 isa_strong_parameter_exception
96              
97             This is just a convenience method that returns true if a possible exception is both a blessed
98             object and ISA L<Catalyst::Exception::StrongParameter>. Since you need to add checking for
99             this everytime I added this method to save a bit of trouble.
100              
101             =head1 AUTHOR
102              
103             See L<Catalyst::TraitFor::Request::StrongParameters>
104              
105             =head1 SEE ALSO
106              
107             L<Catalyst>, L<Catalyst::TraitFor::Request::StrongParameters>
108              
109             =head1 COPYRIGHT & LICENSE
110              
111             See L<Catalyst::TraitFor::Request::StrongParameters>
112              
113             =cut