Yancy/Plugin/Auth/Role/RequireUser.pm

filter: Statementstmt 39.1% 9 / 23 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 2 / 5 Podpod 100% 1 / 1 totaltotal 23.5% 12 / 51 CC 7 SCAR 35.9
CC
7
Cov
23%
__ANON__
56.0
require_user
2.0
__ANON__
2.0
CRAP 36.2
« Yancy/Plugin/Auth/Token.pm ↑ Summary Yancy/Plugin/Auth/OAuth2.pm »
1 package Yancy::Plugin::Auth::Role::RequireUser;
2 6 our $VERSION = '1.078';
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 6 ▶use Mojo::Base '-role';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶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 sub require_user {
56 0 ▶ my ( $self, $c, $where ) = @_;
Subroutine require_user: not called CC=1 SCAR=6.9
Pod documented
57 return sub {
58 0 ▶ my ( $c ) = @_;
Subroutine __ANON__: not called CC=7 SCAR=40.3
59 #; say "Are you authorized? " . $c->yancy->auth->current_user;
60 0 my $user = $c->yancy->auth->current_user;
61 # If where isn't specified, or it's a plain scalar truth value
62 0 ▶ if ( ( !$where || ( !ref $where && $where ) ) && $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$where || !ref($where) && $where and $user) 0 0 0
Condition0%0/9
!$where || !ref($where) && $where and $user
!ll&&!rl&&r
000
not $where or not ref $where and $where
l!l&&r!l&&!r
000
not ref $where and $where
!ll&&!rl&&r
000
MC/DC0%0/4
!$where || !ref($where) && $where and $user
not $where not ref $where $where $user
Truth table0%0/6
not(A) or not(B) and C and D
A= not $whereB= not ref $whereC= $whereD= $user
ABCDresult
00XX0
010X0
1XX00
01100
1XX11
01111
63 0 return 1;
64 }
65 0 ▶ if ( $where && match( $where, $user ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where and match($where, $user)) 0 0 0
Condition0%0/3
$where and match($where, $user)
!ll&&!rl&&r
000
MC/DC0%0/2
$where and match($where, $user)
$where match($where, $user)
Truth table0%0/3
A and B
A= $whereB= match($where, $user)
ABresult
0X0
100
111
66 0 return 1;
67 }
68 # XXX: Create `reply->unauthorized` helper
69 $c->stash(
70 0 template => 'yancy/auth/unauthorized',
71 status => 401,
72 logout_route => $self->logout_route->render,
73 );
74 0 $c->respond_to(
75 json => {},
76 html => {},
77 );
78 0 return undef;
79 0 };
80 }
81  
82 around register => sub {
83 0 ▶ my ( $orig, $self, $app, $config ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 $app->helper(
85 'yancy.auth.require_user' => currym( $self, 'require_user' ),
86 );
87 0 $self->$orig( $app, $config );
88 6 };
89  
90 6 1;
91  
92 __END__
« Yancy/Plugin/Auth/Token.pm ↑ Summary Yancy/Plugin/Auth/OAuth2.pm »