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