Bug 18298: minPaswordLength should not be < 3
[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 unless ( $patron ) {
53     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
54     exit;
55 }
56
57 my $category_type = $patron->category->category_type;
58 my $bor = $patron->unblessed;
59
60 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
61     push( @errors, 'NOPERMISSION' )
62       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
63
64     # need superlibrarian for koha-conf.xml fakeuser.
65 }
66
67 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
68
69 my $minpw = C4::Context->preference('minPasswordLength');
70 $minpw = 3 if not $minpw or $minpw < 3;
71 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
72
73 if ( $newpassword && !scalar(@errors) ) {
74
75     die "Wrong CSRF token"
76         unless Koha::Token->new->check_csrf({
77             session_id => scalar $input->cookie('CGISESSID'),
78             token  => scalar $input->param('csrf_token'),
79         });
80
81     my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
82     my $uid    = $input->param('newuserid') || $bor->{userid};
83     my $dbh    = C4::Context->dbh;
84     if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
85         $template->param( newpassword => $newpassword );
86         if ( $destination eq 'circ' ) {
87             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
88         }
89         else {
90             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
91         }
92     }
93     else {
94         push( @errors, 'BADUSERID' );
95     }
96 }
97
98 if ( $category_type eq 'C') {
99     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
100     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
101     $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
102 }
103
104 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
105
106 $template->param( picture => 1 ) if $patron->image;
107
108 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
109     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
110     $template->param(
111         ExtendedPatronAttributes => 1,
112         extendedattributes       => $attributes
113     );
114 }
115
116 $template->param(
117     othernames                 => $bor->{'othernames'},
118     surname                    => $bor->{'surname'},
119     firstname                  => $bor->{'firstname'},
120     borrowernumber             => $bor->{'borrowernumber'},
121     cardnumber                 => $bor->{'cardnumber'},
122     categorycode               => $bor->{'categorycode'},
123     category_type              => $category_type,
124     categoryname               => $bor->{'description'},
125     address                    => $bor->{address},
126     address2                   => $bor->{'address2'},
127     streettype                 => $bor->{streettype},
128     city                       => $bor->{'city'},
129     state                      => $bor->{'state'},
130     zipcode                    => $bor->{'zipcode'},
131     country                    => $bor->{'country'},
132     phone                      => $bor->{'phone'},
133     phonepro                   => $bor->{'phonepro'},
134     streetnumber               => $bor->{'streetnumber'},
135     mobile                     => $bor->{'mobile'},
136     email                      => $bor->{'email'},
137     emailpro                   => $bor->{'emailpro'},
138     branchcode                 => $bor->{'branchcode'},
139     userid                     => $bor->{'userid'},
140     destination                => $destination,
141     is_child                   => ( $category_type eq 'C' ),
142     minPasswordLength          => $minpw,
143     RoutingSerials             => C4::Context->preference('RoutingSerials'),
144     csrf_token                 => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
145 );
146
147 if ( scalar(@errors) ) {
148     $template->param( errormsg => 1 );
149     foreach my $error (@errors) {
150         $template->param($error) || $template->param( $error => 1 );
151     }
152 }
153
154 output_html_with_http_headers $input, $cookie, $template->output;