Bug 19921: Fix update child when only one adult patron category exist
[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::AuthUtils;
19 use Koha::Token;
20
21 use Koha::Patrons;
22 use Koha::Patron::Categories;
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 $patron = Koha::Patrons->find( $member );
53 unless ( $patron ) {
54     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
55     exit;
56 }
57
58 my $category_type = $patron->category->category_type;
59 my $bor = $patron->unblessed;
60
61 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
62     push( @errors, 'NOPERMISSION' )
63       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
64
65     # need superlibrarian for koha-conf.xml fakeuser.
66 }
67
68 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
69
70 if ( $newpassword and not @errors ) {
71     my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $newpassword );
72     unless ( $is_valid ) {
73         push @errors, 'ERROR_password_too_short' if $error eq 'too_short';
74         push @errors, 'ERROR_password_too_weak' if $error eq 'too_weak';
75         push @errors, 'ERROR_password_has_whitespaces' if $error eq 'has_whitespaces';
76     }
77 }
78
79 if ( $newpassword and not @errors) {
80
81     die "Wrong CSRF token"
82         unless Koha::Token->new->check_csrf({
83             session_id => scalar $input->cookie('CGISESSID'),
84             token  => scalar $input->param('csrf_token'),
85         });
86
87     my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
88     my $uid    = $input->param('newuserid') || $bor->{userid};
89     my $dbh    = C4::Context->dbh;
90     if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
91         $template->param( newpassword => $newpassword );
92         if ( $destination eq 'circ' ) {
93             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
94         }
95         else {
96             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
97         }
98     }
99     else {
100         push( @errors, 'BADUSERID' );
101     }
102 }
103
104 if ( $category_type eq 'C') {
105     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
106     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
107     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
108 }
109
110 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
111
112 $template->param( picture => 1 ) if $patron->image;
113
114 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
115     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
116     $template->param(
117         ExtendedPatronAttributes => 1,
118         extendedattributes       => $attributes
119     );
120 }
121
122 $template->param(
123     othernames                 => $bor->{'othernames'},
124     surname                    => $bor->{'surname'},
125     firstname                  => $bor->{'firstname'},
126     borrowernumber             => $bor->{'borrowernumber'},
127     cardnumber                 => $bor->{'cardnumber'},
128     categorycode               => $bor->{'categorycode'},
129     category_type              => $category_type,
130     categoryname               => $bor->{'description'},
131     address                    => $bor->{address},
132     address2                   => $bor->{'address2'},
133     streettype                 => $bor->{streettype},
134     city                       => $bor->{'city'},
135     state                      => $bor->{'state'},
136     zipcode                    => $bor->{'zipcode'},
137     country                    => $bor->{'country'},
138     phone                      => $bor->{'phone'},
139     phonepro                   => $bor->{'phonepro'},
140     streetnumber               => $bor->{'streetnumber'},
141     mobile                     => $bor->{'mobile'},
142     email                      => $bor->{'email'},
143     emailpro                   => $bor->{'emailpro'},
144     branchcode                 => $bor->{'branchcode'},
145     userid                     => $bor->{'userid'},
146     destination                => $destination,
147     is_child                   => ( $category_type eq 'C' ),
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;