Bug 36137: Make update_totalissues cron always skip the holds queue
[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 qw( get_template_and_user );
10 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
11 use C4::Context;
12 use CGI qw ( -utf8 );
13 use Koha::Token;
14
15 use Koha::Patrons;
16 use Koha::Patron::Categories;
17
18 use Try::Tiny qw( catch try );
19
20 my $input = CGI->new;
21
22 my $theme = $input->param('theme') || "default";
23
24 # only used if allowthemeoverride is set
25
26 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
27     {
28         template_name   => "members/member-password.tt",
29         query           => $input,
30         type            => "intranet",
31         flagsrequired   => { borrowers => 'edit_borrowers' },
32     }
33 );
34
35 my $patron_id    = $input->param('member');
36 my $destination  = $input->param('destination');
37 my $newpassword  = $input->param('newpassword');
38 my $newpassword2 = $input->param('newpassword2');
39 my $new_user_id  = $input->param('newuserid');
40
41 my @errors;
42
43 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
44 my $patron = Koha::Patrons->find( $patron_id );
45 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
46
47 my $category_type = $patron->category->category_type;
48
49 if ( ( $patron_id ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
50     push( @errors, 'NOPERMISSION' )
51       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
52
53     # need superlibrarian for koha-conf.xml fakeuser.
54 }
55
56 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
57
58 if ( defined($newpassword) and not @errors) {
59
60     output_and_exit( $input, $cookie, $template,  'wrong_csrf_token' )
61         unless Koha::Token->new->check_csrf({
62             session_id => scalar $input->cookie('CGISESSID'),
63             token  => scalar $input->param('csrf_token'),
64         });
65
66     try {
67         if ( $newpassword ne '' ) {
68             $patron->set_password({ password => $newpassword });
69             $template->param( newpassword => $newpassword );
70         }
71         $patron->userid($new_user_id)->store
72             if $new_user_id and $new_user_id ne $patron->userid;
73         if ( $destination eq 'circ' ) {
74             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=" . $patron->cardnumber);
75         }
76         else {
77             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$patron_id");
78         }
79     }
80     catch {
81         if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
82             push @errors, 'ERROR_password_too_short';
83         }
84         elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
85             push @errors, 'ERROR_password_has_whitespaces';
86         }
87         elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
88             push @errors, 'ERROR_password_too_weak';
89         }
90         elsif ( $_->isa('Koha::Exceptions::Password::Plugin') ) {
91             push @errors, 'ERROR_from_plugin';
92         }
93         else {
94             push( @errors, 'BADUSERID' );
95         }
96     };
97 }
98
99 $template->param(
100     patron      => $patron,
101     destination => $destination,
102     csrf_token  => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
103 );
104
105 if ( scalar(@errors) ) {
106     $template->param( errormsg => 1 );
107     foreach my $error (@errors) {
108         $template->param($error) || $template->param( $error => 1 );
109     }
110 }
111
112 output_html_with_http_headers $input, $cookie, $template->output;