File Coverage

blib/lib/Mojolicious/Plugin/Authorization.pm
Criterion Covered Total %
statement 39 39 100.0
branch 5 10 50.0
condition 6 17 35.2
subroutine 9 9 100.0
pod 1 1 100.0
total 60 76 78.9


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Authorization;
2             BEGIN {
3 2     2   1266 $Mojolicious::Plugin::Authorization::VERSION = '1.05';
4             }
5 2     2   382 use Mojo::Base 'Mojolicious::Plugin';
  2         6700  
  2         11  
6             # The dog is good, but our real competition is the Hypnotoad.
7             sub register {
8 1     1 1 33 my ($self, $app, $args) = @_;
9 1   50     4 $args ||= {};
10 1         1 for my $sub_ref ( qw/ has_priv is_role user_privs user_role / ) {
11             die __PACKAGE__, ": missing '$sub_ref' subroutine ref in parameters\n"
12 4 50 33     18 unless $args->{$sub_ref} && ref($args->{$sub_ref}) eq 'CODE';
13             }
14 1         1 my $has_priv_cb = $args->{has_priv};
15 1         2 my $is_role_cb = $args->{is_role};
16 1         1 my $user_privs_cb = $args->{user_privs};
17 1         1 my $user_role_cb = $args->{user_role};
18 1         2 my $fail_render = $args->{fail_render};
19             $app->routes->add_condition(has_priv => sub {
20 1     1   5428 my ($r, $c, $captures, $priv) = @_;
21 1 50 33     6 my $res = ($priv && $has_priv_cb->($c,$priv)) ? 1 : 0;
22 1 50 33     260 $c->render( %$fail_render ) if $fail_render && ! $res;
23 1         2 return $res;
24 1         5 });
25 1         24 for my $helper_name ( qw/ is_role is / ) {
26             $app->routes->add_condition($helper_name => sub {
27 2     2   12849 my ($r, $c, $captures, $role) = @_;
28 2 50 33     12 my $res = ($role && $is_role_cb->($c,$role)) ? 1 : 0;
29 2 50 33     634 $c->render( %$fail_render ) if $fail_render && ! $res;
30 2         4 return $res;
31 2         12 });
32             }
33             $app->helper(privileges => sub {
34 2     2   13117 my ($c, $extradata) = @_;
35 2         9 return $user_privs_cb->($c, $extradata);
36 1         12 });
37 1         23 for my $helper_name ( qw/ has_priv has_privilege / ) {
38             $app->helper($helper_name => sub {
39 8     8   60088 my ($c, $priv, $extradata) = @_;
40 8         23 my $has_priv = $has_priv_cb->($c, $priv, $extradata);
41 8         2485 return $has_priv;
42 2         343 });
43             }
44 1         9 for my $helper_name ( qw/ is is_role / ) {
45             $app->helper($helper_name => sub {
46 2     2   12117 my ($c, $role, $extradata) = @_;
47 2         8 return $is_role_cb->($c, $role, $extradata);
48 2         13 });
49             }
50             $app->helper(role => sub {
51 3     3   19382 my ($c, $extradata) = @_;
52 3         12 return $user_role_cb->($c, $extradata);
53 1         10 });
54             }
55             1;
56             __END__