lib/Strehler/Element/User.pm

filter: Statementstmt 94.8% 92 / 97 Branchbran 70.0% 14 / 20 Conditioncond 44.4% 8 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 95.2% 20 / 21 Podpod 25.0% 4 / 16 totaltotal 76.5% 140 / 183 CC 17 SCAR 12.0
CC
17
Cov
84%
error_message
7.5
save_form
5.7
valid_login
4.1
CRAP 3.3
« lib/Strehler/Element/Image.pm ↑ Summary blib/lib/Strehler/Element/Role/Maintainer.pm »
1 package Strehler::Element::User;
2 12 $Strehler::Element::User::VERSION = '1.6.8';
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Dancer2 0.166001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Dancer2::Plugin::DBIC;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Authen::Passphrase::BlowfishCrypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 extends 'Strehler::Element';
10  
11 sub metaclass_data
12 113 ▶{
Subroutine metaclass_data: called CC=1 SCAR=0.0
Pod undocumented
13 113 my $self = shift;
14 113 my $param = shift;
15 113 my %element_conf = ( item_type => 'user',
16 ORMObj => 'User',
17 category_accessor => '',
18 multilang_children => '' );
19 113 return $element_conf{$param};
20 }
21  
22 #Standard configuration overrides
23 sub exposed
24 1 ▶{
Subroutine exposed: called CC=1 SCAR=0.0
Pod undocumented
25 1 my $self = shift;
26 1 return $self->_property('exposed', 0);
27 }
28 sub label
29 1 ▶{
Subroutine label: called CC=1 SCAR=0.0
Pod undocumented
30 1 my $self = shift;
31 1 return $self->_property('label', 'Users');
32 }
33 sub allowed_role
34 7 ▶{
Subroutine allowed_role: called CC=1 SCAR=0.0
Pod undocumented
35 7 my $self = shift;
36 7 return $self->_property('allowed_role', 'admin');
37 }
38 sub class
39 1 ▶{
Subroutine class: called CC=1 SCAR=0.0
Pod undocumented
40 1 return __PACKAGE__;
41 }
42  
43  
44 #Main title redefined because here it's user
45 sub main_title
46 3 ▶{
Subroutine main_title: called CC=1 SCAR=0.0
Pod documented
47 3 my $self = shift;
48 3 return $self->get_attr('user');
49 }
50  
51 sub fields_list
52 1 ▶{
Subroutine fields_list: called CC=1 SCAR=0.0
Pod documented
53 1 my $self = shift;
54 1 my @fields = ( { 'id' => 'id',
55 'label' => 'ID',
56 'ordinable' => 1 },
57 { 'id' => 'user',
58 'label' => 'Username',
59 'ordinable' => 1 },
60 );
61 1 return \@fields;
62
63 }
64  
65 #Because of password management we need to override form management methods
66 sub get_form_data
67 1 ▶{
Subroutine get_form_data: called CC=1 SCAR=0.0
Pod undocumented
68 1 my $self = shift;
69 1 my $user_row = $self->row;
70 1 my $data;
71 1 $data->{'user'} = $self->get_attr('user');
72 1 $data->{'role'} = $self->get_attr('role');
73 1 return $data;
74 }
75  
76 sub save_form
77 2 ▶{
Subroutine save_form: called CC=5 SCAR=17.5
Pod undocumented
78 2 my $self = shift;
79 2 my $id = shift;
80 2 my $form = shift;
81
82 2 my $user_row;
83 2 my $clean_password = $form->param_value('password');
84 2 my $user_data = $self->generate_crypted_password($clean_password);
85 2 $user_data->{ user } = $form->param_value('user');
86 2 $user_data->{ role } = $form->param_value('role');
87 2 ▶ if($user_data->{ user })
Branch: 1 branch
BranchTrueFalseTotal
if ($user_data->{"user"}) 2 0 2
88 {
89 2 my $already_user = $self->get_schema()->resultset($self->ORMObj())->find({user => $form->param_value('user')});
90 2 ▶ return -1 if($already_user && ! $id);
Branch: 1 branch
BranchTrueFalseTotal
if $already_user and not $id 0 2 2
Condition33%1/3
$already_user and not $id
!ll&&!rl&&r
200
MC/DC0%0/2
$already_user and not $id
$already_user not $id
Truth table33%1/3
A and not(B)
A= $already_userB= not $id
ABresult
0X0
100
111
91 }
92 2 ▶ if($id)
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 2 2
93 {
94 0 delete $user_data->{ user };
95 0 $user_row = $self->get_schema()->resultset($self->ORMObj())->find($id);
96 0 $user_row->update($user_data);
97 }
98 else
99 {
100 2 $user_row = $self->get_schema()->resultset($self->ORMObj())->create($user_data);
101 }
102 2 return $user_row->id;
103 }
104  
105 sub save_password
106 1 ▶{
Subroutine save_password: called CC=2 SCAR=6.9
Pod undocumented
107 1 my $self = shift;
108 1 my $id = shift;
109 1 my $form = shift;
110  
111 1 my $clean_password = $form->param_value('password');
112 1 my $user_data = $self->generate_crypted_password($clean_password);
113 1 my $user_row = $self->get_schema()->resultset($self->ORMObj())->find($id);
114 1 ▶ return -1 if( ! $user_row );
Branch: 1 branch
BranchTrueFalseTotal
unless $user_row 0 1 1
115 1 $user_row->update($user_data);
116 1 return $user_row->id;
117 }
118  
119 sub generate_crypted_password
120 3 ▶{
Subroutine generate_crypted_password: called CC=1 SCAR=0.0
Pod undocumented
121 3 my $self = shift;
122 3 my $clean_password = shift;
123 3 my $ppr = Authen::Passphrase::BlowfishCrypt->new(
124 cost => 8, salt_random => 1,
125 passphrase => $clean_password);
126 3 return { password_hash => $ppr->hash_base64, password_salt => $ppr->salt_base64 };
127 }
128  
129 sub valid_login
130 6 ▶{
Subroutine valid_login: called CC=4 SCAR=14.1
Pod undocumented
131 6 my $self = shift;
132 6 my $user = shift;
133 6 my $password = shift;
134 6 my $rs = $self->get_schema()->resultset($self->ORMObj())->find({'user' => $user});
135 6 ▶ if($rs && $rs->user eq $user)
Branch: 1 branch
BranchTrueFalseTotal
if ($rs and $rs->user eq $user) 6 0 6
Condition33%1/3
$rs and $rs->user eq $user
!ll&&!rl&&r
006
MC/DC0%0/2
$rs and $rs->user eq $user
$rs $rs->user eq $user
Truth table33%1/3
A and B
A= $rsB= $rs->user eq $user
ABresult
0X0
100
111
136 {
137 6 my $ppr = Authen::Passphrase::BlowfishCrypt->new(
138 cost => 8, salt_base64 => $rs->password_salt,
139 hash_base64 => $rs->password_hash);
140 6 ▶ if($ppr->match($password))
Branch: 1 branch
BranchTrueFalseTotal
if ($ppr->match($password)) 5 1 6
141 {
142 5 return Strehler::Element::User->new($rs->id);
143 }
144 }
145 1 return undef;
146 }
147  
148 sub get_from_username
149 8 ▶{
Subroutine get_from_username: called CC=3 SCAR=11.0
Pod undocumented
150 8 my $self = shift;
151 8 my $username = shift;
152 8 my $rs = $self->get_schema()->resultset($self->ORMObj())->find({'user' => $username});
153 8 ▶ if($rs && $rs->user eq $username)
Branch: 1 branch
BranchTrueFalseTotal
if ($rs and $rs->user eq $username) { } 5 3 8
Condition66%2/3
$rs and $rs->user eq $username
!ll&&!rl&&r
305
MC/DC50%1/2
$rs and $rs->user eq $username
$rs $rs->user eq $username
Truth table66%2/3
A and B
A= $rsB= $rs->user eq $username
ABresult
0X0
100
111
154 {
155 5 return $self->new($rs->id);
156 }
157 else
158 {
159 3 return undef;
160 }
161 }
162  
163 sub install
164 0 ▶{
Subroutine install: not called CC=1 SCAR=6.9
Pod undocumented
165 0 return "Standard entity. No installation is needed.";
166 }
167  
168 sub delete
169 2 ▶{
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
170 2 my $self = shift;
171 2 ▶ if($self->get_attr("user") eq "admin")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_attr("user") eq "admin") 1 1 2
172 {
173 1 return -2;
174 }
175 1 return $self->SUPER::delete();
176 }
177  
178 sub error_message
179 2 ▶{
Subroutine error_message: called CC=6 SCAR=20.2
Pod documented
180 2 my $self = shift;
181 2 my $action = shift;
182 2 my $code = shift;
183 2 ▶ if($action eq 'delete' && $code == -2)
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'delete' and $code == -2) { } 1 1 2
elsif ($action eq 'add' || $action eq 'edit' and $code == -1) { } 0 1 1
Condition66%2/3
$action eq "delete" and $code == -2
!ll&&!rl&&r
011
MC/DC50%1/2
$action eq "delete" and $code == -2
$action eq "delete" $code == -2
Truth table66%2/3
A and B
A= $action eq "delete"B= $code == -2
ABresult
0X0
100
111
184 {
185 1 return "Admin user cannot be deleted";
186 }
187 ▶ elsif(($action eq 'add' || $action eq 'edit') && $code == -1)
Condition33%2/6
$action eq "add" || $action eq "edit" and $code == -1
!ll&&!rl&&r
100
$action eq "add" or $action eq "edit"
l!l&&r!l&&!r
001
MC/DC0%0/3
$action eq "add" || $action eq "edit" and $code == -1
$action eq "add" $action eq "edit" $code == -1
Truth table20%1/5
A or B and C
A= $action eq "add"B= $action eq "edit"C= $code == -1
ABCresult
00X0
1X00
0100
1X11
0111
188 {
189 0 return "User already exists";
190 }
191 else
192 {
193 1 return $self->SUPER::error_message($action, $code);
194 }
195 }
196  
197  
198  
199 =encoding utf8
200  
201 =head1 NAME
202  
203 Strehler::Element::User - Strehler Entity for users
204  
205 =head1 DESCRIPTION
206  
207 A little entity used to manage users.
208  
209 It keeps also password saving logic. Password is saved using L<Authen::Passphrase::BlowfishCrypt>
210  
211 =cut
212  
213  
214 12 1;
« lib/Strehler/Element/Image.pm ↑ Summary blib/lib/Strehler/Element/Role/Maintainer.pm »