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