DBRev 17.05.00.000
[koha.git] / members / member-password.pl
blob4fdf6994277fbddace8333d3ceffe2d3833c5b66
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::Patron::Images;
19 use Koha::Token;
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 ($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) ) {
67 die "Wrong CSRF token"
68 unless Koha::Token->new->check_csrf({
69 session_id => scalar $input->cookie('CGISESSID'),
70 token => scalar $input->param('csrf_token'),
71 });
73 my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
74 my $uid = $input->param('newuserid') || $bor->{userid};
75 my $dbh = C4::Context->dbh;
76 if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
77 $template->param( newpassword => $newpassword );
78 if ( $destination eq 'circ' ) {
79 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
81 else {
82 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
85 else {
86 push( @errors, 'BADUSERID' );
89 else {
90 my $userid = $bor->{'userid'};
92 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
93 my $length = int( rand(2) ) + C4::Context->preference("minPasswordLength");
94 my $defaultnewpassword = '';
95 for ( my $i = 0 ; $i < $length ; $i++ ) {
96 $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
99 $template->param( defaultnewpassword => $defaultnewpassword );
102 if ( $bor->{'category_type'} eq 'C') {
103 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
104 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
105 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
108 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' || $bor->{'category_type'} eq 'I' );
110 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
111 $template->param( picture => 1 ) if $patron_image;
113 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
114 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
115 $template->param(
116 ExtendedPatronAttributes => 1,
117 extendedattributes => $attributes
121 $template->param(
122 othernames => $bor->{'othernames'},
123 surname => $bor->{'surname'},
124 firstname => $bor->{'firstname'},
125 borrowernumber => $bor->{'borrowernumber'},
126 cardnumber => $bor->{'cardnumber'},
127 categorycode => $bor->{'categorycode'},
128 category_type => $bor->{'category_type'},
129 categoryname => $bor->{'description'},
130 address => $bor->{address},
131 address2 => $bor->{'address2'},
132 streettype => $bor->{streettype},
133 city => $bor->{'city'},
134 state => $bor->{'state'},
135 zipcode => $bor->{'zipcode'},
136 country => $bor->{'country'},
137 phone => $bor->{'phone'},
138 phonepro => $bor->{'phonepro'},
139 streetnumber => $bor->{'streetnumber'},
140 mobile => $bor->{'mobile'},
141 email => $bor->{'email'},
142 emailpro => $bor->{'emailpro'},
143 branchcode => $bor->{'branchcode'},
144 userid => $bor->{'userid'},
145 destination => $destination,
146 is_child => ( $bor->{'category_type'} eq 'C' ),
147 minPasswordLength => $minpw,
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;