File Coverage

blib/lib/Mojolicious/Plugin/Authorization.pm
Criterion Covered Total %
statement 43 43 100.0
branch 14 14 100.0
condition 12 17 70.5
subroutine 9 9 100.0
pod 1 1 100.0
total 79 84 94.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Authorization;
2             BEGIN {
3 4     4   24815 $Mojolicious::Plugin::Authorization::VERSION = '1.06';
4             }
5 4     4   571 use Mojo::Base 'Mojolicious::Plugin';
  4         218070  
  4         34  
6             # The dog is good, but our real competition is the Hypnotoad.
7             sub register {
8 11     11 1 4215 my ($self, $app, $args) = @_;
9 11   50     37 $args ||= {};
10 11         25 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 29 100 100     179 unless $args->{$sub_ref} && ref($args->{$sub_ref}) eq 'CODE';
13             }
14 2         6 my $has_priv_cb = $args->{has_priv};
15 2         5 my $is_role_cb = $args->{is_role};
16 2         5 my $user_privs_cb = $args->{user_privs};
17 2         4 my $user_role_cb = $args->{user_role};
18 2         5 my $fail_render = $args->{fail_render};
19             $app->routes->add_condition(has_priv => sub {
20 3     3   46449 my ($r, $c, $captures, $priv) = @_;
21 3         8 my $extradata;
22 3 100       15 ($priv, $extradata) = @$priv if (ref($priv) eq 'ARRAY');
23 3 100 66     29 my $res = ($priv && $has_priv_cb->($c,$priv,$extradata)) ? 1 : 0;
24 3 100 66     600 $c->render( %$fail_render ) if $fail_render && ! $res;
25 3         900 return $res;
26 2         12 });
27 2         57 for my $helper_name ( qw/ is_role is / ) {
28             $app->routes->add_condition($helper_name => sub {
29 5     5   42315 my ($r, $c, $captures, $role) = @_;
30 5         11 my $extradata;
31 5 100       23 ($role, $extradata) = @$role if (ref($role) eq 'ARRAY');
32 5 100 66     30 my $res = ($role && $is_role_cb->($c,$role,$extradata)) ? 1 : 0;
33 5 100 66     1158 $c->render( %$fail_render ) if $fail_render && ! $res;
34 5         830 return $res;
35 4         28 });
36             }
37             $app->helper(privileges => sub {
38 2     2   28151 my ($c, $extradata) = @_;
39 2         11 return $user_privs_cb->($c, $extradata);
40 2         33 });
41 2         235 for my $helper_name ( qw/ has_priv has_privilege / ) {
42             $app->helper($helper_name => sub {
43 8     8   131895 my ($c, $priv, $extradata) = @_;
44 8         47 my $has_priv = $has_priv_cb->($c, $priv, $extradata);
45 8         4826 return $has_priv;
46 4         178 });
47             }
48 2         156 for my $helper_name ( qw/ is is_role / ) {
49             $app->helper($helper_name => sub {
50 2     2   27609 my ($c, $role, $extradata) = @_;
51 2         32 return $is_role_cb->($c, $role, $extradata);
52 4         237 });
53             }
54             $app->helper(role => sub {
55 3     3   41898 my ($c, $extradata) = @_;
56 3         17 return $user_role_cb->($c, $extradata);
57 2         162 });
58             }
59             1;
60             __END__