Bug 18672: Fix overwriting creation date with modification date when editing list
[koha.git] / members / member-password.pl
blob4cf00bfde47988b538d42b526731d14373863565
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::Token;
20 use Koha::Patrons;
21 use Koha::Patron::Categories;
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 $patron = Koha::Patrons->find( $member );
52 my $category_type = $patron->category->category_type;
53 my $bor = $patron->unblessed;
55 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
56 push( @errors, 'NOPERMISSION' )
57 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
59 # need superlibrarian for koha-conf.xml fakeuser.
62 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
64 my $minpw = C4::Context->preference('minPasswordLength');
65 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
67 if ( $newpassword && !scalar(@errors) ) {
69 die "Wrong CSRF token"
70 unless Koha::Token->new->check_csrf({
71 session_id => scalar $input->cookie('CGISESSID'),
72 token => scalar $input->param('csrf_token'),
73 });
75 my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
76 my $uid = $input->param('newuserid') || $bor->{userid};
77 my $dbh = C4::Context->dbh;
78 if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
79 $template->param( newpassword => $newpassword );
80 if ( $destination eq 'circ' ) {
81 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
83 else {
84 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
87 else {
88 push( @errors, 'BADUSERID' );
91 else {
92 my $userid = $bor->{'userid'};
94 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
95 my $length = int( rand(2) ) + C4::Context->preference("minPasswordLength");
96 my $defaultnewpassword = '';
97 for ( my $i = 0 ; $i < $length ; $i++ ) {
98 $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
101 $template->param( defaultnewpassword => $defaultnewpassword );
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 minPasswordLength => $minpw,
149 RoutingSerials => C4::Context->preference('RoutingSerials'),
150 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
153 if ( scalar(@errors) ) {
154 $template->param( errormsg => 1 );
155 foreach my $error (@errors) {
156 $template->param($error) || $template->param( $error => 1 );
160 output_html_with_http_headers $input, $cookie, $template->output;