More changes to update-child process. Many scripts missing necessary data for checkin...
[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::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     if ( $bor->{'category_type'} eq 'C') {
80         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
81         my $cnt = scalar(@$catcodes);
82         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
83         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
84     }
85         
86 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
87 my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
88 $template->param( picture => 1 ) if $picture;
89         
90     $template->param( othernames => $bor->{'othernames'},
91             surname     => $bor->{'surname'},
92             firstname   => $bor->{'firstname'},
93             borrowernumber => $bor->{'borrowernumber'},
94             cardnumber => $bor->{'cardnumber'},
95             categorycode => $bor->{'categorycode'},
96             category_type => $bor->{'category_type'},
97             category_description => $bor->{'description'},
98             address => $bor->{'address'},
99             address2 => $bor->{'address2'},
100             city => $bor->{'city'},
101             zipcode => $bor->{'zipcode'},
102             phone => $bor->{'phone'},
103             email => $bor->{'email'},
104             branchcode => $bor->{'branchcode'},
105             userid      => $bor->{'userid'},
106             destination => $destination,
107                 is_child        => ($bor->{'category_type'} eq 'C'),
108             defaultnewpassword => $defaultnewpassword 
109         );
110
111
112 }
113
114 $template->param( member => $member,
115                                         errormsg => $errormsg,
116                                         $errormsg => 1 ,
117                                         minPasswordLength => $minpw );
118
119 output_html_with_http_headers $input, $cookie, $template->output;