Bug 18423 - Add 'Add child' button to files, housebound, and deletemem pages
[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::Branch;
16 use C4::Circulation;
17 use CGI qw ( -utf8 );
18 use C4::Members::Attributes qw(GetBorrowerAttributes);
19 use Koha::Patron::Images;
20 use Koha::Token;
21
22 use Koha::Patron::Categories;
23
24 my $input = new CGI;
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         authnotrequired => 0,
36         flagsrequired   => { borrowers => 1 },
37         debug           => 1,
38     }
39 );
40
41 my $flagsrequired;
42 $flagsrequired->{borrowers} = 1;
43
44 my $member      = $input->param('member');
45 my $cardnumber  = $input->param('cardnumber');
46 my $destination = $input->param('destination');
47 my $newpassword  = $input->param('newpassword');
48 my $newpassword2 = $input->param('newpassword2');
49
50 my @errors;
51
52 my ($bor) = GetMember( 'borrowernumber' => $member );
53
54 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
55     push( @errors, 'NOPERMISSION' )
56       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
57
58     # need superlibrarian for koha-conf.xml fakeuser.
59 }
60
61 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
62
63 my $minpw = C4::Context->preference('minPasswordLength');
64 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
65
66 if ( $newpassword && !scalar(@errors) ) {
67
68     die "Wrong CSRF token"
69         unless Koha::Token->new->check_csrf({
70             session_id => scalar $input->cookie('CGISESSID'),
71             token  => scalar $input->param('csrf_token'),
72         });
73
74     my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
75     my $uid    = $input->param('newuserid') || $bor->{userid};
76     my $dbh    = C4::Context->dbh;
77     if ( changepassword( $uid, $member, $digest ) ) {
78         $template->param( newpassword => $newpassword );
79         if ( $destination eq 'circ' ) {
80             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
81         }
82         else {
83             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
84         }
85     }
86     else {
87         push( @errors, 'BADUSERID' );
88     }
89 }
90 else {
91     my $userid = $bor->{'userid'};
92
93     my $chars              = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
94     my $length             = int( rand(2) ) + C4::Context->preference("minPasswordLength");
95     my $defaultnewpassword = '';
96     for ( my $i = 0 ; $i < $length ; $i++ ) {
97         $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
98     }
99
100     $template->param( defaultnewpassword => $defaultnewpassword );
101 }
102
103 if ( $bor->{'category_type'} eq 'C' ) {
104     my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
105     my $cnt = scalar(@$catcodes);
106     $template->param( 'CATCODE_MULTI' => 1 ) if $cnt > 1;
107     $template->param( 'catcode' => $catcodes->[0] ) if $cnt == 1;
108 }
109
110 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
111
112 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
113 $template->param( picture => 1 ) if $patron_image;
114
115 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
116     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
117     $template->param(
118         ExtendedPatronAttributes => 1,
119         extendedattributes       => $attributes
120     );
121 }
122
123 $template->param(
124     othernames                 => $bor->{'othernames'},
125     surname                    => $bor->{'surname'},
126     firstname                  => $bor->{'firstname'},
127     borrowernumber             => $bor->{'borrowernumber'},
128     cardnumber                 => $bor->{'cardnumber'},
129     categorycode               => $bor->{'categorycode'},
130     category_type              => $bor->{'category_type'},
131     categoryname               => $bor->{'description'},
132     address                    => $bor->{address},
133     address2                   => $bor->{'address2'},
134     streettype                 => $bor->{streettype},
135     city                       => $bor->{'city'},
136     state                      => $bor->{'state'},
137     zipcode                    => $bor->{'zipcode'},
138     country                    => $bor->{'country'},
139     phone                      => $bor->{'phone'},
140     phonepro                   => $bor->{'phonepro'},
141     mobile                     => $bor->{'mobile'},
142     email                      => $bor->{'email'},
143     emailpro                   => $bor->{'emailpro'},
144     branchcode                 => $bor->{'branchcode'},
145     branchname                 => GetBranchName( $bor->{'branchcode'} ),
146     userid                     => $bor->{'userid'},
147     destination                => $destination,
148     is_child                   => ( $bor->{'category_type'} eq 'C' ),
149     minPasswordLength          => $minpw,
150     RoutingSerials             => C4::Context->preference('RoutingSerials'),
151     csrf_token                 => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
152 );
153
154 if ( scalar(@errors) ) {
155     $template->param( errormsg => 1 );
156     foreach my $error (@errors) {
157         $template->param($error) || $template->param( $error => 1 );
158     }
159 }
160
161 output_html_with_http_headers $input, $cookie, $template->output;