moving members related scripts to "members" directory
[wip/koha-chris_n.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::Interface::CGI::Output;
11 use C4::Search;
12 use C4::Context;
13 use C4::Circulation::Circ2;
14 use CGI;
15 use HTML::Template;
16 use Digest::MD5 qw(md5_base64);
17
18 my $input = new CGI;
19
20 my $theme = $input->param('theme') || "default";
21                         # only used if allowthemeoverride is set
22
23 my ($template, $loggedinuser, $cookie)
24     = get_template_and_user({template_name => "members/member-password.tmpl",
25                              query => $input,
26                              type => "intranet",
27                              authnotrequired => 0,
28                              flagsrequired => {borrowers => 1},
29                              debug => 1,
30                              });
31
32 my $flagsrequired;
33 $flagsrequired->{borrowers}=1;
34 my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired);
35
36 my $member=$input->param('member');
37 my %env;
38 $env{'nottodayissues'}=1;
39 my %member2;
40 $member2{'borrowernumber'}=$member;
41 my $issues=currentissues(\%env,\%member2);
42 my $i=0;
43 foreach (sort keys %$issues) {
44     $i++;
45 }
46
47 my ($bor,$flags)=getpatroninformation(\%env, $member,'');
48 my $newpassword = $input->param('newpassword');
49
50 if ( $newpassword ) {
51     my $digest=md5_base64($input->param('newpassword'));
52     my $uid = $input->param('newuserid');
53     my $dbh=C4::Context->dbh;
54
55         #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
56         #Then we need to tell the user and have them create a new one.
57         my $sth2=$dbh->prepare("select * from borrowers where userid=? and borrowernumber != ?");
58         $sth2->execute($uid,$member);
59
60         if ( ($uid ne '') && ($sth2->fetchrow) ) {
61                 #The userid exists so we should display a warning.
62                 my $warn = 1;
63         $template->param( warn => $warn,
64                         othernames => $bor->{'othernames'},
65                         surname     => $bor->{'surname'},
66                         firstname   => $bor->{'firstname'},
67                         userid      => $bor->{'userid'},
68                         defaultnewpassword => $newpassword );
69     } else {
70                 #Everything is good so we can update the information.
71                 my $sth=$dbh->prepare("update borrowers set userid=?, password=? where borrowernumber=?");
72         $sth->execute($uid, $digest, $member);
73                 $template->param(newpassword => $newpassword);
74         }
75
76 } else {
77     my $userid = $bor->{'userid'};
78
79     my $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
80     my $length=int(rand(2))+4;
81     my $defaultnewpassword='';
82     for (my $i=0; $i<$length; $i++) {
83         $defaultnewpassword.=substr($chars, int(rand(length($chars))),1);
84     }
85         $template->param(       othernames => $bor->{'othernames'},
86                         surname     => $bor->{'surname'},
87                         firstname   => $bor->{'firstname'},
88                         userid      => $bor->{'userid'},
89                         defaultnewpassword => $defaultnewpassword );
90
91
92 }
93
94 $template->param( member => $member );
95
96 output_html_with_http_headers $input, $cookie, $template->output;