Yancy/Plugin/Roles.pm

filter: Statementstmt 100% 31 / 31 Branchbran 100% 6 / 6 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 33.3% 1 / 3 totaltotal 88.4% 46 / 52 CC 6 SCAR 9.0
CC
6
Cov
95%
register
4.0
__ANON__
2.0
has_role
2.0
CRAP 2.5
« Yancy/Plugin/Auth/OAuth2.pm ↑ Summary Yancy/Plugin/Form.pm »
1 package Yancy::Plugin::Roles;
2 3 our $VERSION = '1.088';
3 # ABSTRACT: Role-based access controls (RBAC)
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod plugin Yancy => ...;
8 #pod plugin Auth => ...;
9 #pod plugin Roles => {
10 #pod schema => 'roles',
11 #pod userid_field => 'username',
12 #pod };
13 #pod
14 #pod =head1 DESCRIPTION
15 #pod
16 #pod B<Note:> This module is C<EXPERIMENTAL> and its API may change before
17 #pod Yancy v2.000 is released.
18 #pod
19 #pod This plugin provides user authorization based on roles. Roles are
20 #pod created by using the L</require_role> method. User accounts are provided
21 #pod by L<Yancy::Plugin::Auth> (or a subclass). Accounts are mapped to roles
22 #pod in the database.
23 #pod
24 #pod =head1 CONFIGURATION
25 #pod
26 #pod This plugin has the following configuration options.
27 #pod
28 #pod =head2 schema
29 #pod
30 #pod The name of the Yancy schema that holds role memberships. Required.
31 #pod
32 #pod =head2 userid_field
33 #pod
34 #pod The name of the field in the schema which is the user's identifier.
35 #pod This field should be named the same in both the user schema and the
36 #pod roles schema.
37 #pod
38 #pod =head2 role_field
39 #pod
40 #pod The name of the field in the schema which holds the role. Defaults to
41 #pod C<role>.
42 #pod
43 #pod =head1 HELPERS
44 #pod
45 #pod This plugin has the following helpers.
46 #pod
47 #pod =head2 yancy.auth.has_role
48 #pod
49 #pod Return true if the current user has the given role.
50 #pod
51 #pod get '/admin' => sub {
52 #pod my $c = shift;
53 #pod return $c->reply->not_found unless $c->yancy->auth->has_role( 'admin' );
54 #pod };
55 #pod
56 #pod =head2 yancy.auth.require_role
57 #pod
58 #pod Validate there is a logged-in user and that the user belongs to the given
59 #pod role(s). Returns a callback that can be used in C<under>.
60 #pod
61 #pod my $require_admin = $app->yancy->auth->require_role( 'admin' );
62 #pod my $admin_routes = $app->routes->under( '/admin', $require_admin );
63 #pod
64 #pod =head1 SEE ALSO
65 #pod
66 #pod L<Yancy::Plugin::Auth>
67 #pod
68 #pod =cut
69  
70 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 ▶use Yancy::Util qw( currym );
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 3 has schema =>;
74 3 has userid_field =>;
75 3 has role_field => 'role';
76  
77 2 ▶sub register {
Subroutine register: called CC=4 SCAR=14.0
Pod documented
78 2 my ( $self, $app, $config ) = @_;
79 my $schema_name = $config->{schema}
80 2 ▶ || die "Error configuring Roles plugin: No schema defined\n";
Condition50%1/2
$config->{'schema'} || die("Error configuring Roles plugin: No schema defined\n")
l!l
20
MC/DC0%0/1
$config->{'schema'} || die("Error configuring Roles plugin: No schema defined\n")
$config->{'schema'}
Truth table50%1/2
A
A= $config->{'schema'}
Aresult
11
00
81 2 $self->schema( $schema_name );
82  
83 2 ▶ my $schema = $app->yancy->schema( $schema_name )
Condition50%1/2
$app->yancy->schema($schema_name) || die(sprintf(qq[Error configuring Roles plugin: Schema "%s" not found\n], $schema_name))
l!l
20
MC/DC0%0/1
$app->yancy->schema($schema_name) || die(sprintf(qq[Error configuring Roles plugin: Schema "%s" not found\n], $schema_name))
$app->yancy->schema($schema_name)
Truth table50%1/2
A
A= $app->yancy->schema($schema_name)
Aresult
11
00
84 || die sprintf(
85 q{Error configuring Roles plugin: Schema "%s" not found}."\n",
86 $schema_name,
87 );
88  
89 2 $self->userid_field( $config->{userid_field} );
90 2 ▶ $self->role_field( $config->{role_field} ) if $config->{role_field};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"role_field"} 1 1 2
91 2 $app->helper(
92 'yancy.auth.require_role' => currym( $self, 'require_role' ),
93 );
94 2 $app->helper(
95 'yancy.auth.has_role' => currym( $self, 'has_role' ),
96 );
97 }
98  
99 1 ▶sub require_role {
Subroutine require_role: called CC=1 SCAR=0.0
Pod undocumented
100 1 my ( $self, $c, $role ) = @_;
101 2 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
102 2 my ( $c ) = @_;
103 2 ▶ return 1 if $self->has_role( $c, $role );
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_role($c, $role) 1 1 2
104 # XXX: Create `reply->unauthorized` helper
105 1 $c->stash(
106 template => 'yancy/auth/unauthorized',
107 status => 401,
108 );
109 1 $c->respond_to(
110 json => {},
111 html => {},
112 );
113 1 return undef;
114 }
115 1 }
116  
117 5 ▶sub has_role {
Subroutine has_role: called CC=2 SCAR=6.9
Pod undocumented
118 5 my ( $self, $c, $role ) = @_;
119 5 ▶ my $user = $c->yancy->auth->current_user or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $user = $c->yancy->auth->current_user or return undef 3 2 5
120 my %search = (
121 3 $self->userid_field => $user->{ $self->userid_field },
122 $self->role_field => $role,
123 );
124 3 my ( $has_role ) = $c->yancy->list( $self->schema, \%search );
125 3 return !!$has_role;
126 }
127  
128 3 1;
129  
130 __END__
« Yancy/Plugin/Auth/OAuth2.pm ↑ Summary Yancy/Plugin/Form.pm »