|
Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Patron::Password; |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 6 |
# terms of the GNU General Public License as published by the Free Software |
| 7 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 8 |
# version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License along |
| 15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Mojo::Base 'Mojolicious::Controller'; |
| 21 |
|
| 22 |
use C4::Context; |
| 23 |
use C4::Members; |
| 24 |
use Koha::AuthUtils qw(hash_password); |
| 25 |
use Koha::Database; |
| 26 |
use Koha::Patron::Password::Recovery qw( |
| 27 |
SendPasswordRecoveryEmail |
| 28 |
ValidateBorrowernumber |
| 29 |
CompletePasswordRecovery |
| 30 |
); |
| 31 |
use Koha::Patrons; |
| 32 |
|
| 33 |
use Koha::Exceptions; |
| 34 |
use Koha::Exceptions::Authentication; |
| 35 |
use Koha::Exceptions::Authorization; |
| 36 |
|
| 37 |
use Try::Tiny; |
| 38 |
|
| 39 |
sub recovery { |
| 40 |
my $c = shift->openapi->valid_input or return; |
| 41 |
|
| 42 |
my $patron; |
| 43 |
return try { |
| 44 |
my $body = $c->req->json; |
| 45 |
|
| 46 |
unless (C4::Context->preference('OpacPasswordChange') and |
| 47 |
C4::Context->preference('OpacPasswordReset')) |
| 48 |
{ |
| 49 |
return $c->render(status => 403, openapi => { |
| 50 |
error => 'Password recovery is disabled.' |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
unless (defined $body->{userid} or defined $body->{cardnumber}) { |
| 55 |
Koha::Exceptions::BadParameter->throw( |
| 56 |
error => 'Either userid or cardnumber must be given.' |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
my $patron = Koha::Patrons->search({ |
| 61 |
email => $body->{email}, |
| 62 |
'-or' => { |
| 63 |
userid => $body->{userid}, |
| 64 |
cardnumber => $body->{cardnumber}, |
| 65 |
} |
| 66 |
})->next; |
| 67 |
|
| 68 |
unless ($patron) { |
| 69 |
Koha::Exceptions::Patron::NotFound->throw( |
| 70 |
error => 'Patron not found' |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
my $link = $body->{'custom_link'}; |
| 75 |
my $skip_mail = 0; |
| 76 |
if ($body->{'skip_mail'}) { |
| 77 |
Koha::Exceptions::Authentication::Required->throw( |
| 78 |
error => 'Authentication required while skip_mail parameter is on' |
| 79 |
) unless ref($c->stash('koha.user')) eq 'Koha::Patron'; |
| 80 |
if (my $user = $c->stash('koha.user')) { |
| 81 |
Koha::Exceptions::Authorization::Unauthorized->throw( |
| 82 |
error => 'Permission get_password_reset_uuid required while ' |
| 83 |
.'skip_mail parameter is on' |
| 84 |
) unless C4::Auth::haspermission($user->userid, { |
| 85 |
borrowers => 'get_password_reset_uuid' |
| 86 |
}); |
| 87 |
$skip_mail = 1; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
my $resend = ValidateBorrowernumber($patron->borrowernumber); |
| 92 |
|
| 93 |
my $ret = SendPasswordRecoveryEmail($patron, $patron->email, $resend, { |
| 94 |
url => $link, |
| 95 |
skip_mail => $skip_mail, |
| 96 |
}); |
| 97 |
|
| 98 |
if (ref($ret) eq 'HASH') { |
| 99 |
$ret->{status} = 2; |
| 100 |
return $c->render(status => 201, openapi => $ret); |
| 101 |
} |
| 102 |
|
| 103 |
return $c->render(status => 201, openapi => { |
| 104 |
status => 1, |
| 105 |
to_address => $patron->email |
| 106 |
}); |
| 107 |
} |
| 108 |
catch { |
| 109 |
if ($_->isa('Koha::Exceptions::BadParameter')) { |
| 110 |
return $c->render(status => 400, openapi => { error => $_->error }); |
| 111 |
} |
| 112 |
elsif ($_->isa('Koha::Exceptions::Patron::NotFound')) { |
| 113 |
return $c->render(status => 404, openapi => { error => $_->error }); |
| 114 |
} |
| 115 |
elsif ($_->isa('Koha::Exceptions::WrongParameter')) { |
| 116 |
return $c->render(status => 400, openapi => { error => $_->error }); |
| 117 |
} |
| 118 |
elsif ($_->isa('Koha::Exceptions::Authentication::Required')) { |
| 119 |
return $c->render(status => 401, openapi => { error => $_->error }); |
| 120 |
} |
| 121 |
elsif ($_->isa('Koha::Exceptions::Authorization::Unauthorized')) { |
| 122 |
return $c->render(status => 403, openapi => { error => $_->error }); |
| 123 |
} |
| 124 |
Koha::Exceptions::rethrow_exception($_); |
| 125 |
}; |
| 126 |
} |
| 127 |
|
| 128 |
sub complete_recovery { |
| 129 |
my $c = shift->openapi->valid_input or return; |
| 130 |
|
| 131 |
my $rs = Koha::Database->new->schema->resultset('BorrowerPasswordRecovery'); |
| 132 |
return try { |
| 133 |
my $body = $c->req->json; |
| 134 |
|
| 135 |
my $password_recovery = $rs->find({ |
| 136 |
uuid => $body->{uuid} |
| 137 |
}); |
| 138 |
unless ($password_recovery) { |
| 139 |
return $c->render(status => 404, openapi => { |
| 140 |
error => 'Password recovery request with given uuid not found.' |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
my $patron = Koha::Patrons->find($password_recovery->borrowernumber); |
| 145 |
my $categorycode = $patron->categorycode; |
| 146 |
my ($success, $error, $errmsg) = C4::Members::ValidateMemberPassword( |
| 147 |
$categorycode, $body->{new_password}, $body->{confirm_new_password} |
| 148 |
); |
| 149 |
if ($error) { |
| 150 |
return $c->render(status => 400, openapi => { |
| 151 |
error => $errmsg |
| 152 |
}); |
| 153 |
} |
| 154 |
my $password = $body->{new_password}; |
| 155 |
$patron->update_password( $patron->userid, hash_password($password) ); |
| 156 |
return $c->render(status => 200, openapi => {}); |
| 157 |
} |
| 158 |
catch { |
| 159 |
Koha::Exceptions::rethrow_exception($_); |
| 160 |
}; |
| 161 |
} |
| 162 |
|
| 163 |
1; |