| 1 | package Catalyst::Plugin::StrongParameters; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | our $VERSION = '0.001'; | |||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Scalar::Util; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | sub strong_body { return shift->req->strong_body(@_) } | ||||||||||
|
Subroutine
strong_body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 9 | 1 | ▶ | sub strong_data { return shift->req->strong_data(@_) } | ||||||||||
|
Subroutine
strong_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 10 | 2 | ▶ | sub strong_query { return shift->req->strong_query(@_) } | ||||||||||
|
Subroutine
strong_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 11 | |||||||||||||
| 12 | 0 | ▶ | sub isa_strong_parameter_exception { | ||||||||||
|
Subroutine
isa_strong_parameter_exception: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 13 | 0 | my ($self, $obj) = @_; | |||||||||||
| 14 | 0 | ▶ | return 0 unless Scalar::Util::blessed($obj); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 0 | ▶ | return $obj->isa('Catalyst::Exception::StrongParameter') ? 1:0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | } | ||||||||||||
| 17 | |||||||||||||
| 18 | 1 | ▶ | around request_class_traits => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 19 | 1 | my ($orig, $self, @args) = @_; | |||||||||||
| 20 | 1 | my $traits = $self->$orig(@args); | |||||||||||
| 21 | 1 | ▶ | return [ @{$traits||[]}, 'Catalyst::TraitFor::Request::StrongParameters' ]; | ||||||||||
|
Condition50%1/2
$traits || []
MC/DC0%0/1
$traits || []
$traits
Truth table50%1/2
A
A= $traits
| |||||||||||||
| 22 | 1 | }; | |||||||||||
| 23 | |||||||||||||
| 24 | 1 | 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 | ||||||||||||