Merge git://git.koha.org/pub/scm/koha
[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 C4::Auth;
9 use C4::Output;
10 use C4::Context;
11 use C4::Members;
12 use C4::Circulation;
13 use CGI;
14
15 use Digest::MD5 qw(md5_base64);
16
17 my $input = new CGI;
18
19 my $theme = $input->param('theme') || "default";
20                         # only used if allowthemeoverride is set
21
22 my ($template, $loggedinuser, $cookie, $staffflags)
23     = get_template_and_user({template_name => "members/member-password.tmpl",
24                              query => $input,
25                              type => "intranet",
26                              authnotrequired => 0,
27                              flagsrequired => {borrowers => 1},
28                              debug => 1,
29                              });
30
31 my $flagsrequired;
32 $flagsrequired->{borrowers}=1;
33
34 #my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired);
35
36 my $member=$input->param('member');
37 my $cardnumber = $input->param('cardnumber');
38 my $destination = $input->param('destination');
39 my $errormsg;
40 my ($bor)=GetMember($member);
41 if(( $member ne $loggedinuser ) && ($bor->{'category_type'} eq 'S' ) ) {
42         $errormsg = 'NOPERMISSION' unless($staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
43         # need superlibrarian for koha-conf.xml fakeuser.
44 }
45 my $newpassword = $input->param('newpassword');
46 my $minpw = C4::Context->preference('minPasswordLength');
47 $errormsg = 'SHORTPASSWORD' if( $newpassword && $minpw & (length($newpassword) < $minpw ) );
48
49 if ( $newpassword  && ! $errormsg ) {
50     my $digest=md5_base64($input->param('newpassword'));
51     my $uid = $input->param('newuserid');
52     my $dbh=C4::Context->dbh;
53     if (changepassword($uid,$member,$digest)) {
54                 $template->param(newpassword => $newpassword);
55                 if ($destination eq 'circ') {
56                     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");               
57                 } else {
58                     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
59                 }
60     } else {
61                         $errormsg = 'BADUSERID';
62             $template->param(othernames => $bor->{'othernames'},
63                                                 surname     => $bor->{'surname'},
64                                                 firstname   => $bor->{'firstname'},
65                                                 userid      => $bor->{'userid'},
66                                                 defaultnewpassword => $newpassword 
67                                                 );
68     }
69 } else {
70     my $userid = $bor->{'userid'};
71
72     my $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
73     my $length=int(rand(2))+4;
74     my $defaultnewpassword='';
75     for (my $i=0; $i<$length; $i++) {
76         $defaultnewpassword.=substr($chars, int(rand(length($chars))),1);
77     }
78         
79         my $borrowercategory = GetBorrowercategory( $bor->{'categorycode'} );
80 my $category_type = $borrowercategory->{'category_type'};
81 ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
82         
83     $template->param( othernames => $bor->{'othernames'},
84             surname     => $bor->{'surname'},
85             firstname   => $bor->{'firstname'},
86             borrowernumber => $bor->{'borrowernumber'},
87             cardnumber => $bor->{'cardnumber'},
88             categorycode => $bor->{'categorycode'},
89             category_type => $bor->{'category_type'},
90             category_description => $bor->{'description'},
91             address => $bor->{'address'},
92             address2 => $bor->{'address2'},
93             city => $bor->{'city'},
94             zipcode => $bor->{'zipcode'},
95             phone => $bor->{'phone'},
96             email => $bor->{'email'},
97             branchcode => $bor->{'branchcode'},
98             userid      => $bor->{'userid'},
99             destination => $destination,
100             defaultnewpassword => $defaultnewpassword 
101         );
102
103
104 }
105
106 $template->param( member => $member,
107                                         errormsg => $errormsg,
108                                         $errormsg => 1 ,
109                                         minPasswordLength => $minpw );
110
111 output_html_with_http_headers $input, $cookie, $template->output;