Bug 15395: Do not use nl_putenv, use $ENV instead
[koha.git] / members / member-password.pl
blobe886c41976ed35669acf9a119abe0615252d224d
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 Modern::Perl;
9 use C4::Auth;
10 use Koha::AuthUtils;
11 use C4::Output;
12 use C4::Context;
13 use C4::Members;
14 use C4::Circulation;
15 use CGI qw ( -utf8 );
16 use C4::Members::Attributes qw(GetBorrowerAttributes);
17 use Koha::AuthUtils;
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 => 'edit_borrowers' },
36 debug => 1,
40 my $member = $input->param('member');
41 my $cardnumber = $input->param('cardnumber');
42 my $destination = $input->param('destination');
43 my $newpassword = $input->param('newpassword');
44 my $newpassword2 = $input->param('newpassword2');
46 my @errors;
48 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
49 my $patron = Koha::Patrons->find( $member );
50 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
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 if ( $newpassword and not @errors ) {
65 my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $newpassword );
66 unless ( $is_valid ) {
67 push @errors, 'ERROR_password_too_short' if $error eq 'too_short';
68 push @errors, 'ERROR_password_too_weak' if $error eq 'too_weak';
69 push @errors, 'ERROR_password_has_whitespaces' if $error eq 'has_whitespaces';
73 if ( $newpassword and not @errors) {
75 die "Wrong CSRF token"
76 unless Koha::Token->new->check_csrf({
77 session_id => scalar $input->cookie('CGISESSID'),
78 token => scalar $input->param('csrf_token'),
79 });
81 my $uid = $input->param('newuserid') || $bor->{userid};
82 my $password = $input->param('newpassword');
83 my $dbh = C4::Context->dbh;
84 if ( Koha::Patrons->find( $member )->update_password($uid, $password) ) {
85 $template->param( newpassword => $newpassword );
86 if ( $destination eq 'circ' ) {
87 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
89 else {
90 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
93 else {
94 push( @errors, 'BADUSERID' );
98 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
99 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
100 $template->param(
101 ExtendedPatronAttributes => 1,
102 extendedattributes => $attributes
106 $template->param(
107 patron => $patron,
108 destination => $destination,
109 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
112 if ( scalar(@errors) ) {
113 $template->param( errormsg => 1 );
114 foreach my $error (@errors) {
115 $template->param($error) || $template->param( $error => 1 );
119 output_html_with_http_headers $input, $cookie, $template->output;