Bug 19214: prevent crash if club_template_field is called in list context
[koha.git] / members / member-password.pl
blobcb922da43c10504783c06d809543d3d30e7d6441
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
7 use strict;
8 use warnings;
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::Token;
20 use Koha::Patrons;
21 use Koha::Patron::Categories;
23 my $input = new CGI;
25 my $theme = $input->param('theme') || "default";
27 # only used if allowthemeoverride is set
29 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
31 template_name => "members/member-password.tt",
32 query => $input,
33 type => "intranet",
34 authnotrequired => 0,
35 flagsrequired => { borrowers => 1 },
36 debug => 1,
40 my $flagsrequired;
41 $flagsrequired->{borrowers} = 1;
43 my $member = $input->param('member');
44 my $cardnumber = $input->param('cardnumber');
45 my $destination = $input->param('destination');
46 my $newpassword = $input->param('newpassword');
47 my $newpassword2 = $input->param('newpassword2');
49 my @errors;
51 my $patron = Koha::Patrons->find( $member );
52 unless ( $patron ) {
53 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
54 exit;
57 my $category_type = $patron->category->category_type;
58 my $bor = $patron->unblessed;
60 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
61 push( @errors, 'NOPERMISSION' )
62 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
64 # need superlibrarian for koha-conf.xml fakeuser.
67 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
69 my $minpw = C4::Context->preference('minPasswordLength');
70 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
72 if ( $newpassword && !scalar(@errors) ) {
74 die "Wrong CSRF token"
75 unless Koha::Token->new->check_csrf({
76 session_id => scalar $input->cookie('CGISESSID'),
77 token => scalar $input->param('csrf_token'),
78 });
80 my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
81 my $uid = $input->param('newuserid') || $bor->{userid};
82 my $dbh = C4::Context->dbh;
83 if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
84 $template->param( newpassword => $newpassword );
85 if ( $destination eq 'circ' ) {
86 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
88 else {
89 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
92 else {
93 push( @errors, 'BADUSERID' );
96 else {
97 my $userid = $bor->{'userid'};
99 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
100 my $length = int( rand(2) ) + C4::Context->preference("minPasswordLength");
101 my $defaultnewpassword = '';
102 for ( my $i = 0 ; $i < $length ; $i++ ) {
103 $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
106 $template->param( defaultnewpassword => $defaultnewpassword );
109 if ( $category_type eq 'C') {
110 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
111 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
112 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
115 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
117 $template->param( picture => 1 ) if $patron->image;
119 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
120 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
121 $template->param(
122 ExtendedPatronAttributes => 1,
123 extendedattributes => $attributes
127 $template->param(
128 othernames => $bor->{'othernames'},
129 surname => $bor->{'surname'},
130 firstname => $bor->{'firstname'},
131 borrowernumber => $bor->{'borrowernumber'},
132 cardnumber => $bor->{'cardnumber'},
133 categorycode => $bor->{'categorycode'},
134 category_type => $category_type,
135 categoryname => $bor->{'description'},
136 address => $bor->{address},
137 address2 => $bor->{'address2'},
138 streettype => $bor->{streettype},
139 city => $bor->{'city'},
140 state => $bor->{'state'},
141 zipcode => $bor->{'zipcode'},
142 country => $bor->{'country'},
143 phone => $bor->{'phone'},
144 phonepro => $bor->{'phonepro'},
145 streetnumber => $bor->{'streetnumber'},
146 mobile => $bor->{'mobile'},
147 email => $bor->{'email'},
148 emailpro => $bor->{'emailpro'},
149 branchcode => $bor->{'branchcode'},
150 userid => $bor->{'userid'},
151 destination => $destination,
152 is_child => ( $category_type eq 'C' ),
153 minPasswordLength => $minpw,
154 RoutingSerials => C4::Context->preference('RoutingSerials'),
155 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
158 if ( scalar(@errors) ) {
159 $template->param( errormsg => 1 );
160 foreach my $error (@errors) {
161 $template->param($error) || $template->param( $error => 1 );
165 output_html_with_http_headers $input, $cookie, $template->output;