Mojito/Auth/Deep.pm

filter: Statementstmt 29.8% 17 / 57 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 4 / 10 Podpod 100% 4 / 4 totaltotal 26.6% 25 / 94 CC 9 SCAR 24.4
CC
9
Cov
23%
get_user
20.0
remove_user
20.0
add_user
12.0
CRAP 11.4
« Mojito/Auth/Elasticsearch.pm ↑ Summary Mojito/Page/Render.pm »
1 4 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mojito::Auth::Deep;
3 4 $Mojito::Auth::Deep::VERSION = '0.25';
4 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mojito::Page::CRUD::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 with('Mojito::Role::DB::Deep');
9  
10 =head1 Name
11  
12 Mojito::Auth::Deep - authentication delegatee class for DBM::Deep
13  
14 =cut
15  
16 has editer => (
17 is => 'ro',
18 lazy => 1,
19 0 ▶ default => sub { Mojito::Page::CRUD::Deep->new(collection_name => 'users') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 4 );
21 =head1 Methods
22  
23 =head2 add_user
24  
25 Provide the username, realm (default Mojito) and password.
26  
27 =cut
28  
29 0 ▶sub add_user {
Subroutine add_user: not called CC=3 SCAR=24.8
Pod documented
30 0 my ($self, $args) = @_;
31  
32 0 ▶ my $username = $args->{username} || $self->username;
Condition0%0/3
$args->{'username'} || $self->username
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'username'} || $self->username
$args->{'username'} $self->username
Truth table0%0/3
A || B
A= $args->{'username'}B= $self->username
ABresult
1X1
011
000
33 0 ▶ if ($self->get_user($username)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_user($username)) 0 0 0
34 0 warn "Username '$username' already taken!";
35 0 return;
36 }
37 0 my @digest_input_parts = qw/ username realm password /;
38 0 my $digest_input = join ':', map { $self->$_ } @digest_input_parts;
39 0 my $HA1 = Digest::MD5::md5_hex($digest_input);
40 0 my $md5_password = Digest::MD5::md5_hex( $self->password );
41 # TODO - Make sure backend support unique user names
42 # For Mongo we can ensure an index, but a general technique is
43 # to check for existence of a username before attempting to add it.
44 0 my $id = $self->editer->create(
45 {
46 first_name => $self->first_name,
47 last_name => $self->last_name,
48 email => $self->email,
49 username => $self->username,
50 realm => $self->realm,
51 HA1 => $HA1,
52 password => $md5_password
53 }
54 );
55  
56 0 return $id;
57 }
58  
59 =head2 get_user
60  
61 Get a user from the database.
62  
63 =cut
64  
65 0 ▶sub get_user {
Subroutine get_user: not called CC=4 SCAR=30.0
Pod documented
66 0 my ( $self, $username ) = @_;
67 0 ▶ $username //= $self->username;
Condition0%0/3
$username //= $self->username
l!l&&r!l&&!r
000
MC/DC0%0/2
$username //= $self->username
$username $self->username
Truth table0%0/3
A //= B
A= $usernameB= $self->username
ABresult
1X1
011
000
68 0 ▶ return if !$username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 0 0
69 # If we don't have any users yet, the be somewhat graceful about it
70 0 ▶ return if !$self->collection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->collection 0 0 0
71  
72 # Get collection
73 0 my $collection = $self->collection->export;
74 0 my @users = values %{$collection};
75 0 ▶ my $user = first {$_->{username} eq $username} @users;
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 return $user;
77 }
78  
79 =head2 remove_user
80  
81 Remove a user from the database.
82  
83 =cut
84  
85 0 ▶sub remove_user {
Subroutine remove_user: not called CC=4 SCAR=30.0
Pod documented
86 0 my ( $self, $username ) = @_;
87 0 ▶ $username //= $self->username;
Condition0%0/3
$username //= $self->username
l!l&&r!l&&!r
000
MC/DC0%0/2
$username //= $self->username
$username $self->username
Truth table0%0/3
A //= B
A= $usernameB= $self->username
ABresult
1X1
011
000
88 0 ▶ return if !$username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 0 0
89 # Just in case we have multiple occurrences of the same user
90 0 my $collection = $self->collection->export;
91 0 my @users = values %{$collection};
92 0 my @wanted_users = grep {$_->{username} eq $username} @users;
93 0 my @wanted_ids = map {$_->{id} } @wanted_users;
94 0 my $users_deleted = 0;
95 0 foreach my $id (@wanted_ids) {
96 0 delete $self->collection->{$id};
97 0 $users_deleted++;
98 }
99 0 return $users_deleted;
100 }
101  
102 # Apply the role after the (role) required interface is defined (get_user, add_user)
103 4 with('Mojito::Auth::Role');
104  
105 =head2 BUILD
106  
107 Set some things post object construction, pre object use.
108  
109 =cut
110  
111 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
112 0 my $self = shift;
113  
114 # We use the users collection for Auth stuff
115 0 $self->collection_name('users');
116 }
117  
118 4 1;
« Mojito/Auth/Elasticsearch.pm ↑ Summary Mojito/Page/Render.pm »