Bug 17829: Move GetMember to Koha::Patron
[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 strict;
8 use warnings;
9
10 use C4::Auth;
11 use Koha::AuthUtils;
12 use C4::Output;
13 use C4::Context;
14 use C4::Members;
15 use C4::Circulation;
16 use CGI qw ( -utf8 );
17 use C4::Members::Attributes qw(GetBorrowerAttributes);
18 use Koha::Token;
19
20 use Koha::Patrons;
21 use Koha::Patron::Categories;
22
23 my $input = new CGI;
24
25 my $theme = $input->param('theme') || "default";
26
27 # only used if allowthemeoverride is set
28
29 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
30     {
31         template_name   => "members/member-password.tt",
32         query           => $input,
33         type            => "intranet",
34         authnotrequired => 0,
35         flagsrequired   => { borrowers => 1 },
36         debug           => 1,
37     }
38 );
39
40 my $flagsrequired;
41 $flagsrequired->{borrowers} = 1;
42
43 my $member      = $input->param('member');
44 my $cardnumber  = $input->param('cardnumber');
45 my $destination = $input->param('destination');
46 my $newpassword  = $input->param('newpassword');
47 my $newpassword2 = $input->param('newpassword2');
48
49 my @errors;
50
51 my $patron = Koha::Patrons->find( $member );
52 my $category_type = $patron->category->category_type;
53 my $bor = $patron->unblessed;
54
55 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
56     push( @errors, 'NOPERMISSION' )
57       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
58
59     # need superlibrarian for koha-conf.xml fakeuser.
60 }
61
62 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
63
64 my $minpw = C4::Context->preference('minPasswordLength');
65 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
66
67 if ( $newpassword && !scalar(@errors) ) {
68
69     die "Wrong CSRF token"
70         unless Koha::Token->new->check_csrf({
71             session_id => scalar $input->cookie('CGISESSID'),
72             token  => scalar $input->param('csrf_token'),
73         });
74
75     my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
76     my $uid    = $input->param('newuserid') || $bor->{userid};
77     my $dbh    = C4::Context->dbh;
78     if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
79         $template->param( newpassword => $newpassword );
80         if ( $destination eq 'circ' ) {
81             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
82         }
83         else {
84             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
85         }
86     }
87     else {
88         push( @errors, 'BADUSERID' );
89     }
90 }
91 else {
92     my $userid = $bor->{'userid'};
93
94     my $chars              = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
95     my $length             = int( rand(2) ) + C4::Context->preference("minPasswordLength");
96     my $defaultnewpassword = '';
97     for ( my $i = 0 ; $i < $length ; $i++ ) {
98         $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
99     }
100
101     $template->param( defaultnewpassword => $defaultnewpassword );
102 }
103
104 if ( $category_type eq 'C') {
105     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
106     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
107     $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
108 }
109
110 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
111
112 $template->param( picture => 1 ) if $patron->image;
113
114 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
115     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
116     $template->param(
117         ExtendedPatronAttributes => 1,
118         extendedattributes       => $attributes
119     );
120 }
121
122 $template->param(
123     othernames                 => $bor->{'othernames'},
124     surname                    => $bor->{'surname'},
125     firstname                  => $bor->{'firstname'},
126     borrowernumber             => $bor->{'borrowernumber'},
127     cardnumber                 => $bor->{'cardnumber'},
128     categorycode               => $bor->{'categorycode'},
129     category_type              => $category_type,
130     categoryname               => $bor->{'description'},
131     address                    => $bor->{address},
132     address2                   => $bor->{'address2'},
133     streettype                 => $bor->{streettype},
134     city                       => $bor->{'city'},
135     state                      => $bor->{'state'},
136     zipcode                    => $bor->{'zipcode'},
137     country                    => $bor->{'country'},
138     phone                      => $bor->{'phone'},
139     phonepro                   => $bor->{'phonepro'},
140     streetnumber               => $bor->{'streetnumber'},
141     mobile                     => $bor->{'mobile'},
142     email                      => $bor->{'email'},
143     emailpro                   => $bor->{'emailpro'},
144     branchcode                 => $bor->{'branchcode'},
145     userid                     => $bor->{'userid'},
146     destination                => $destination,
147     is_child                   => ( $category_type eq 'C' ),
148     minPasswordLength          => $minpw,
149     RoutingSerials             => C4::Context->preference('RoutingSerials'),
150     csrf_token                 => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
151 );
152
153 if ( scalar(@errors) ) {
154     $template->param( errormsg => 1 );
155     foreach my $error (@errors) {
156         $template->param($error) || $template->param( $error => 1 );
157     }
158 }
159
160 output_html_with_http_headers $input, $cookie, $template->output;