Auth/Provider/DBIx.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 61.1% 22 / 36 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 7 / 7 Podpod 25.0% 1 / 4 totaltotal 70.9% 88 / 124 CC 25 SCAR 20.1
CC
25
Cov
76%
load_user
10.7
validate_user
10.1
new
4.2
CRAP 7.4
↑ Summary Auth/Provider.pm »
1 package Mojolicious::Plugin::Fondation::Auth::Provider::DBIx;
2 2 $Mojolicious::Plugin::Fondation::Auth::Provider::DBIx::VERSION = '0.04';
3 # ABSTRACT: DBIx::Class-backed authentication provider for Fondation::Auth
4  
5 2 ▶use Mojo::Base 'Mojolicious::Plugin::Fondation::Auth::Provider', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 has 'model'; # model name (e.g. 'user')
8 2 has 'username_column'; # default: 'username'
9 2 has 'password_column'; # default: 'password'
10  
11 # ── Constructor ───────────────────────────────────────────────────────
12  
13 2 ▶sub new ($class, %args) {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
14 2 my $self = $class->SUPER::new(%args);
15  
16 2 ▶ $self->username_column('username') unless $self->username_column;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->username_column 0 2 2
17 2 ▶ $self->password_column('password') unless $self->password_column;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->password_column 0 2 2
18 2 $self->name('dbix');
19  
20 2 ▶ $self->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
$self->model ? : 2 0 2
21 $self->model
22 ? "DBIx provider initialized for model '$self->{model}'"
23 : "DBIx provider initialized (model will be resolved from model_list)"
24 );
25  
26 2 return $self;
27 }
28  
29 # ── Authentication ────────────────────────────────────────────────────
30  
31 6 ▶sub validate_user ($self, $c, $username, $password, $extra = {}) {
Subroutine validate_user: called CC=9 SCAR=23.2
Pod undocumented
32 6 ▶ return undef unless $username && $password;
Branch: 1 branch
BranchTrueFalseTotal
unless $username and $password 0 6 6
Condition33%1/3
$username and $password
!ll&&!rl&&r
006
MC/DC0%0/2
$username and $password
$username $password
Truth table33%1/3
A and B
A= $usernameB= $password
ABresult
0X0
100
111
33  
34 6 my $schema = $self->_schema($c);
35 6 ▶ return undef unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 6 6
36  
37 6 my $source = $self->_source($c);
38 6 ▶ return undef unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 6 6
39  
40 6 my $user = $schema->resultset($source)->search({
41 $self->username_column => $username,
42 })->single;
43  
44 6 ▶ return undef unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 1 5 6
45  
46 5 ▶ if ($user->check_password($password)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->check_password($password)) 4 1 5
47 4 $self->log->debug("Authentication successful for '$username'");
48 4 ▶ return $user->id // $username;
Condition33%1/3
$user->id // $username
l!l&&r!l&&!r
400
MC/DC0%0/2
$user->id // $username
$user->id $username
Truth table33%1/3
A // B
A= $user->idB= $username
ABresult
1X1
011
000
49 }
50  
51 1 $self->log->info("Authentication failed for '$username' (invalid password)");
52 1 return undef;
53 }
54  
55 # ── Load user ─────────────────────────────────────────────────────────
56  
57 11 ▶sub load_user ($self, $app, $uid) {
Subroutine load_user: called CC=9 SCAR=23.7
Pod undocumented
58 #$self->log->debug("load_user called for uid='$uid'");
59  
60 # The Authentication plugin calls load_user with the controller as
61 # first argument, not the Mojolicious app. Use it directly.
62 11 ▶ my $c = ref($app) ? $app : $app->build_controller;
Branch: 1 branch
BranchTrueFalseTotal
ref $app ? : 11 0 11
63 11 my $schema = $self->_schema($c);
64 11 ▶ return undef unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 11 11
65  
66 11 my $source = $self->_source($c);
67 11 ▶ return undef unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 11 11
68  
69 11 my $user = $schema->resultset($source)->find($uid);
70 11 ▶ return undef unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 11 11
71  
72 11 ▶ my $result = {
Condition33%2/6
$user->id // $uid
l!l&&r!l&&!r
1100
$user->get_column($self->username_column) // $uid
l!l&&r!l&&!r
1100
MC/DC0%0/2
$user->id // $uid
$user->id $uid
MC/DC0%0/2
$user->get_column($self->username_column) // $uid
$user->get_column($self->username_column) $uid
Truth table33%1/3
A // B
A= $user->idB= $uid
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $user->get_column($self->username_column)B= $uid
ABresult
1X1
011
000
73 uid => $user->id // $uid,
74 username => $user->get_column($self->username_column) // $uid,
75 provider => 'dbix',
76 };
77  
78 # Copy all user fields except the password column
79 11 my $pass_col = $self->password_column;
80 11 for my $col ($user->result_source->columns) {
81 77 ▶ next if $col eq $pass_col;
Branch: 1 branch
BranchTrueFalseTotal
if $col eq $pass_col 11 66 77
82 66 $result->{$col} = $user->get_column($col);
83 }
84  
85 11 return $result;
86 }
87  
88 # ── Login form ────────────────────────────────────────────────────────
89  
90 2 ▶sub auth_form ($self, $c) {
Subroutine auth_form: called CC=2 SCAR=9.2
Pod undocumented
91 2 my $template_file = $c->app->home->child('templates/auth/login.html.ep');
92 2 ▶ if (-e $template_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $template_file) 0 2 2
93 0 my $mt = Mojo::Template->new;
94 0 my $content = $template_file->slurp;
95 0 return $mt->render($content, $c);
96 }
97  
98 2 return $c->render_to_string(inline => <<'HTML');
99 <h1><%= l 'Login' %></h1>
100 <form method="post">
101 <%= csrf_field %>
102 <label><%= l 'Username' %>:</label>
103 <input type="text" name="username" required><br>
104 <label><%= l 'Password' %>:</label>
105 <input type="password" name="password" required><br>
106 <button type="submit"><%= l 'Sign in' %></button>
107 </form>
108 HTML
109 }
110  
111 # ── Internal ──────────────────────────────────────────────────────────
112  
113 17 ▶sub _schema ($self, $c) {
Subroutine _schema: called CC=4 SCAR=14.0
114 17 ▶ return $self->{_schema} if $self->{_schema};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_schema"} 16 1 17
115  
116 1 my $be = $c->backend_config;
117 1 ▶ return undef unless $be;
Branch: 1 branch
BranchTrueFalseTotal
unless $be 0 1 1
118  
119 1 my $schema_class = $be->{schema_class};
120 1 ▶ return undef unless $schema_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_class 0 1 1
121  
122 $self->{_schema} = $schema_class->connect(
123 $be->{dsn}, $be->{user}, $be->{pass}, $be->{dbi_attrs},
124 1 );
125  
126 $self->log->debug(
127 1 sprintf("DBIx provider connected to %s via %s", $be->{dsn}, $schema_class));
128  
129 1 return $self->{_schema};
130 }
131  
132 17 ▶sub _source ($self, $c) {
Subroutine _source: called CC=2 SCAR=7.1
133 17 my $model_cfg = $c->model_config($self->model);
134 17 ▶ return undef unless $model_cfg;
Branch: 1 branch
BranchTrueFalseTotal
unless $model_cfg 0 17 17
135 17 return $model_cfg->{source};
136 }
137  
138 2 1;
139  
140 __END__
↑ Summary Auth/Provider.pm »