| 1 | package Yukki::Web::Controller::Login; | ||
| 2 | $Yukki::Web::Controller::Login::VERSION = '0.991_007'; # TRIAL | ||
| 3 | $Yukki::Web::Controller::Login::VERSION = '0.991007'; | ||
| 4 | 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__ |