Bug 9611: (follow-up) move new password hashing routines to separate module
[koha.git] / members / member-password.pl
1 #!/usr/bin/perl
2 #script to set the password, and optionally a userid, for a borrower
3 #written 2/5/00
4 #by chris@katipo.co.nz
5 #converted to using templates 3/16/03 by mwhansen@hmc.edu
6
7 use strict;
8 use warnings;
9
10 use C4::Auth;
11 use Koha::AuthUtils;
12 use C4::Output;
13 use C4::Context;
14 use C4::Members;
15 use C4::Branch;
16 use C4::Circulation;
17 use CGI;
18 use C4::Members::Attributes qw(GetBorrowerAttributes);
19
20 use Digest::MD5 qw(md5_base64);
21
22 my $input = new CGI;
23
24 my $theme = $input->param('theme') || "default";
25                         # only used if allowthemeoverride is set
26
27 my ($template, $loggedinuser, $cookie, $staffflags)
28     = get_template_and_user({template_name => "members/member-password.tmpl",
29                              query => $input,
30                              type => "intranet",
31                              authnotrequired => 0,
32                              flagsrequired => {borrowers => 1},
33                              debug => 1,
34                              });
35
36 my $flagsrequired;
37 $flagsrequired->{borrowers}=1;
38
39 #my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired, 'intranet');
40
41 my $member=$input->param('member');
42 my $cardnumber = $input->param('cardnumber');
43 my $destination = $input->param('destination');
44 my @errors;
45 my ($bor)=GetMember('borrowernumber' => $member);
46 if(( $member ne $loggedinuser ) && ($bor->{'category_type'} eq 'S' ) ) {
47         push(@errors,'NOPERMISSION') unless($staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
48         # need superlibrarian for koha-conf.xml fakeuser.
49 }
50 my $newpassword = $input->param('newpassword');
51 my $newpassword2 = $input->param('newpassword2');
52
53 push(@errors,'NOMATCH') if ( ( $newpassword && $newpassword2 ) && ($newpassword ne $newpassword2) );
54
55 my $minpw = C4::Context->preference('minPasswordLength');
56 push(@errors,'SHORTPASSWORD') if( $newpassword && $minpw && (length($newpassword) < $minpw ) );
57
58 if ( $newpassword  && !scalar(@errors) ) {
59     my $digest=Koha::AuthUtils::hash_password($input->param('newpassword'));
60     my $uid = $input->param('newuserid');
61     my $dbh=C4::Context->dbh;
62     if (changepassword($uid,$member,$digest)) {
63                 $template->param(newpassword => $newpassword);
64                 if ($destination eq 'circ') {
65                     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");               
66                 } else {
67                     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
68                 }
69     } else {
70                         push(@errors,'BADUSERID');
71     }
72 } else {
73     my $userid = $bor->{'userid'};
74
75     my $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
76     my $length=int(rand(2))+C4::Context->preference("minPasswordLength");
77     my $defaultnewpassword='';
78     for (my $i=0; $i<$length; $i++) {
79         $defaultnewpassword.=substr($chars, int(rand(length($chars))),1);
80     }
81
82         $template->param( defaultnewpassword => $defaultnewpassword );
83 }
84     if ( $bor->{'category_type'} eq 'C') {
85         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
86         my $cnt = scalar(@$catcodes);
87         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
88         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
89     }
90         
91 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
92 my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
93 $template->param( picture => 1 ) if $picture;
94
95 if (C4::Context->preference('ExtendedPatronAttributes')) {
96     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
97     $template->param(
98         ExtendedPatronAttributes => 1,
99         extendedattributes => $attributes
100     );
101 }
102
103     $template->param( othernames => $bor->{'othernames'},
104             surname     => $bor->{'surname'},
105             firstname   => $bor->{'firstname'},
106             borrowernumber => $bor->{'borrowernumber'},
107             cardnumber => $bor->{'cardnumber'},
108             categorycode => $bor->{'categorycode'},
109             category_type => $bor->{'category_type'},
110             categoryname => $bor->{'description'},
111             address => $bor->{'address'},
112             address2 => $bor->{'address2'},
113             city => $bor->{'city'},
114             state => $bor->{'state'},
115             zipcode => $bor->{'zipcode'},
116             country => $bor->{'country'},
117             phone => $bor->{'phone'},
118             email => $bor->{'email'},
119             branchcode => $bor->{'branchcode'},
120             branchname => GetBranchName($bor->{'branchcode'}),
121             userid      => $bor->{'userid'},
122             destination => $destination,
123                 is_child        => ($bor->{'category_type'} eq 'C'),
124                 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
125         minPasswordLength => $minpw,
126         RoutingSerials => C4::Context->preference('RoutingSerials'),
127         );
128
129 if( scalar(@errors )){
130         $template->param( errormsg => 1 );
131         foreach my $error (@errors) {
132         $template->param($error) || $template->param( $error => 1);
133         }
134
135 }
136
137 output_html_with_http_headers $input, $cookie, $template->output;