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