Bug 16911: (QA followup) CGI->param should not be called in list context
[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 Koha::AuthUtils;
12 use C4::Output;
13 use C4::Context;
14 use C4::Members;
15 use C4::Circulation;
16 use CGI qw ( -utf8 );
17 use C4::Members::Attributes qw(GetBorrowerAttributes);
18 use Koha::Patron::Images;
19 use Koha::Token;
20
21 use Koha::Patron::Categories;
22
23 use Digest::MD5 qw(md5_base64);
24
25 my $input = new CGI;
26
27 my $theme = $input->param('theme') || "default";
28
29 # only used if allowthemeoverride is set
30
31 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
32     {
33         template_name   => "members/member-password.tt",
34         query           => $input,
35         type            => "intranet",
36         authnotrequired => 0,
37         flagsrequired   => { borrowers => 1 },
38         debug           => 1,
39     }
40 );
41
42 my $flagsrequired;
43 $flagsrequired->{borrowers} = 1;
44
45 my $member      = $input->param('member');
46 my $cardnumber  = $input->param('cardnumber');
47 my $destination = $input->param('destination');
48 my $newpassword  = $input->param('newpassword');
49 my $newpassword2 = $input->param('newpassword2');
50
51 my @errors;
52
53 my ($bor) = GetMember( 'borrowernumber' => $member );
54
55 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
56     push( @errors, 'NOPERMISSION' )
57       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
58
59     # need superlibrarian for koha-conf.xml fakeuser.
60 }
61
62 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
63
64 my $minpw = C4::Context->preference('minPasswordLength');
65 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
66
67 if ( $newpassword && !scalar(@errors) ) {
68
69     die "Wrong CSRF token"
70         unless Koha::Token->new->check_csrf({
71             id     => C4::Context->userenv->{id},
72             secret => md5_base64( C4::Context->config('pass') ),
73             token  => scalar $input->param('csrf_token'),
74         });
75
76     my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
77     my $uid    = $input->param('newuserid') || $bor->{userid};
78     my $dbh    = C4::Context->dbh;
79     if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
80         $template->param( newpassword => $newpassword );
81         if ( $destination eq 'circ' ) {
82             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
83         }
84         else {
85             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
86         }
87     }
88     else {
89         push( @errors, 'BADUSERID' );
90     }
91 }
92 else {
93     my $userid = $bor->{'userid'};
94
95     my $chars              = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
96     my $length             = int( rand(2) ) + C4::Context->preference("minPasswordLength");
97     my $defaultnewpassword = '';
98     for ( my $i = 0 ; $i < $length ; $i++ ) {
99         $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
100     }
101
102     $template->param( defaultnewpassword => $defaultnewpassword );
103 }
104
105 if ( $bor->{'category_type'} eq 'C') {
106     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
107     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
108     $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
109 }
110
111 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
112
113 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
114 $template->param( picture => 1 ) if $patron_image;
115
116 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
117     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
118     $template->param(
119         ExtendedPatronAttributes => 1,
120         extendedattributes       => $attributes
121     );
122 }
123
124 $template->param(
125     othernames                 => $bor->{'othernames'},
126     surname                    => $bor->{'surname'},
127     firstname                  => $bor->{'firstname'},
128     borrowernumber             => $bor->{'borrowernumber'},
129     cardnumber                 => $bor->{'cardnumber'},
130     categorycode               => $bor->{'categorycode'},
131     category_type              => $bor->{'category_type'},
132     categoryname               => $bor->{'description'},
133     address                    => $bor->{address},
134     address2                   => $bor->{'address2'},
135     streettype                 => $bor->{streettype},
136     city                       => $bor->{'city'},
137     state                      => $bor->{'state'},
138     zipcode                    => $bor->{'zipcode'},
139     country                    => $bor->{'country'},
140     phone                      => $bor->{'phone'},
141     phonepro                   => $bor->{'phonepro'},
142     mobile                     => $bor->{'mobile'},
143     email                      => $bor->{'email'},
144     emailpro                   => $bor->{'emailpro'},
145     branchcode                 => $bor->{'branchcode'},
146     userid                     => $bor->{'userid'},
147     destination                => $destination,
148     is_child                   => ( $bor->{'category_type'} eq 'C' ),
149     activeBorrowerRelationship => ( C4::Context->preference('borrowerRelationship') ne '' ),
150     minPasswordLength          => $minpw,
151     RoutingSerials             => C4::Context->preference('RoutingSerials'),
152     csrf_token                 => Koha::Token->new->generate_csrf({
153         id     => C4::Context->userenv->{id},
154         secret => md5_base64( C4::Context->config('pass') ),
155     }),
156 );
157
158 if ( scalar(@errors) ) {
159     $template->param( errormsg => 1 );
160     foreach my $error (@errors) {
161         $template->param($error) || $template->param( $error => 1 );
162     }
163 }
164
165 output_html_with_http_headers $input, $cookie, $template->output;