Bug 19369: Unit tests
[koha.git] / members / member-password.pl
blobc458ae8f1f1337f2621398ab330e4ab029989ec2
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
7 use strict;
8 use warnings;
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::AuthUtils;
19 use Koha::Token;
21 use Koha::Patrons;
22 use Koha::Patron::Categories;
24 my $input = new CGI;
26 my $theme = $input->param('theme') || "default";
28 # only used if allowthemeoverride is set
30 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
32 template_name => "members/member-password.tt",
33 query => $input,
34 type => "intranet",
35 authnotrequired => 0,
36 flagsrequired => { borrowers => 1 },
37 debug => 1,
41 my $flagsrequired;
42 $flagsrequired->{borrowers} = 1;
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');
50 my @errors;
52 my $patron = Koha::Patrons->find( $member );
53 unless ( $patron ) {
54 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
55 exit;
58 my $category_type = $patron->category->category_type;
59 my $bor = $patron->unblessed;
61 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
62 push( @errors, 'NOPERMISSION' )
63 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
65 # need superlibrarian for koha-conf.xml fakeuser.
68 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
70 if ( $newpassword and not @errors ) {
71 my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $newpassword );
72 unless ( $is_valid ) {
73 push @errors, 'ERROR_password_too_short' if $error eq 'too_short';
74 push @errors, 'ERROR_password_too_weak' if $error eq 'too_weak';
75 push @errors, 'ERROR_password_has_whitespaces' if $error eq 'has_whitespaces';
79 if ( $newpassword and not @errors) {
81 die "Wrong CSRF token"
82 unless Koha::Token->new->check_csrf({
83 session_id => scalar $input->cookie('CGISESSID'),
84 token => scalar $input->param('csrf_token'),
85 });
87 my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
88 my $uid = $input->param('newuserid') || $bor->{userid};
89 my $dbh = C4::Context->dbh;
90 if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
91 $template->param( newpassword => $newpassword );
92 if ( $destination eq 'circ' ) {
93 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
95 else {
96 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
99 else {
100 push( @errors, 'BADUSERID' );
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;
110 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
112 $template->param( picture => 1 ) if $patron->image;
114 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
115 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
116 $template->param(
117 ExtendedPatronAttributes => 1,
118 extendedattributes => $attributes
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 RoutingSerials => C4::Context->preference('RoutingSerials'),
149 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
152 if ( scalar(@errors) ) {
153 $template->param( errormsg => 1 );
154 foreach my $error (@errors) {
155 $template->param($error) || $template->param( $error => 1 );
159 output_html_with_http_headers $input, $cookie, $template->output;