Bug 17100: Do not display payments if patron has nothing to pay
[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::Branch;
16 use C4::Circulation;
17 use CGI qw ( -utf8 );
18 use C4::Members::Attributes qw(GetBorrowerAttributes);
19 use Koha::Patron::Images;
20 use Koha::Token;
21
22 use Digest::MD5 qw(md5_base64);
23
24 my $input = new CGI;
25
26 my $theme = $input->param('theme') || "default";
27
28 # only used if allowthemeoverride is set
29
30 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
31     {
32         template_name   => "members/member-password.tt",
33         query           => $input,
34         type            => "intranet",
35         authnotrequired => 0,
36         flagsrequired   => { borrowers => 1 },
37         debug           => 1,
38     }
39 );
40
41 my $flagsrequired;
42 $flagsrequired->{borrowers} = 1;
43
44 my $member      = $input->param('member');
45 my $cardnumber  = $input->param('cardnumber');
46 my $destination = $input->param('destination');
47 my $newpassword  = $input->param('newpassword');
48 my $newpassword2 = $input->param('newpassword2');
49
50 my @errors;
51
52 my ($bor) = GetMember( 'borrowernumber' => $member );
53
54 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
55     push( @errors, 'NOPERMISSION' )
56       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
57
58     # need superlibrarian for koha-conf.xml fakeuser.
59 }
60
61 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
62
63 my $minpw = C4::Context->preference('minPasswordLength');
64 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
65
66 if ( $newpassword && !scalar(@errors) ) {
67
68     die "Wrong CSRF token"
69         unless Koha::Token->new->check_csrf({
70             id     => C4::Context->userenv->{id},
71             secret => md5_base64( C4::Context->config('pass') ),
72             token  => scalar $input->param('csrf_token'),
73         });
74
75     my $digest = Koha::AuthUtils::hash_password( $input->param('newpassword') );
76     my $uid    = $input->param('newuserid') || $bor->{userid};
77     my $dbh    = C4::Context->dbh;
78     if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
79         $template->param( newpassword => $newpassword );
80         if ( $destination eq 'circ' ) {
81             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
82         }
83         else {
84             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
85         }
86     }
87     else {
88         push( @errors, 'BADUSERID' );
89     }
90 }
91 else {
92     my $userid = $bor->{'userid'};
93
94     my $chars              = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
95     my $length             = int( rand(2) ) + C4::Context->preference("minPasswordLength");
96     my $defaultnewpassword = '';
97     for ( my $i = 0 ; $i < $length ; $i++ ) {
98         $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
99     }
100
101     $template->param( defaultnewpassword => $defaultnewpassword );
102 }
103
104 if ( $bor->{'category_type'} eq 'C' ) {
105     my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
106     my $cnt = scalar(@$catcodes);
107     $template->param( 'CATCODE_MULTI' => 1 ) if $cnt > 1;
108     $template->param( 'catcode' => $catcodes->[0] ) if $cnt == 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     branchname                 => GetBranchName( $bor->{'branchcode'} ),
147     userid                     => $bor->{'userid'},
148     destination                => $destination,
149     is_child                   => ( $bor->{'category_type'} eq 'C' ),
150     activeBorrowerRelationship => ( C4::Context->preference('borrowerRelationship') ne '' ),
151     minPasswordLength          => $minpw,
152     RoutingSerials             => C4::Context->preference('RoutingSerials'),
153     csrf_token                 => Koha::Token->new->generate_csrf({
154         id     => C4::Context->userenv->{id},
155         secret => md5_base64( C4::Context->config('pass') ),
156     }),
157 );
158
159 if ( scalar(@errors) ) {
160     $template->param( errormsg => 1 );
161     foreach my $error (@errors) {
162         $template->param($error) || $template->param( $error => 1 );
163     }
164 }
165
166 output_html_with_http_headers $input, $cookie, $template->output;