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