Bug 15542 [QA Followup] - Tidy member-password.pl
[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
20 use Digest::MD5 qw(md5_base64);
21
22 my $input = new CGI;
23
24 my $theme = $input->param('theme') || "default";
25
26 # only used if allowthemeoverride is set
27
28 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
29     {
30         template_name   => "members/member-password.tt",
31         query           => $input,
32         type            => "intranet",
33         authnotrequired => 0,
34         flagsrequired   => { borrowers => 1 },
35         debug           => 1,
36     }
37 );
38
39 my $flagsrequired;
40 $flagsrequired->{borrowers} = 1;
41
42 my $member      = $input->param('member');
43 my $cardnumber  = $input->param('cardnumber');
44 my $destination = $input->param('destination');
45 my $newpassword  = $input->param('newpassword');
46 my $newpassword2 = $input->param('newpassword2');
47
48 my @errors;
49
50 my ($bor) = GetMember( 'borrowernumber' => $member );
51
52 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
53     push( @errors, 'NOPERMISSION' )
54       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
55
56     # need superlibrarian for koha-conf.xml fakeuser.
57 }
58
59 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
60
61 my $minpw = C4::Context->preference('minPasswordLength');
62 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
63
64 if ( $newpassword && !scalar(@errors) ) {
65     my $digest = Koha::AuthUtils::hash_password( $input->param('newpassword') );
66     my $uid    = $input->param('newuserid');
67     my $dbh    = C4::Context->dbh;
68     if ( changepassword( $uid, $member, $digest ) ) {
69         $template->param( newpassword => $newpassword );
70         if ( $destination eq 'circ' ) {
71             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
72         }
73         else {
74             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
75         }
76     }
77     else {
78         push( @errors, 'BADUSERID' );
79     }
80 }
81 else {
82     my $userid = $bor->{'userid'};
83
84     my $chars              = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
85     my $length             = int( rand(2) ) + C4::Context->preference("minPasswordLength");
86     my $defaultnewpassword = '';
87     for ( my $i = 0 ; $i < $length ; $i++ ) {
88         $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
89     }
90
91     $template->param( defaultnewpassword => $defaultnewpassword );
92 }
93
94 if ( $bor->{'category_type'} eq 'C' ) {
95     my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
96     my $cnt = scalar(@$catcodes);
97     $template->param( 'CATCODE_MULTI' => 1 ) if $cnt > 1;
98     $template->param( 'catcode' => $catcodes->[0] ) if $cnt == 1;
99 }
100
101 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
102
103 my ( $picture, $dberror ) = GetPatronImage( $bor->{'borrowernumber'} );
104 $template->param( picture => 1 ) if $picture;
105
106 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
107     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
108     $template->param(
109         ExtendedPatronAttributes => 1,
110         extendedattributes       => $attributes
111     );
112 }
113
114 $template->param(
115     othernames                 => $bor->{'othernames'},
116     surname                    => $bor->{'surname'},
117     firstname                  => $bor->{'firstname'},
118     borrowernumber             => $bor->{'borrowernumber'},
119     cardnumber                 => $bor->{'cardnumber'},
120     categorycode               => $bor->{'categorycode'},
121     category_type              => $bor->{'category_type'},
122     categoryname               => $bor->{'description'},
123     address                    => $bor->{address},
124     address2                   => $bor->{'address2'},
125     streettype                 => $bor->{streettype},
126     city                       => $bor->{'city'},
127     state                      => $bor->{'state'},
128     zipcode                    => $bor->{'zipcode'},
129     country                    => $bor->{'country'},
130     phone                      => $bor->{'phone'},
131     phonepro                   => $bor->{'phonepro'},
132     mobile                     => $bor->{'mobile'},
133     email                      => $bor->{'email'},
134     emailpro                   => $bor->{'emailpro'},
135     branchcode                 => $bor->{'branchcode'},
136     branchname                 => GetBranchName( $bor->{'branchcode'} ),
137     userid                     => $bor->{'userid'},
138     destination                => $destination,
139     is_child                   => ( $bor->{'category_type'} eq 'C' ),
140     activeBorrowerRelationship => ( C4::Context->preference('borrowerRelationship') ne '' ),
141     minPasswordLength          => $minpw,
142     RoutingSerials             => C4::Context->preference('RoutingSerials'),
143 );
144
145 if ( scalar(@errors) ) {
146     $template->param( errormsg => 1 );
147     foreach my $error (@errors) {
148         $template->param($error) || $template->param( $error => 1 );
149     }
150 }
151
152 output_html_with_http_headers $input, $cookie, $template->output;