Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / opac / opac-passwd.pl
1 #!/usr/bin/perl
2 # This script lets the users change the passwords by themselves.
3 #
4 # (c) 2005 Universidad ORT Uruguay.
5 #
6 # This file is part of the extensions and enhacments made to koha by Universidad ORT Uruguay
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24
25 use C4::Auth;    # checkauth, getborrowernumber.
26 use C4::Context;
27 use C4::Circulation;
28 use C4::Members;
29 use C4::Output;
30 use Koha::Patrons;
31
32 use Try::Tiny;
33
34 my $query = CGI->new;
35
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-passwd.tt",
39         query           => $query,
40         type            => "opac",
41     }
42 );
43
44 my $patron = Koha::Patrons->find( $borrowernumber );
45 if ( $patron->category->effective_change_password ) {
46     if (   $query->param('Oldkey')
47         && $query->param('Newkey')
48         && $query->param('Confirm') )
49     {
50         my $error;
51         my $new_password = $query->param('Newkey');
52         my $confirm_password = $query->param('Confirm');
53         if ( C4::Auth::checkpw_hash( scalar $query->param('Oldkey'), $patron->password ) ) {
54
55             if ( $new_password ne $confirm_password ) {
56                 $template->param( 'Ask_data'       => '1' );
57                 $template->param( 'Error_messages' => '1' );
58                 $template->param( 'passwords_mismatch'   => '1' );
59             } else {
60                 try {
61                     $patron->set_password({ password => $new_password });
62                     $template->param( 'password_updated' => '1' );
63                     $template->param( 'borrowernumber'   => $borrowernumber );
64                 }
65                 catch {
66                     $error = 'password_too_short'
67                         if $_->isa('Koha::Exceptions::Password::TooShort');
68                     $error = 'password_too_weak'
69                         if $_->isa('Koha::Exceptions::Password::TooWeak');
70                     $error = 'password_has_whitespaces'
71                         if $_->isa('Koha::Exceptions::Password::WhitespaceCharacters');
72                 };
73             }
74         }
75         else {
76             $error = 'WrongPass';
77         }
78         if ($error) {
79             $template->param(
80                 Ask_data       => 1,
81                 Error_messages => 1,
82                 $error         => 1,
83             );
84
85         }
86     }
87     else {
88
89         # Called Empty, Ask for data.
90         $template->param( 'Ask_data' => '1' );
91         if (!$query->param('Oldkey') && ($query->param('Newkey') || $query->param('Confirm'))){
92             # Old password is empty but one of the others isn't
93             $template->param( 'Error_messages' => '1' );
94             $template->param( 'WrongPass'      => '1' );
95         }
96         elsif ($query->param('Oldkey') && (!$query->param('Newkey') || !$query->param('Confirm'))){
97             # Oldpassword is entered but one of the other fields is empty
98             $template->param( 'Error_messages' => '1' );
99             $template->param( 'PassMismatch'   => '1' );
100         }
101     }
102 }
103 $template->param(
104     firstname  => $patron->firstname,
105     surname    => $patron->surname,
106     passwdview => 1,
107 );
108
109
110 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };