Bug 9302: Add button directing use to keeper's patron record
[koha.git] / opac / opac-passwd.pl
blobb5ae895947562929fbf4bbd106127595b93f2101
1 #!/usr/bin/perl
2 # This script lets the users change the passwords by themselves.
4 # (c) 2005 Universidad ORT Uruguay.
6 # This file is part of the extensions and enhacments made to koha by Universidad ORT Uruguay
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.
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.
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>.
21 use strict;
22 use warnings;
24 use CGI qw ( -utf8 );
26 use C4::Auth; # checkauth, getborrowernumber.
27 use C4::Context;
28 use Digest::MD5 qw(md5_base64);
29 use C4::Circulation;
30 use C4::Members;
31 use C4::Output;
32 use Koha::AuthUtils qw(hash_password);
33 use Koha::Patrons;
35 my $query = new CGI;
36 my $dbh = C4::Context->dbh;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 template_name => "opac-passwd.tt",
41 query => $query,
42 type => "opac",
43 authnotrequired => 0,
44 debug => 1,
48 my $patron = Koha::Patrons->find( $borrowernumber );
49 if ( C4::Context->preference("OpacPasswordChange") ) {
50 my $sth = $dbh->prepare("UPDATE borrowers SET password = ? WHERE borrowernumber=?");
51 if ( $query->param('Oldkey')
52 && $query->param('Newkey')
53 && $query->param('Confirm') )
55 my $error;
56 my $new_password = $query->param('Newkey');
57 my $confirm_password = $query->param('Confirm');
58 if ( goodkey( $dbh, $borrowernumber, $query->param('Oldkey') ) ) {
60 if ( $new_password ne $confirm_password ) {
61 $template->param( 'Ask_data' => '1' );
62 $template->param( 'Error_messages' => '1' );
63 $template->param( 'passwords_mismatch' => '1' );
64 } else {
65 my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $new_password );
66 unless ( $is_valid ) {
67 $error = 'password_too_short' if $error eq 'too_short';
68 $error = 'password_too_weak' if $error eq 'too_weak';
69 $error = 'password_has_whitespaces' if $error eq 'has_whitespaces';
70 } else {
71 # Password is valid and match
72 my $clave = hash_password( $new_password );
73 $sth->execute( $clave, $borrowernumber );
74 $template->param( 'password_updated' => '1' );
75 $template->param( 'borrowernumber' => $borrowernumber );
79 else {
80 $error = 'WrongPass';
82 if ($error) {
83 $template->param(
84 Ask_data => 1,
85 Error_messages => 1,
86 $error => 1,
91 else {
93 # Called Empty, Ask for data.
94 $template->param( 'Ask_data' => '1' );
95 if (!$query->param('Oldkey') && ($query->param('Newkey') || $query->param('Confirm'))){
96 # Old password is empty but one of the others isnt
97 $template->param( 'Error_messages' => '1' );
98 $template->param( 'WrongPass' => '1' );
100 elsif ($query->param('Oldkey') && (!$query->param('Newkey') || !$query->param('Confirm'))){
101 # Oldpassword is entered but one of the other fields is empty
102 $template->param( 'Error_messages' => '1' );
103 $template->param( 'PassMismatch' => '1' );
107 $template->param(
108 firstname => $patron->firstname,
109 surname => $patron->surname,
110 passwdview => 1,
114 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
116 sub goodkey {
117 my ( $dbh, $borrowernumber, $key ) = @_;
119 my $sth =
120 $dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
121 $sth->execute($borrowernumber);
122 if ( $sth->rows ) {
123 my $hash;
124 my ($stored_hash) = $sth->fetchrow;
125 if ( substr($stored_hash,0,2) eq '$2') {
126 $hash = hash_password($key, $stored_hash);
127 } else {
128 $hash = md5_base64($key);
130 if ( $hash eq $stored_hash ) { return 1; }
131 else { return 0; }
133 else { return 0; }