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