Merge remote-tracking branch 'kc/new/bug_5995' into kcmaster
[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 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
94 if (C4::Context->preference('ExtendedPatronAttributes')) {
95     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
96     $template->param(
97         ExtendedPatronAttributes => 1,
98         extendedattributes => $attributes
99     );
100 }
101
102     $template->param( othernames => $bor->{'othernames'},
103             surname     => $bor->{'surname'},
104             firstname   => $bor->{'firstname'},
105             borrowernumber => $bor->{'borrowernumber'},
106             cardnumber => $bor->{'cardnumber'},
107             categorycode => $bor->{'categorycode'},
108             category_type => $bor->{'category_type'},
109             categoryname => $bor->{'description'},
110             address => $bor->{'address'},
111             address2 => $bor->{'address2'},
112             city => $bor->{'city'},
113             state => $bor->{'state'},
114             zipcode => $bor->{'zipcode'},
115             country => $bor->{'country'},
116             phone => $bor->{'phone'},
117             email => $bor->{'email'},
118             branchcode => $bor->{'branchcode'},
119             branchname => GetBranchName($bor->{'branchcode'}),
120             userid      => $bor->{'userid'},
121             destination => $destination,
122                 is_child        => ($bor->{'category_type'} eq 'C'),
123             defaultnewpassword => $defaultnewpassword,
124         );
125
126
127 }
128
129 $template->param( member => $member,
130                                         errormsg => $errormsg,
131                                         $errormsg => 1 ,
132                                         minPasswordLength => $minpw );
133
134 output_html_with_http_headers $input, $cookie, $template->output;