lib/Mojolicious/Plugin/Fondation/Authorization.pm

filter: Statementstmt 94.0% 47 / 50 Branchbran 87.5% 14 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.6% 11 / 12 Podpod 50.0% 1 / 2 totaltotal 83.3% 75 / 90 CC 11 SCAR 11.7
CC
11
Cov
88%
__ANON__
5.2
__ANON__
5.2
__ANON__
3.1
CRAP 3.2
↑ Summary
1 package Mojolicious::Plugin::Fondation::Authorization;
2 4 $Mojolicious::Plugin::Fondation::Authorization::VERSION = '0.02';
3 # ABSTRACT: Authorization plugin — grants loading and check_perm/check_group helpers
4  
5 4 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶sub fondation_meta {
Subroutine fondation_meta: called CC=1 SCAR=0.0
Pod undocumented
8 return {
9 3 dependencies => [
10 'Fondation::Auth',
11 'Fondation::Group',
12 'Fondation::Perm',
13 'Fondation::SessionStore'
14 ],
15 defaults => {},
16 };
17 }
18  
19 3 ▶sub register ($self, $app, $config) {
Subroutine register: called CC=1 SCAR=0.1
Pod documented
20  
21 # ── Helpers ──────────────────────────────────────────────────────
22  
23 44 ▶ $app->helper(check_perm => sub ($c, $perm) {
Subroutine __ANON__: called CC=5 SCAR=16.4
24 44 ▶ return 0 unless $c->is_user_authenticated;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->is_user_authenticated 5 39 44
25 39 my $grants = $c->session('grants');
26 39 ▶ return 0 unless $grants && $grants->{permissions};
Branch: 1 branch
BranchTrueFalseTotal
unless $grants and $grants->{"permissions"} 0 39 39
Condition33%1/3
$grants and $grants->{"permissions"}
!ll&&!rl&&r
0039
MC/DC0%0/2
$grants and $grants->{"permissions"}
$grants $grants->{"permissions"}
Truth table33%1/3
A and B
A= $grantsB= $grants->{"permissions"}
ABresult
0X0
100
111
27 39 ▶ return 1 if grep { $_ eq $perm } @{$grants->{permissions}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $perm;} @{$$grants{"permissions"};} 17 22 39
28 22 return 0;
29 3 });
30  
31 44 ▶ $app->helper(check_group => sub ($c, $group) {
Subroutine __ANON__: called CC=5 SCAR=16.4
32 44 ▶ return 0 unless $c->is_user_authenticated;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->is_user_authenticated 5 39 44
33 39 my $grants = $c->session('grants');
34 39 ▶ return 0 unless $grants && $grants->{groups};
Branch: 1 branch
BranchTrueFalseTotal
unless $grants and $grants->{"groups"} 0 39 39
Condition33%1/3
$grants and $grants->{"groups"}
!ll&&!rl&&r
0039
MC/DC0%0/2
$grants and $grants->{"groups"}
$grants $grants->{"groups"}
Truth table33%1/3
A and B
A= $grantsB= $grants->{"groups"}
ABresult
0X0
100
111
35 39 ▶ return 1 if grep { $_ eq $group } @{$grants->{groups}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $group;} @{$$grants{"groups"};} 9 30 39
36 30 return 0;
37 3 });
38  
39 # ── around_dispatch — async grant loading ─────────────────────
40  
41 70 ▶ $app->hook(around_dispatch => sub ($next, $c) {
Subroutine __ANON__: called CC=3 SCAR=11.3
42 70 ▶ unless ($c->is_user_authenticated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->is_user_authenticated) 20 50 70
43 20 $c->session(grants => undef);
44 20 return $next->();
45 }
46 50 ▶ return $next->() if $c->session('grants');
Branch: 1 branch
BranchTrueFalseTotal
if $c->session("grants") 40 10 50
47  
48 10 ▶ $self->_load_grants_async($c)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 10 $next->();
50 0 ▶ })->on_fail(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 my $err = shift;
52 0 $c->app->log->error("[Authorization] Failed to load grants: $err");
53 0 $next->();
54 10 })->retain;
55 3 });
56  
57 3 return $self;
58 }
59  
60 # ── Internal: async loading via $c->model ──────────────────────────
61  
62 10 ▶sub _load_grants_async ($self, $c) {
Subroutine _load_grants_async: called CC=1 SCAR=0.0
63 10 my $uid = $c->current_user->{uid};
64 10 my $log = $c->app->log;
65  
66 10 $log->info("[Authorization] Loading grants for uid=$uid");
67  
68 # Load groups — user → user_group → group
69 my $groups_f = $c->model('group')->search(
70 { 'user_group.user_id' => $uid },
71 { join => 'user_group' },
72 10 ▶ )->all->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 10 my $rows = shift;
74 10 $log->info("[Authorization] Groups query returned " . scalar(@$rows) . " rows");
75 10 return [map { $_->name } @$rows];
76 10 });
77  
78 # Load permissions — user → user_group → group → group_perm → perm
79 my $perms_f = $c->model('perm')->search(
80 { 'user_group.user_id' => $uid },
81 {
82 join => { group_perm => { group => { user_group => undef } } },
83 distinct => 1,
84 },
85 10 ▶ )->all->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 10 my $rows = shift;
87 10 $log->info("[Authorization] Perms query returned " . scalar(@$rows) . " rows");
88 10 return [map { $_->name } @$rows];
89 10 });
90  
91 # Combine both results and store in session
92 10 ▶ return Future->needs_all($groups_f, $perms_f)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 10 my ($groups, $perms) = @_;
94  
95 10 $log->info(sprintf '[Authorization] Loaded %d groups, %d perms for uid=%s',
96 scalar @$groups, scalar @$perms, $uid);
97  
98 10 $c->session(grants => {
99 permissions => $perms,
100 groups => $groups,
101 });
102 10 });
103 }
104  
105 4 1;
106  
107 __END__
↑ Summary