| 1 | package Catalyst::TraitFor::Controller::PermissionCheck; | ||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 4 | $Catalyst::TraitFor::Controller::PermissionCheck::VERSION = '0.04'; | |||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | ## ABSTRACT: Provides an opinionated method for verifying permissions on a per-action basis by inspecting the user. | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | # Requires setup in the consuming class. | ||||||||||||||||||||||||||||||||
| 13 | 4 | requires 'setup'; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | has 'permissions' => ( | ||||||||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 18 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||
| 19 | traits => [ 'Hash' ], | ||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | default => sub { { } }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 21 | 4 | lazy => 1, | |||||||||||||||||||||||||||||||
| 22 | handles => { | ||||||||||||||||||||||||||||||||
| 23 | 'get_permission_for_action' => 'get', | ||||||||||||||||||||||||||||||||
| 24 | 'has_permissions' => 'count', | ||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | has 'allow_by_default' => ( | ||||||||||||||||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 31 | isa => 'Bool', | ||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | default => sub { 1; }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 33 | 4 | lazy => 1, | |||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | sub fetch_permissions { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch_permissions: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 38 | 6 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||
| 39 | 6 | ▶ | return $c->stash->{context}->{permissions} || {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$c->stash->{'context'}{'permissions'} || {}
MC/DC0%0/1
$c->stash->{'context'}{'permissions'} || {}
$c->stash->{'context'}{'permissions'}
Truth table50%1/2
A
A= $c->stash->{'context'}{'permissions'}
| |||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 9 | ▶ | after 'setup' => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||
| 44 | 9 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||
| 45 | 9 | my $namespace = $self->action_namespace($c); | |||||||||||||||||||||||||||||||
| 46 | 9 | my $chain = $c->dispatcher->expand_action($c->action); | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 9 | my @actions = grep { $_->namespace eq $namespace } @{ $chain->chain }; | |||||||||||||||||||||||||||||||
| 49 | # XX This should crawl the entire action chain and iterate to find | ||||||||||||||||||||||||||||||||
| 50 | # permissions. But it doesn't, so supply a patch! | ||||||||||||||||||||||||||||||||
| 51 | 9 | ▶ | my $action = $actions[-1] ? $actions[-1]->name : $c->action->name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 9 | my $perm; | |||||||||||||||||||||||||||||||
| 54 | 9 | ▶ | if ( $c->req->method eq 'GET' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 7 | $perm = $self->get_permission_for_action( $action ); | |||||||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||||||
| 57 | # Not a GET request, so look up the $action_PUT style actions that | ||||||||||||||||||||||||||||||||
| 58 | # Catalyst::Controller::REST uses. | ||||||||||||||||||||||||||||||||
| 59 | 2 | $perm = $self->get_permission_for_action( $action . '_' . $c->req->method); | |||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | $c->log->debug("Nothing on top level, checking req method: $action") if $c->debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | # Still don't have permissions, look at setup | ||||||||||||||||||||||||||||||||
| 63 | 9 | ▶ | if ( not defined $perm ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | 3 | $perm = $self->get_permission_for_action( 'setup' ); | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 9 | ▶ | if ( not defined $perm and not $self->allow_by_default ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $perm || $self->allow_by_default
MC/DC100%2/2
defined $perm || $self->allow_by_default
defined $perm
$self->allow_by_default
Truth table100%3/3
A || B
A= defined $permB= $self->allow_by_default
| |||||||||||||||||||||||||||||||||
| 68 | 2 | $c->log->error("Action misconfiguration! allow_by_default is off but this action ($action) has no permissions configured (nor a setup action)"); | |||||||||||||||||||||||||||||||
| 69 | 2 | $c->detach('permission_denied'); | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | ▶ | elsif ( defined $perm and | |||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $perm and not grep({exists $self->fetch_permissions($c)->{$_};} @$perm)
MC/DC100%2/2
defined $perm and not grep({exists $self->fetch_permissions($c)->{$_};} @$perm)
defined $perm
not grep({exists $self->fetch_permissions($c)->{$_};} @$perm)
Truth table100%3/3
A and not(B)
A= defined $permB= not grep({exists $self->fetch_permissions($c)->{$_};} @$perm)
| |||||||||||||||||||||||||||||||||
| 72 | 7 | not grep { exists $self->fetch_permissions($c)->{$_} } @$perm | |||||||||||||||||||||||||||||||
| 73 | ) { | ||||||||||||||||||||||||||||||||
| 74 | $c->log->info( | ||||||||||||||||||||||||||||||||
| 75 | "Access denied for user: " . | ||||||||||||||||||||||||||||||||
| 76 | ( $c->user_exists ? $c->user->name : 'anonymous' ) . | ||||||||||||||||||||||||||||||||
| 77 | ", require permissions @$perm for action $action, only has: " . | ||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | join(', ', keys %{ $self->fetch_permissions($c) } ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||
| 80 | 2 | $c->detach('permission_denied'); | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 4 | }; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 85 | 4 | 1; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | =pod | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | Catalyst::TraitFor::Controller::PermissionCheck - Provides an opinionated method for verifying permissions on a per-action basis by inspecting the user. | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | version 0.04 | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | package MyApp::Controller::Something; | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | use Moose; | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | BEGIN { extends 'Catalyst::Controller'; } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | # Include the role here | ||||||||||||||||||||||||||||||||
| 108 | with 'Catalyst::TraitFor::Controller::PermissionCheck'; | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | __PACKAGE__->config( | ||||||||||||||||||||||||||||||||
| 111 | permissions => { | ||||||||||||||||||||||||||||||||
| 112 | 'some_action' => [ qw/List Of Permissions Required/ ], | ||||||||||||||||||||||||||||||||
| 113 | }, | ||||||||||||||||||||||||||||||||
| 114 | # Deny everything, requires all actions have permissions. | ||||||||||||||||||||||||||||||||
| 115 | # allow_by_default => 1 only checks if a permission entry exists | ||||||||||||||||||||||||||||||||
| 116 | allow_by_default => 0, | ||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | # Your root chain must be called 'setup'. This is convention must be | ||||||||||||||||||||||||||||||||
| 120 | # followed if you want to use this module. | ||||||||||||||||||||||||||||||||
| 121 | sub setup : Chained('/something_that_sets_permissions') PathPart('') CaptureArgs(0) { | ||||||||||||||||||||||||||||||||
| 122 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||||||
| 123 | # Permissions must be in $c->stash->{context}->{permissions} | ||||||||||||||||||||||||||||||||
| 124 | # and you can set them here. The module only looks at the keys | ||||||||||||||||||||||||||||||||
| 125 | # of the hash. | ||||||||||||||||||||||||||||||||
| 126 | $c->stash->{context}->{permissions} = { | ||||||||||||||||||||||||||||||||
| 127 | 'Admin' => 1, | ||||||||||||||||||||||||||||||||
| 128 | 'Super Admin' => 1, | ||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | sub some_action : Chained('setup') Args(0) { | ||||||||||||||||||||||||||||||||
| 133 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||||||
| 134 | $c->res->body('Only accessible if permissions are ok'); | ||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | sub permission_denied : Private { | ||||||||||||||||||||||||||||||||
| 138 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||||||
| 139 | $c->res->status(403); | ||||||||||||||||||||||||||||||||
| 140 | $c->res->body('GTFO'); | ||||||||||||||||||||||||||||||||
| 141 | $c->detach; | ||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | no Moose; | ||||||||||||||||||||||||||||||||
| 145 | 1; | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | =head2 permissions | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | Configuration hash that is keyed by action name and should point to an | ||||||||||||||||||||||||||||||||
| 152 | array ref of required permissions. | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | Set via config: | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | __PACKAGE__->config( | ||||||||||||||||||||||||||||||||
| 157 | permissions => { | ||||||||||||||||||||||||||||||||
| 158 | 'action_name' => [ qw/Permission List/ ] | ||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | =head2 allow_by_default | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | A boolean configuration option to control whether this module should restrict | ||||||||||||||||||||||||||||||||
| 165 | everything or let things go and only check permissions if they exist in | ||||||||||||||||||||||||||||||||
| 166 | the permissions hash. | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | =head2 fetch_permissions | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | Retrieve a hashref of permissions. This may be overridden to allow alternate | ||||||||||||||||||||||||||||||||
| 173 | sources of permissions, but by default it looks in | ||||||||||||||||||||||||||||||||
| 174 | $c->stash->{context}->{permissions}. | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | =head2 setup | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | Before setup is called, this role inspects | ||||||||||||||||||||||||||||||||
| 179 | C<< $c->stash->{context}->{permissions} >> for applicable roles. | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | It confirms permissions to access the action. This only works with | ||||||||||||||||||||||||||||||||
| 182 | L<Catalyst::DispatchType::Chained> and will walk the entire chain and verify | ||||||||||||||||||||||||||||||||
| 183 | access checks at each level. | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | Andrew Nelson | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | J. Shirley <jshirley@cpan.org> | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | This software is copyright (c) 2011 by Cold Hard Code, LLC. | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 198 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | __END__ | ||||||||||||||||||||||||||||||||