Bug 7976: Remove the borrow permission
[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 strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25
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
34 my $query = new CGI;
35 my $dbh   = C4::Context->dbh;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => "opac-passwd.tt",
40         query           => $query,
41         type            => "opac",
42         authnotrequired => 0,
43         debug           => 1,
44     }
45 );
46
47 # get borrower information ....
48 my ( $borr ) = GetMemberDetails( $borrowernumber );
49 my $minpasslen = C4::Context->preference("minPasswordLength");
50 if ( C4::Context->preference("OpacPasswordChange") ) {
51     my $sth =  $dbh->prepare("UPDATE borrowers SET password = ? WHERE borrowernumber=?");
52     if (   $query->param('Oldkey')
53         && $query->param('Newkey')
54         && $query->param('Confirm') )
55     {
56         if ( goodkey( $dbh, $borrowernumber, $query->param('Oldkey') ) ) {
57             if ( $query->param('Newkey') eq $query->param('Confirm')
58                 && length( $query->param('Confirm') ) >= $minpasslen )
59             {    # Record password
60                 my $clave = hash_password( $query->param('Newkey') );
61                 $sth->execute( $clave, $borrowernumber );
62                 $template->param( 'password_updated' => '1' );
63                 $template->param( 'borrowernumber'   => $borrowernumber );
64             }
65             elsif ( $query->param('Newkey') ne $query->param('Confirm') ) {
66                 $template->param( 'Ask_data'       => '1' );
67                 $template->param( 'Error_messages' => '1' );
68                 $template->param( 'PassMismatch'   => '1' );
69             }
70             elsif ( length( $query->param('Confirm') ) < $minpasslen ) {
71                 $template->param( 'Ask_data'       => '1' );
72                 $template->param( 'Error_messages' => '1' );
73                 $template->param( 'ShortPass'      => '1' );
74             }
75             else {
76                 $template->param( 'Error_messages' => '1' );
77             }
78         }
79         else {
80             $template->param( 'Ask_data'       => '1' );
81             $template->param( 'Error_messages' => '1' );
82             $template->param( 'WrongPass'      => '1' );
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 isnt
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(firstname => $borr->{'firstname'},
102                                                         surname => $borr->{'surname'},
103                                                         minpasslen => $minpasslen,
104                                                         passwdview => 1,
105 );
106
107 output_html_with_http_headers $query, $cookie, $template->output;
108
109 sub goodkey {
110     my ( $dbh, $borrowernumber, $key ) = @_;
111
112     my $sth =
113       $dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
114     $sth->execute($borrowernumber);
115     if ( $sth->rows ) {
116         my $hash;
117         my ($stored_hash) = $sth->fetchrow;
118         if ( substr($stored_hash,0,2) eq '$2') {
119             $hash = hash_password($key, $stored_hash);
120         } else {
121             $hash = md5_base64($key);
122         }
123         if ( $hash eq $stored_hash ) { return 1; }
124         else { return 0; }
125     }
126     else { return 0; }
127 }