Bug 9374: (follow-up) add FIXME suggesting that use of a regexp is not the long-term...
[koha.git] / members / member-password.pl
index 696a436..ac25da5 100755 (executable)
@@ -5,13 +5,17 @@
 #converted to using templates 3/16/03 by mwhansen@hmc.edu
 
 use strict;
+use warnings;
+
 use C4::Auth;
+use Koha::AuthUtils;
 use C4::Output;
 use C4::Context;
 use C4::Members;
 use C4::Branch;
 use C4::Circulation;
 use CGI;
+use C4::Members::Attributes qw(GetBorrowerAttributes);
 
 use Digest::MD5 qw(md5_base64);
 
@@ -32,23 +36,27 @@ my ($template, $loggedinuser, $cookie, $staffflags)
 my $flagsrequired;
 $flagsrequired->{borrowers}=1;
 
-#my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired);
+#my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired, 'intranet');
 
 my $member=$input->param('member');
 my $cardnumber = $input->param('cardnumber');
 my $destination = $input->param('destination');
-my $errormsg;
-my ($bor)=GetMember($member);
+my @errors;
+my ($bor)=GetMember('borrowernumber' => $member);
 if(( $member ne $loggedinuser ) && ($bor->{'category_type'} eq 'S' ) ) {
-       $errormsg = 'NOPERMISSION' unless($staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
+       push(@errors,'NOPERMISSION') unless($staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
        # need superlibrarian for koha-conf.xml fakeuser.
 }
 my $newpassword = $input->param('newpassword');
+my $newpassword2 = $input->param('newpassword2');
+
+push(@errors,'NOMATCH') if ( ( $newpassword && $newpassword2 ) && ($newpassword ne $newpassword2) );
+
 my $minpw = C4::Context->preference('minPasswordLength');
-$errormsg = 'SHORTPASSWORD' if( $newpassword && $minpw && (length($newpassword) < $minpw ) );
+push(@errors,'SHORTPASSWORD') if( $newpassword && $minpw && (length($newpassword) < $minpw ) );
 
-if ( $newpassword  && ! $errormsg ) {
-    my $digest=md5_base64($input->param('newpassword'));
+if ( $newpassword  && !scalar(@errors) ) {
+    my $digest=Koha::AuthUtils::hash_password($input->param('newpassword'));
     my $uid = $input->param('newuserid');
     my $dbh=C4::Context->dbh;
     if (changepassword($uid,$member,$digest)) {
@@ -59,24 +67,20 @@ if ( $newpassword  && ! $errormsg ) {
                    print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
                }
     } else {
-                       $errormsg = 'BADUSERID';
-           $template->param(othernames => $bor->{'othernames'},
-                                               surname     => $bor->{'surname'},
-                                               firstname   => $bor->{'firstname'},
-                                               userid      => $bor->{'userid'},
-                                               defaultnewpassword => $newpassword 
-                                               );
+                       push(@errors,'BADUSERID');
     }
 } else {
     my $userid = $bor->{'userid'};
 
     my $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
-    my $length=int(rand(2))+4;
+    my $length=int(rand(2))+C4::Context->preference("minPasswordLength");
     my $defaultnewpassword='';
     for (my $i=0; $i<$length; $i++) {
        $defaultnewpassword.=substr($chars, int(rand(length($chars))),1);
     }
-       
+
+       $template->param( defaultnewpassword => $defaultnewpassword );
+}
     if ( $bor->{'category_type'} eq 'C') {
         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
         my $cnt = scalar(@$catcodes);
@@ -85,9 +89,17 @@ if ( $newpassword  && ! $errormsg ) {
     }
        
 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
-my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
+my ($picture, $dberror) = GetPatronImage($bor->{'borrowernumber'});
 $template->param( picture => 1 ) if $picture;
-       
+
+if (C4::Context->preference('ExtendedPatronAttributes')) {
+    my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
+    $template->param(
+        ExtendedPatronAttributes => 1,
+        extendedattributes => $attributes
+    );
+}
+
     $template->param( othernames => $bor->{'othernames'},
            surname     => $bor->{'surname'},
            firstname   => $bor->{'firstname'},
@@ -99,6 +111,7 @@ $template->param( picture => 1 ) if $picture;
            address => $bor->{'address'},
            address2 => $bor->{'address2'},
            city => $bor->{'city'},
+           state => $bor->{'state'},
            zipcode => $bor->{'zipcode'},
            country => $bor->{'country'},
            phone => $bor->{'phone'},
@@ -108,15 +121,17 @@ $template->param( picture => 1 ) if $picture;
            userid      => $bor->{'userid'},
            destination => $destination,
                is_child        => ($bor->{'category_type'} eq 'C'),
-           defaultnewpassword => $defaultnewpassword 
+               activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
+        minPasswordLength => $minpw,
+        RoutingSerials => C4::Context->preference('RoutingSerials'),
        );
 
+if( scalar(@errors )){
+       $template->param( errormsg => 1 );
+       foreach my $error (@errors) {
+        $template->param($error) || $template->param( $error => 1);
+       }
 
 }
 
-$template->param( member => $member,
-                                       errormsg => $errormsg,
-                                       $errormsg => 1 ,
-                                       minPasswordLength => $minpw );
-
 output_html_with_http_headers $input, $cookie, $template->output;