Bug 20249: (bug 18789 follow-up) "Patron has no outstanding fines" now appears alongs...
[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 Modern::Perl;
8
9 use C4::Auth;
10 use Koha::AuthUtils;
11 use C4::Output;
12 use C4::Context;
13 use C4::Members;
14 use C4::Circulation;
15 use CGI qw ( -utf8 );
16 use C4::Members::Attributes qw(GetBorrowerAttributes);
17 use Koha::AuthUtils;
18 use Koha::Token;
19
20 use Koha::Patrons;
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 => 'edit_borrowers' },
36         debug           => 1,
37     }
38 );
39
40 my $member      = $input->param('member');
41 my $cardnumber  = $input->param('cardnumber');
42 my $destination = $input->param('destination');
43 my $newpassword  = $input->param('newpassword');
44 my $newpassword2 = $input->param('newpassword2');
45
46 my @errors;
47
48 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
49 my $patron = Koha::Patrons->find( $member );
50 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
51
52 my $category_type = $patron->category->category_type;
53 my $bor = $patron->unblessed;
54
55 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
56     push( @errors, 'NOPERMISSION' )
57       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
58
59     # need superlibrarian for koha-conf.xml fakeuser.
60 }
61
62 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
63
64 if ( $newpassword and not @errors ) {
65     my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $newpassword );
66     unless ( $is_valid ) {
67         push @errors, 'ERROR_password_too_short' if $error eq 'too_short';
68         push @errors, 'ERROR_password_too_weak' if $error eq 'too_weak';
69         push @errors, 'ERROR_password_has_whitespaces' if $error eq 'has_whitespaces';
70     }
71 }
72
73 if ( $newpassword and not @errors) {
74
75     die "Wrong CSRF token"
76         unless Koha::Token->new->check_csrf({
77             session_id => scalar $input->cookie('CGISESSID'),
78             token  => scalar $input->param('csrf_token'),
79         });
80
81     my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
82     my $uid    = $input->param('newuserid') || $bor->{userid};
83     my $dbh    = C4::Context->dbh;
84     if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
85         $template->param( newpassword => $newpassword );
86         if ( $destination eq 'circ' ) {
87             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
88         }
89         else {
90             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
91         }
92     }
93     else {
94         push( @errors, 'BADUSERID' );
95     }
96 }
97
98 if ( $patron->is_child ) {
99     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
100     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
101     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
102 }
103
104 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
105     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
106     $template->param(
107         ExtendedPatronAttributes => 1,
108         extendedattributes       => $attributes
109     );
110 }
111
112 $template->param(
113     patron                     => $patron,
114     destination                => $destination,
115     csrf_token                 => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
116 );
117
118 if ( scalar(@errors) ) {
119     $template->param( errormsg => 1 );
120     foreach my $error (@errors) {
121         $template->param($error) || $template->param( $error => 1 );
122     }
123 }
124
125 output_html_with_http_headers $input, $cookie, $template->output;