Bug 14598: (QA followup) Don't die on bad barcode
[koha.git] / members / member-password.pl
blobd2255f8ddfb4f57042611842da6e312465fd9a95
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::Patron::Images;
19 use Koha::Token;
21 use Koha::Patron::Categories;
23 use Digest::MD5 qw(md5_base64);
25 my $input = new CGI;
27 my $theme = $input->param('theme') || "default";
29 # only used if allowthemeoverride is set
31 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
33 template_name => "members/member-password.tt",
34 query => $input,
35 type => "intranet",
36 authnotrequired => 0,
37 flagsrequired => { borrowers => 1 },
38 debug => 1,
42 my $flagsrequired;
43 $flagsrequired->{borrowers} = 1;
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');
51 my @errors;
53 my ($bor) = GetMember( 'borrowernumber' => $member );
55 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
56 push( @errors, 'NOPERMISSION' )
57 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
59 # need superlibrarian for koha-conf.xml fakeuser.
62 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
64 my $minpw = C4::Context->preference('minPasswordLength');
65 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
67 if ( $newpassword && !scalar(@errors) ) {
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 });
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");
84 else {
85 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
88 else {
89 push( @errors, 'BADUSERID' );
92 else {
93 my $userid = $bor->{'userid'};
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 );
102 $template->param( defaultnewpassword => $defaultnewpassword );
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;
111 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
113 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
114 $template->param( picture => 1 ) if $patron_image;
116 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
117 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
118 $template->param(
119 ExtendedPatronAttributes => 1,
120 extendedattributes => $attributes
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') ),
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;