Bug 15956: Rearranging some SIP unit tests
[koha.git] / opac / opac-passwd.pl
blob4b33413f3e7d95a29d60977ff51ff43d6ab42204
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);
34 my $query = new CGI;
35 my $dbh = C4::Context->dbh;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39 template_name => "opac-passwd.tt",
40 query => $query,
41 type => "opac",
42 authnotrequired => 0,
43 debug => 1,
47 my $borr = C4::Members::GetMember( borrowernumber => $borrowernumber );
48 my $minpasslen = C4::Context->preference("minPasswordLength");
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 if ( goodkey( $dbh, $borrowernumber, $query->param('Oldkey') ) ) {
56 if ( $query->param('Newkey') =~ m|^\s+| or $query->param('Newkey') =~ m|\s+$| ) {
57 $template->param(
58 Error_messages => 1,
59 PasswordContainsTrailingSpaces => 1,
62 elsif ( $query->param('Newkey') eq $query->param('Confirm')
63 && length( $query->param('Confirm') ) >= $minpasslen )
64 { # Record password
65 my $clave = hash_password( $query->param('Newkey') );
66 $sth->execute( $clave, $borrowernumber );
67 $template->param( 'password_updated' => '1' );
68 $template->param( 'borrowernumber' => $borrowernumber );
70 elsif ( $query->param('Newkey') ne $query->param('Confirm') ) {
71 $template->param( 'Ask_data' => '1' );
72 $template->param( 'Error_messages' => '1' );
73 $template->param( 'PassMismatch' => '1' );
75 elsif ( length( $query->param('Confirm') ) < $minpasslen ) {
76 $template->param( 'Ask_data' => '1' );
77 $template->param( 'Error_messages' => '1' );
78 $template->param( 'ShortPass' => '1' );
80 else {
81 $template->param( 'Error_messages' => '1' );
84 else {
85 $template->param( 'Ask_data' => '1' );
86 $template->param( 'Error_messages' => '1' );
87 $template->param( 'WrongPass' => '1' );
90 else {
92 # Called Empty, Ask for data.
93 $template->param( 'Ask_data' => '1' );
94 if (!$query->param('Oldkey') && ($query->param('Newkey') || $query->param('Confirm'))){
95 # Old password is empty but one of the others isnt
96 $template->param( 'Error_messages' => '1' );
97 $template->param( 'WrongPass' => '1' );
99 elsif ($query->param('Oldkey') && (!$query->param('Newkey') || !$query->param('Confirm'))){
100 # Oldpassword is entered but one of the other fields is empty
101 $template->param( 'Error_messages' => '1' );
102 $template->param( 'PassMismatch' => '1' );
106 $template->param(firstname => $borr->{'firstname'},
107 surname => $borr->{'surname'},
108 minpasslen => $minpasslen,
109 passwdview => 1,
112 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
114 sub goodkey {
115 my ( $dbh, $borrowernumber, $key ) = @_;
117 my $sth =
118 $dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
119 $sth->execute($borrowernumber);
120 if ( $sth->rows ) {
121 my $hash;
122 my ($stored_hash) = $sth->fetchrow;
123 if ( substr($stored_hash,0,2) eq '$2') {
124 $hash = hash_password($key, $stored_hash);
125 } else {
126 $hash = md5_base64($key);
128 if ( $hash eq $stored_hash ) { return 1; }
129 else { return 0; }
131 else { return 0; }