Bug 16419: follow-up of bug 11371 - Fix t/db_dependent/Acquisition.t
[koha.git] / members / member-password.pl
blob85e4b976ec03f8b025093bcda6fa026a6a164998
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::Branch;
16 use C4::Circulation;
17 use CGI qw ( -utf8 );
18 use C4::Members::Attributes qw(GetBorrowerAttributes);
19 use Koha::Patron::Images;
21 use Digest::MD5 qw(md5_base64);
23 my $input = new CGI;
25 my $theme = $input->param('theme') || "default";
27 # only used if allowthemeoverride is set
29 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
31 template_name => "members/member-password.tt",
32 query => $input,
33 type => "intranet",
34 authnotrequired => 0,
35 flagsrequired => { borrowers => 1 },
36 debug => 1,
40 my $flagsrequired;
41 $flagsrequired->{borrowers} = 1;
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');
49 my @errors;
51 my ($bor) = GetMember( 'borrowernumber' => $member );
53 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
54 push( @errors, 'NOPERMISSION' )
55 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
57 # need superlibrarian for koha-conf.xml fakeuser.
60 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
62 my $minpw = C4::Context->preference('minPasswordLength');
63 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
65 if ( $newpassword && !scalar(@errors) ) {
66 my $digest = Koha::AuthUtils::hash_password( $input->param('newpassword') );
67 my $uid = $input->param('newuserid');
68 my $dbh = C4::Context->dbh;
69 if ( changepassword( $uid, $member, $digest ) ) {
70 $template->param( newpassword => $newpassword );
71 if ( $destination eq 'circ' ) {
72 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
74 else {
75 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
78 else {
79 push( @errors, 'BADUSERID' );
82 else {
83 my $userid = $bor->{'userid'};
85 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
86 my $length = int( rand(2) ) + C4::Context->preference("minPasswordLength");
87 my $defaultnewpassword = '';
88 for ( my $i = 0 ; $i < $length ; $i++ ) {
89 $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
92 $template->param( defaultnewpassword => $defaultnewpassword );
95 if ( $bor->{'category_type'} eq 'C' ) {
96 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
97 my $cnt = scalar(@$catcodes);
98 $template->param( 'CATCODE_MULTI' => 1 ) if $cnt > 1;
99 $template->param( 'catcode' => $catcodes->[0] ) if $cnt == 1;
102 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
104 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
105 $template->param( picture => 1 ) if $patron_image;
107 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
108 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
109 $template->param(
110 ExtendedPatronAttributes => 1,
111 extendedattributes => $attributes
115 $template->param(
116 othernames => $bor->{'othernames'},
117 surname => $bor->{'surname'},
118 firstname => $bor->{'firstname'},
119 borrowernumber => $bor->{'borrowernumber'},
120 cardnumber => $bor->{'cardnumber'},
121 categorycode => $bor->{'categorycode'},
122 category_type => $bor->{'category_type'},
123 categoryname => $bor->{'description'},
124 address => $bor->{address},
125 address2 => $bor->{'address2'},
126 streettype => $bor->{streettype},
127 city => $bor->{'city'},
128 state => $bor->{'state'},
129 zipcode => $bor->{'zipcode'},
130 country => $bor->{'country'},
131 phone => $bor->{'phone'},
132 phonepro => $bor->{'phonepro'},
133 mobile => $bor->{'mobile'},
134 email => $bor->{'email'},
135 emailpro => $bor->{'emailpro'},
136 branchcode => $bor->{'branchcode'},
137 branchname => GetBranchName( $bor->{'branchcode'} ),
138 userid => $bor->{'userid'},
139 destination => $destination,
140 is_child => ( $bor->{'category_type'} eq 'C' ),
141 activeBorrowerRelationship => ( C4::Context->preference('borrowerRelationship') ne '' ),
142 minPasswordLength => $minpw,
143 RoutingSerials => C4::Context->preference('RoutingSerials'),
146 if ( scalar(@errors) ) {
147 $template->param( errormsg => 1 );
148 foreach my $error (@errors) {
149 $template->param($error) || $template->param( $error => 1 );
153 output_html_with_http_headers $input, $cookie, $template->output;