Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 qw( get_template_and_user checkpw checkpw_hash );
26 use C4::Context;
27 use C4::Output qw( output_html_with_http_headers );
28 use Koha::Patrons;
29
30 use Try::Tiny qw( catch try );
31
32 my $query = CGI->new;
33
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35     {
36         template_name   => "opac-passwd.tt",
37         query           => $query,
38         type            => "opac",
39     }
40 );
41
42 my $patron = Koha::Patrons->find( $borrowernumber );
43 if ( $patron->category->effective_change_password ) {
44     if (   $query->param('Oldkey')
45         && $query->param('Newkey')
46         && $query->param('Confirm') )
47     {
48         my $error;
49         my $new_password = $query->param('Newkey');
50         my $confirm_password = $query->param('Confirm');
51         if ( C4::Auth::checkpw_hash( scalar $query->param('Oldkey'), $patron->password ) ) {
52
53             if ( $new_password ne $confirm_password ) {
54                 $template->param( 'Ask_data'       => '1' );
55                 $template->param( 'Error_messages' => '1' );
56                 $template->param( 'passwords_mismatch'   => '1' );
57             } else {
58                 try {
59                     $patron->set_password({ password => $new_password });
60                     $template->param( 'password_updated' => '1' );
61                     $template->param( 'borrowernumber'   => $borrowernumber );
62                 }
63                 catch {
64                     $error = 'password_too_short'
65                         if $_->isa('Koha::Exceptions::Password::TooShort');
66                     $error = 'password_too_weak'
67                         if $_->isa('Koha::Exceptions::Password::TooWeak');
68                     $error = 'password_has_whitespaces'
69                         if $_->isa('Koha::Exceptions::Password::WhitespaceCharacters');
70                 };
71             }
72         }
73         else {
74             $error = 'WrongPass';
75         }
76         if ($error) {
77             $template->param(
78                 Ask_data       => 1,
79                 Error_messages => 1,
80                 $error         => 1,
81             );
82
83         }
84     }
85     else {
86
87         # Called Empty, Ask for data.
88         $template->param( 'Ask_data' => '1' );
89         if (!$query->param('Oldkey') && ($query->param('Newkey') || $query->param('Confirm'))){
90             # Old password is empty but one of the others isn't
91             $template->param( 'Error_messages' => '1' );
92             $template->param( 'WrongPass'      => '1' );
93         }
94         elsif ($query->param('Oldkey') && (!$query->param('Newkey') || !$query->param('Confirm'))){
95             # Oldpassword is entered but one of the other fields is empty
96             $template->param( 'Error_messages' => '1' );
97             $template->param( 'PassMismatch'   => '1' );
98         }
99     }
100 }
101 $template->param(
102     firstname  => $patron->firstname,
103     surname    => $patron->surname,
104     passwdview => 1,
105 );
106
107
108 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };