3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use C4::Auth qw( get_template_and_user checkpw checkpw_hash );
24 use C4::Output qw( output_html_with_http_headers );
27 use Try::Tiny qw( catch try );
31 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
33 template_name => "opac-reset-password.tt",
40 my $op = $query->param('op');
42 if ( $op eq 'update' ) {
43 my $userid = $query->param('userid');
44 my $currentpassword = $query->param('currentpassword');
45 my $newpassword = $query->param('newpassword');
46 my $confirmpassword = $query->param('confirmpassword');
48 my $patron = Koha::Patrons->find( { userid => $userid } );
49 $patron = Koha::Patrons->find( { cardnumber => $userid } ) unless $patron;
51 if ( $patron && $patron->password_expiration_date ) {
52 if ( $patron->account_locked ) {
53 $template->param( error => 'account_locked' );
55 elsif ( $currentpassword && $newpassword && $confirmpassword ) {
57 if ( C4::Auth::checkpw_hash( $currentpassword, $patron->password ) ) {
59 if ( $newpassword ne $confirmpassword ) {
60 $template->param( 'error' => 'passwords_mismatch' );
62 elsif ( $currentpassword eq $newpassword ) {
63 $template->param( 'error' => 'no_change' );
67 $patron->set_password( { password => $newpassword } );
68 $template->param( 'password_updated' => '1' );
69 $template->param( 'staff_access' => 1 )
70 if $patron->has_permission( { catalogue => 1 } );
73 $error = 'password_too_short'
74 if $_->isa('Koha::Exceptions::Password::TooShort');
75 $error = 'password_too_weak'
76 if $_->isa('Koha::Exceptions::Password::TooWeak');
77 $error = 'password_has_whitespaces'
79 'Koha::Exceptions::Password::WhitespaceCharacters');
80 $template->param( 'error' => $error );
85 $template->param( 'error' => 'invalid_credentials' );
87 { login_attempts => $patron->login_attempts + 1 } )
88 if !$patron->account_locked;
92 $template->param( 'incomplete_form' => '1' );
96 template->param( 'error' => 'invalid_credentials' );
98 elsif ( !$patron->password_expiration_date ) {
99 $template->param( 'error' => 'no_expire' );
102 $template->param( 'error' => 'unknown' );
106 output_html_with_http_headers $query, $cookie, $template->output, undef,
107 { force_no_caching => 1 };