| 1 | package CatalystX::Controller::Verifier; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $CatalystX::Controller::Verifier::VERSION = '0.02'; | |
| 4 | } | ||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | use Carp; | ||
| 12 | use Scalar::Util qw/blessed refaddr/; | ||
| 13 | |||
| 14 | use Data::Manager; | ||
| 15 | use Data::Verifier; | ||
| 16 | use Message::Stack; | ||
| 17 | |||
| 18 | # ABSTRACT: Moose Role for verifying request parameters on a per action basis. | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | has 'verifiers' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'HashRef' | ||
| 25 | ); | ||
| 26 | |||
| 27 | has '_verifier_stash_key' => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'Str', | ||
| 30 | default => '_verifier_stash' | ||
| 31 | ); | ||
| 32 | |||
| 33 | |||
| 34 | has 'detach_on_failure' => ( | ||
| 35 | is => 'rw', | ||
| 36 | isa => 'Str', | ||
| 37 | clearer => 'clear_detach_on_failure', | ||
| 38 | predicate => 'has_detach_on_failure', | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | sub verify { | ||
| 43 | my ( $self, $c, $params ) = @_; | ||
| 44 | $params = $c->req->params if not defined $params or ref $params ne 'HASH'; | ||
| 45 | |||
| 46 | my $dm = $self->data_manager($c); | ||
| 47 | my $results = $dm->verify($c->action->name, $params); | ||
| 48 | |||
| 49 | if ( not $results->success and $self->has_detach_on_failure ) { | ||
| 50 | my $detach = $c->controller->action_for( $self->detach_on_failure ); | ||
| 51 | if ( not $detach ) { | ||
| 52 | croak "Invalid detach action specified, " . $c->controller . " does not have an action '" . $self->detach_on_failure . "'."; | ||
| 53 | } | ||
| 54 | $c->detach($detach, [ $results ]); | ||
| 55 | } | ||
| 56 | |||
| 57 | return $results; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub messages { | ||
| 62 | my ( $self, $c ) = @_; | ||
| 63 | my $dm = $self->data_manager($c); | ||
| 64 | |||
| 65 | my $scope = undef; # Not sure of syntax here. | ||
| 66 | if ( defined $scope and blessed $scope ) { | ||
| 67 | if ( $scope->isa('Catalyst') ) { | ||
| 68 | $scope = $scope->action->name; | ||
| 69 | } | ||
| 70 | elsif ( $scope->isa('Catalyst::Action') ) { | ||
| 71 | $scope = $scope->name; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | return $scope ? $dm->messages_for_scope($scope) : $dm->messages; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub data_manager { | ||
| 79 | my ( $self, $c ) = @_; | ||
| 80 | |||
| 81 | # Should always be blessed, but you never know. | ||
| 82 | my $key = blessed $self ? refaddr $self : $self; | ||
| 83 | my $dm = $c->stash->{ $self->_verifier_stash_key }->{ $key }; | ||
| 84 | if ( not $dm ) { | ||
| 85 | $dm = $self->_build_data_manager; | ||
| 86 | $c->stash->{ $self->_verifier_stash_key }->{ $key } = $dm; | ||
| 87 | } | ||
| 88 | return $dm; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _build_data_manager { | ||
| 92 | my ( $self ) = @_; | ||
| 93 | |||
| 94 | my $verifiers = $self->verifiers; | ||
| 95 | my %profiles = (); | ||
| 96 | |||
| 97 | foreach my $scope ( keys %$verifiers ) { | ||
| 98 | my $profile = $verifiers->{$scope}; | ||
| 99 | if ( not ref $profile ) { | ||
| 100 | $profile = $verifiers->{$profile}; | ||
| 101 | } | ||
| 102 | if ( not defined $profile or ref $profile ne 'HASH' ) { | ||
| 103 | croak "Invalid profile specified: $profile is invalid"; | ||
| 104 | } | ||
| 105 | $profiles{$scope} = Data::Verifier->new( $profile ); | ||
| 106 | } | ||
| 107 | |||
| 108 | return Data::Manager->new( verifiers => \%profiles ); | ||
| 109 | } | ||
| 110 | |||
| 111 | no Moose::Role; | ||
| 112 | 1; | ||
| 113 | |||
| 114 | __END__ | ||