Catalyst-TraitFor-Controller-PermissionCheck
view release on metacpan or search on metacpan
lib/Catalyst/TraitFor/Controller/PermissionCheck.pm view on Meta::CPAN
package Catalyst::TraitFor::Controller::PermissionCheck;
BEGIN {
$Catalyst::TraitFor::Controller::PermissionCheck::VERSION = '0.04';
}
use Moose::Role;
use Try::Tiny;
## ABSTRACT: Provides an opinionated method for verifying permissions on a per-action basis by inspecting the user.
# Requires setup in the consuming class.
requires 'setup';
has 'permissions' => (
is => 'rw',
isa => 'HashRef',
traits => [ 'Hash' ],
default => sub { { } },
lazy => 1,
handles => {
'get_permission_for_action' => 'get',
'has_permissions' => 'count',
}
);
has 'allow_by_default' => (
is => 'rw',
isa => 'Bool',
default => sub { 1; },
lazy => 1,
);
sub fetch_permissions {
my ( $self, $c ) = @_;
return $c->stash->{context}->{permissions} || {};
}
after 'setup' => sub {
my ( $self, $c ) = @_;
my $namespace = $self->action_namespace($c);
my $chain = $c->dispatcher->expand_action($c->action);
my @actions = grep { $_->namespace eq $namespace } @{ $chain->chain };
# XX This should crawl the entire action chain and iterate to find
# permissions. But it doesn't, so supply a patch!
my $action = $actions[-1] ? $actions[-1]->name : $c->action->name;
my $perm;
if ( $c->req->method eq 'GET' ) {
$perm = $self->get_permission_for_action( $action );
} else {
# Not a GET request, so look up the $action_PUT style actions that
# Catalyst::Controller::REST uses.
$perm = $self->get_permission_for_action( $action . '_' . $c->req->method);
$c->log->debug("Nothing on top level, checking req method: $action") if $c->debug;
}
# Still don't have permissions, look at setup
if ( not defined $perm ) {
$perm = $self->get_permission_for_action( 'setup' );
}
if ( not defined $perm and not $self->allow_by_default ) {
( run in 3.793 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )