blib/lib/Yukki/Web/Controller/Login.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 172 SCAR 58.3
CC
18
Cov
0%
update_profile
132.0
fire
42.0
check_login_submission
12.0
CRAP 342.0
1 package Yukki::Web::Controller::Login;
2 $Yukki::Web::Controller::Login::VERSION = '0.991_002'; # TRIAL
3  
4 $Yukki::Web::Controller::Login::VERSION = '0.991002';use v5.24;
5 use utf8;
6 use Moo;
7  
8 with 'Yukki::Web::Controller';
9  
10 use Email::Address;
11 use Yukki::Error qw( http_throw );
12 use Yukki::TextUtil qw( dump_file );
13  
14 use namespace::clean;
15  
16 # ABSTRACT: shows the login page and handles login
17  
18  
19 sub fire {
20 my ($self, $ctx) = @_;
21  
22 my $res;
23 my $action = $ctx->request->path_parameters->{action};
24 if ($action eq 'page') { $self->show_login_page($ctx) }
25 elsif ($action eq 'submit') { $self->check_login_submission($ctx) }
26 elsif ($action eq 'profile') { $self->show_profile_page($ctx) }
27 elsif ($action eq 'update') { $self->update_profile($ctx) }
28 elsif ($action eq 'exit') { $self->logout($ctx) }
29 else {
30 http_throw('That login action does not exist.', {
31 status => 'NotFound',
32 })
33 }
34 }
35  
36  
37 sub show_login_page {
38 my ($self, $ctx) = @_;
39  
40 $ctx->response->body( $self->view('Login')->page($ctx) );
41 }
42  
43  
44 sub show_profile_page {
45 my ($self, $ctx, $name, $email) = @_;
46  
47 $ctx->response->body(
48 $self->view('Login')->profile($ctx, $name, $email)
49 );
50 }
51  
52  
53 sub update_profile {
54 my ($self, $ctx) = @_;
55  
56 http_throw('You are not authorized to run this action.', {
57 status => 'Forbidden',
58 }) unless $ctx->session->{user};
59  
60 my $login_name = $ctx->request->body_parameters->{login_name};
61  
62 unless ($login_name eq $ctx->session->{user}{login_name}) {
63 $ctx->add_errors('Are you sure you are logged in as the correct user? Please make sure and try again.');
64 $ctx->response->redirect('/profile');
65 return;
66 }
67  
68 my $name = $ctx->request->body_parameters->{name};
69 my $email = $ctx->request->body_parameters->{email};
70  
71 $name =~ s/^\s+//; $name =~ s/\s+$//;
72 $email =~ s/^\s+//; $email =~ s/\s+$//;
73  
74 my $invalid = 0;
75 unless ($name =~ /\S+/) {
76 $ctx->add_errors('name must contain at least one letter');
77 $invalid++;
78 }
79  
80 my @emails = Email::Address->parse($email);
81 if (@emails != 1) {
82 $ctx->add_errors('that does not appear to be an email address');
83 $invalid++;
84 }
85  
86 if ($invalid) {
87 return $self->show_profile_page($ctx, $name, $email);
88 }
89  
90 use DDP;
91  
92 my %user = $ctx->session->{user}->%*;
93 p %user;
94 $user{name} = $name;
95 $user{email} = $email;
96  
97 my $password_old = $ctx->request->body_parameters->{password_old};
98 my $password_new = $ctx->request->body_parameters->{password_new};
99 my $password_con = $ctx->request->body_parameters->{password_confirm};
100  
101 # Only activate password check/reset if they use it
102 if (length $password_old) {
103 my $okay = $self->check_password(
104 $ctx->session->{user},
105 $password_old,
106 );
107  
108 unless ($okay) {
109 $ctx->add_errors('the current password you entered is incorrect');
110 return $self->show_profile_page($ctx, $name, $email);
111 }
112  
113 if (length($password_new) == 0) {
114 $ctx->add_errors('you must enter a new password');
115 return $self->show_profile_page($ctx, $name, $email);
116 }
117  
118 if ($password_old eq $password_new) {
119 $ctx->add_errors('the new and old passwords you entered are the same');
120 return $self->show_profile_page($ctx, $name, $email);
121 }
122  
123 if ($password_new ne $password_con) {
124 $ctx->add_errors('the new passwords you entered do not match');
125 return $self->show_profile_page($ctx, $name, $email);
126 }
127  
128 my $digest = $self->app->hasher;
129 $digest->add($password_new);
130 $user{password} = $digest->generate;
131 }
132  
133 my $user_file = $self->app->locate('user_path', $login_name);
134 chmod 0600, "$user_file";
135 dump_file($user_file, \%user);
136 chmod 0400, "$user_file";
137  
138 $ctx->session->{user} = \%user;
139  
140 $ctx->response->redirect('/profile');
141 return;
142 }
143  
144  
145 sub check_password {
146 my ($self, $user, $password) = @_;
147  
148 return scalar $self->app->hasher->validate(
149 $user->{password},
150 $password,
151 );
152 }
153  
154  
155 sub check_login_submission {
156 my ($self, $ctx) = @_;
157  
158 my $login_name = $ctx->request->body_parameters->{login_name};
159 my $password = $ctx->request->body_parameters->{password};
160  
161 my $user = $self->model('User')->find(login_name => $login_name);
162  
163 if (not ($user and $self->check_password($user, $password))) {
164 $ctx->add_errors('no such user or you typed your password incorrectly');
165 }
166  
167 if ($ctx->has_errors) {
168 $self->show_login_page($ctx);
169 return;
170 }
171  
172 else {
173 $ctx->session->{user} = $user;
174  
175 $ctx->response->redirect($ctx->rebase_url('page/view/main'));
176 return;
177 }
178 }
179  
180  
181 sub logout {
182 my ($self, $ctx) = @_;
183  
184 $ctx->session_options->{expire} = 1;
185 $ctx->response->redirect($ctx->rebase_url('page/view/main'));
186 }
187  
188 1;
189  
190 __END__
191  
192 =pod
193  
194 =encoding UTF-8
195  
196 =head1 NAME
197  
198 Yukki::Web::Controller::Login - shows the login page and handles login
199  
200 =head1 VERSION
201  
202 version 0.991_002
203  
204 =head1 DESCRIPTION
205  
206 Shows the login page and handles login.
207  
208 =head1 METHODS
209  
210 =head2 fire
211  
212 Routes page reqquests to L</show_login_page>, submit requests to L</check_login_submission>, and exit requests to L</logout>.
213  
214 =head2 show_login_page
215  
216 Calls L<Yukki::Web::View::Login/page> to display the login page.
217  
218 =head2 show_profile_page
219  
220 Calls L<Yukki::Web::View::Login/profile> to display the profile page.
221  
222 =head2 update_profile
223  
224 Validates the input user information and updates the user. Redirects the user back to the profile page.
225  
226 =head2 check_password
227  
228 Checks that the user's password is valid.
229  
230 =head2 check_login_submission
231  
232 Authenticates a user login.
233  
234 =head2 logout
235  
236 Expires the session, causing logout.
237  
238 =head1 AUTHOR
239  
240 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 This software is copyright (c) 2017 by Qubling Software LLC.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut