Add release notes to 16.05.14 release
[koha.git] / members / member-password.pl
blobab49ca9c76b7fdcd022a5b1a1e7ed829cf74c58b
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;
20 use Koha::Token;
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 ($bor) = GetMember( 'borrowernumber' => $member );
54 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
55 push( @errors, 'NOPERMISSION' )
56 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
58 # need superlibrarian for koha-conf.xml fakeuser.
61 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
63 my $minpw = C4::Context->preference('minPasswordLength');
64 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
66 if ( $newpassword && !scalar(@errors) ) {
68 die "Wrong CSRF token"
69 unless Koha::Token->new->check_csrf({
70 session_id => scalar $input->cookie('CGISESSID'),
71 token => scalar $input->param('csrf_token'),
72 });
74 my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
75 my $uid = $input->param('newuserid') || $bor->{userid};
76 my $dbh = C4::Context->dbh;
77 if ( changepassword( $uid, $member, $digest ) ) {
78 $template->param( newpassword => $newpassword );
79 if ( $destination eq 'circ' ) {
80 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
82 else {
83 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
86 else {
87 push( @errors, 'BADUSERID' );
90 else {
91 my $userid = $bor->{'userid'};
93 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
94 my $length = int( rand(2) ) + C4::Context->preference("minPasswordLength");
95 my $defaultnewpassword = '';
96 for ( my $i = 0 ; $i < $length ; $i++ ) {
97 $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
100 $template->param( defaultnewpassword => $defaultnewpassword );
103 if ( $bor->{'category_type'} eq 'C' ) {
104 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
105 my $cnt = scalar(@$catcodes);
106 $template->param( 'CATCODE_MULTI' => 1 ) if $cnt > 1;
107 $template->param( 'catcode' => $catcodes->[0] ) if $cnt == 1;
110 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' || $bor->{'category_type'} eq 'I' );
112 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
113 $template->param( picture => 1 ) if $patron_image;
115 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
116 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
117 $template->param(
118 ExtendedPatronAttributes => 1,
119 extendedattributes => $attributes
123 $template->param(
124 othernames => $bor->{'othernames'},
125 surname => $bor->{'surname'},
126 firstname => $bor->{'firstname'},
127 borrowernumber => $bor->{'borrowernumber'},
128 cardnumber => $bor->{'cardnumber'},
129 categorycode => $bor->{'categorycode'},
130 category_type => $bor->{'category_type'},
131 categoryname => $bor->{'description'},
132 address => $bor->{address},
133 address2 => $bor->{'address2'},
134 streettype => $bor->{streettype},
135 city => $bor->{'city'},
136 state => $bor->{'state'},
137 zipcode => $bor->{'zipcode'},
138 country => $bor->{'country'},
139 phone => $bor->{'phone'},
140 phonepro => $bor->{'phonepro'},
141 mobile => $bor->{'mobile'},
142 email => $bor->{'email'},
143 emailpro => $bor->{'emailpro'},
144 branchcode => $bor->{'branchcode'},
145 branchname => GetBranchName( $bor->{'branchcode'} ),
146 userid => $bor->{'userid'},
147 destination => $destination,
148 is_child => ( $bor->{'category_type'} eq 'C' ),
149 minPasswordLength => $minpw,
150 RoutingSerials => C4::Context->preference('RoutingSerials'),
151 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
154 if ( scalar(@errors) ) {
155 $template->param( errormsg => 1 );
156 foreach my $error (@errors) {
157 $template->param($error) || $template->param( $error => 1 );
161 output_html_with_http_headers $input, $cookie, $template->output;