Bug 25184: (RM follow-up) Make DB update idempotent
[koha.git] / members / member-password.pl
blobe9d5f54d9f86a7ff5c3de08738e74786fbda9527
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 = new CGI;
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 authnotrequired => 0,
36 flagsrequired => { borrowers => 'edit_borrowers' },
37 debug => 1,
41 my $patron_id = $input->param('member');
42 my $destination = $input->param('destination');
43 my $newpassword = $input->param('newpassword');
44 my $newpassword2 = $input->param('newpassword2');
45 my $new_user_id = $input->param('newuserid');
47 my @errors;
49 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
50 my $patron = Koha::Patrons->find( $patron_id );
51 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
53 my $category_type = $patron->category->category_type;
55 if ( ( $patron_id ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
56 push( @errors, 'NOPERMISSION' )
57 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
59 # need superlibrarian for koha-conf.xml fakeuser.
62 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
64 if ( $newpassword and not @errors) {
66 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
67 unless Koha::Token->new->check_csrf({
68 session_id => scalar $input->cookie('CGISESSID'),
69 token => scalar $input->param('csrf_token'),
70 });
72 try {
73 $patron->set_password({ password => $newpassword });
74 $patron->userid($new_user_id)->store
75 if $new_user_id and $new_user_id ne $patron->userid;
76 $template->param( newpassword => $newpassword );
77 if ( $destination eq 'circ' ) {
78 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=" . $patron->cardnumber);
80 else {
81 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$patron_id");
84 catch {
85 if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
86 push @errors, 'ERROR_password_too_short';
88 elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
89 push @errors, 'ERROR_password_has_whitespaces';
91 elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
92 push @errors, 'ERROR_password_too_weak';
94 elsif ( $_->isa('Koha::Exceptions::Password::Plugin') ) {
95 push @errors, 'ERROR_from_plugin';
97 else {
98 push( @errors, 'BADUSERID' );
103 $template->param(
104 patron => $patron,
105 destination => $destination,
106 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
109 if ( scalar(@errors) ) {
110 $template->param( errormsg => 1 );
111 foreach my $error (@errors) {
112 $template->param($error) || $template->param( $error => 1 );
116 output_html_with_http_headers $input, $cookie, $template->output;