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