2090ddae93e038ab88612809bb497d9b13650d6b
[koha.git] / opac / opac-reset-password.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21
22 use C4::Auth qw( get_template_and_user checkpw checkpw_hash );
23 use C4::Context;
24 use C4::Output qw( output_html_with_http_headers );
25 use Koha::Patrons;
26
27 use Try::Tiny qw( catch try );
28
29 my $query = CGI->new;
30
31 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
32     {
33         template_name   => "opac-reset-password.tt",
34         query           => $query,
35         type            => "opac",
36         authnotrequired => 1,
37     }
38 );
39
40 my $op = $query->param('op');
41
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');
47
48     my $patron = Koha::Patrons->find( { userid => $userid } );
49     $patron = Koha::Patrons->find( { cardnumber => $userid } ) unless $patron;
50
51     if ( $patron && $patron->password_expiration_date ) {
52         if ( $patron->account_locked ) {
53             $template->param( error => 'account_locked' );
54         }
55         elsif ( $currentpassword && $newpassword && $confirmpassword ) {
56             my $error;
57             if ( C4::Auth::checkpw_hash( $currentpassword, $patron->password ) ) {
58
59                 if ( $newpassword ne $confirmpassword ) {
60                     $template->param( 'error' => 'passwords_mismatch' );
61                 }
62                 elsif ( $currentpassword eq $newpassword ) {
63                     $template->param( 'error' => 'no_change' );
64                 }
65                 else {
66                     try {
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 } );
71                     }
72                     catch {
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'
78                           if $_->isa(
79                             'Koha::Exceptions::Password::WhitespaceCharacters');
80                         $template->param( 'error' => $error );
81                     };
82                 }
83             }
84             else {
85                 $template->param( 'error' => 'invalid_credentials' );
86                 $patron->update(
87                     { login_attempts => $patron->login_attempts + 1 } )
88                   if !$patron->account_locked;
89             }
90         }
91         else {
92             $template->param( 'incomplete_form' => '1' );
93         }
94     }
95     elsif ( !$patron ) {
96         template->param( 'error' => 'invalid_credentials' );
97     }
98     elsif ( !$patron->password_expiration_date ) {
99         $template->param( 'error' => 'no_expire' );
100     }
101     else {
102         $template->param( 'error' => 'unknown' );
103     }
104 }
105
106 output_html_with_http_headers $query, $cookie, $template->output, undef,
107   { force_no_caching => 1 };