Bug 26922: Better error handling in SendAlerts
[koha.git] / members / member-password.pl
blob323eb88f850d56eb51ce7359e9e279df6741407b
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
7 use Modern::Perl;
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 Koha::AuthUtils;
17 use Koha::Token;
19 use Koha::Patrons;
20 use Koha::Patron::Categories;
22 use Try::Tiny;
24 my $input = CGI->new;
26 my $theme = $input->param('theme') || "default";
28 # only used if allowthemeoverride is set
30 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
32 template_name => "members/member-password.tt",
33 query => $input,
34 type => "intranet",
35 flagsrequired => { borrowers => 'edit_borrowers' },
36 debug => 1,
40 my $patron_id = $input->param('member');
41 my $destination = $input->param('destination');
42 my $newpassword = $input->param('newpassword');
43 my $newpassword2 = $input->param('newpassword2');
44 my $new_user_id = $input->param('newuserid');
46 my @errors;
48 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
49 my $patron = Koha::Patrons->find( $patron_id );
50 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
52 my $category_type = $patron->category->category_type;
54 if ( ( $patron_id ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
55 push( @errors, 'NOPERMISSION' )
56 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
58 # need superlibrarian for koha-conf.xml fakeuser.
61 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
63 if ( $newpassword and not @errors) {
65 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
66 unless Koha::Token->new->check_csrf({
67 session_id => scalar $input->cookie('CGISESSID'),
68 token => scalar $input->param('csrf_token'),
69 });
71 try {
72 $patron->set_password({ password => $newpassword });
73 $patron->userid($new_user_id)->store
74 if $new_user_id and $new_user_id ne $patron->userid;
75 $template->param( newpassword => $newpassword );
76 if ( $destination eq 'circ' ) {
77 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=" . $patron->cardnumber);
79 else {
80 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$patron_id");
83 catch {
84 if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
85 push @errors, 'ERROR_password_too_short';
87 elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
88 push @errors, 'ERROR_password_has_whitespaces';
90 elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
91 push @errors, 'ERROR_password_too_weak';
93 elsif ( $_->isa('Koha::Exceptions::Password::Plugin') ) {
94 push @errors, 'ERROR_from_plugin';
96 else {
97 push( @errors, 'BADUSERID' );
102 $template->param(
103 patron => $patron,
104 destination => $destination,
105 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
108 if ( scalar(@errors) ) {
109 $template->param( errormsg => 1 );
110 foreach my $error (@errors) {
111 $template->param($error) || $template->param( $error => 1 );
115 output_html_with_http_headers $input, $cookie, $template->output;