| 1 | package Yancy::Plugin::Auth::Role::RequireUser; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 12 | our $VERSION = '1.088'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Add authorization based on user attributes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | #pod use Mojolicious::Lite; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | #pod plugin Yancy => ...; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | #pod # Require any user | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | #pod my $require_user = app->yancy->auth->require_user; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | #pod my $user = app->routes->under( '/user', $require_user ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod # Require a user with the `is_admin` field set to true | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod my $require_admin = app->yancy->auth->require_user( { is_admin => 1 } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod my $admin = app->routes->under( '/admin', $require_admin ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod =head1 DESCRIPTION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod B<Note:> This module is C<EXPERIMENTAL> and its API may change before | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod Yancy v2.000 is released. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod This plugin adds a simple authorization method to your site. All default | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod Yancy auth plugins use this role to provide the C<yancy.auth.require_user> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod helper. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod =head1 SEE ALSO | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod L<Yancy::Plugin::Auth> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 12 | ▶ | use Mojo::Base '-role'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 12 | ▶ | use Yancy::Util qw( currym match ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod =method require_user | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod my $subref = $c->yancy->auth->require_user( \%match ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod Build a callback to validate there is a logged-in user, and optionally | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod that the current user has certain fields set. C<\%match> is optional and | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | #pod is a L<SQL::Abstract where clause|SQL::Abstract/WHERE CLAUSES> matched | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | #pod with L<Yancy::Util/match>. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod # Ensure the user is logged-in | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod my $user_cb = $app->yancy->auth->require_user; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod my $user_only = $app->routes->under( $user_cb ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod # Ensure the user's "is_admin" field is set to 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod my $admin_cb = $app->yancy->auth->require_user( { is_admin => 1 } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod my $admin_only = $app->routes->under( $admin_cb ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | #pod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | #pod =cut | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 9 | ▶ | sub require_user { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
require_user: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | my ( $self, $c, $where ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 16 | ▶ | return sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 16 | my ( $c ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | #; say "Are you authorized? " . $c->yancy->auth->current_user; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 16 | my $user = $c->yancy->auth->current_user; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # If where isn't specified, or it's a plain scalar truth value | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 16 | ▶ | if ( ( !$where || ( !ref $where && $where ) ) && $user ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
!$where || !ref($where) && $where and $user
not $where or not ref $where and $where
not ref $where and $where
MC/DC75%3/4
!$where || !ref($where) && $where and $user
not $where
not ref $where
$where
$user
Truth table83%5/6
not(A) or not(B) and C and D
A= not $whereB= not ref $whereC= $whereD= $user
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | return 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 12 | ▶ | if ( $where && match( $where, $user ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$where and match($where, $user)
MC/DC100%2/2
$where and match($where, $user)
$where
match($where, $user)
Truth table100%3/3
A and B
A= $whereB= match($where, $user)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | return 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # XXX: Create `reply->unauthorized` helper | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $c->stash( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | template => 'yancy/auth/unauthorized', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | status => 401, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | logout_route => $self->logout_route->render, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 9 | $c->respond_to( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | json => {}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | html => {}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 9 | return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 9 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 13 | ▶ | around register => sub { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 13 | my ( $orig, $self, $app, $config ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 13 | $app->helper( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 'yancy.auth.require_user' => currym( $self, 'require_user' ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 13 | $self->$orig( $app, $config ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 12 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 12 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | __END__ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||