Bug 17843: Replace C4::Koha::getitemtypeinfo with Koha::ItemTypes
[koha.git] / opac / opac-password-recovery.pl
index aa98040..8c76015 100755 (executable)
@@ -5,11 +5,11 @@ use CGI;
 
 use C4::Auth;
 use C4::Koha;
-use C4::Members qw(changepassword);
 use C4::Output;
 use C4::Context;
-use C4::Passwordrecovery
-  qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
+use Koha::Patron::Password::Recovery
+  qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery DeleteExpiredPasswordRecovery);
+use Koha::Patrons;
 use Koha::AuthUtils qw(hash_password);
 use Koha::Patrons;
 my $query = new CGI;
@@ -54,7 +54,7 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
     #try with the main email
     $email ||= '';    # avoid undef
     my $borrower;
-    my $search_results;
+    my $search_results = [];
 
     # Find the borrower by his userid or email
     if ($username) {
@@ -71,6 +71,12 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
         $username ||= $borrower->userid;
         my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
 
+        my $firstNonEmptyEmail = '';
+        foreach my $address ( @emails ) {
+            $firstNonEmptyEmail = $address if length $address;
+            last if $firstNonEmptyEmail;
+        }
+
         # Is the given email one of the borrower's ?
         if ( $email && !( grep { $_ eq $email } @emails ) ) {
             $hasError    = 1;
@@ -78,20 +84,20 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
         }
 
 # If we dont have an email yet. Get one of the borrower's email or raise an error.
-# FIXME: That ugly shift-grep contraption.
-# $email = shift [ grep { length() } @emails ]
-# It's supposed to get a non-empty string from the @emails array. There's surely a simpler way
-        elsif ( !$email && !( $email = shift [ grep { length() } @emails ] ) ) {
+        elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
             $hasError           = 1;
-            $errNoBorrowerFound = 1;
+            $errNoBorrowerEmail = 1;
         }
 
 # Check if a password reset already issued for this borrower AND we are not asking for a new email
-        elsif ( ValidateBorrowernumber( $borrower->borrowernumber )
-            && !$query->param('resendEmail') )
-        {
-            $hasError                = 1;
-            $errAlreadyStartRecovery = 1;
+        elsif ( not $query->param('resendEmail') ) {
+            if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
+                $hasError                = 1;
+                $errAlreadyStartRecovery = 1;
+            }
+            else {
+                DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
+            }
         }
     }
     else {    # 0 matching borrower
@@ -132,7 +138,7 @@ elsif ( $query->param('passwordReset') ) {
         && ( $password eq $repeatPassword )
         && ( length($password) >= $minPassLength ) )
     {    #apply changes
-        changepassword( $username, $borrower_number, hash_password($password) );
+        Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
         CompletePasswordRecovery($uniqueKey);
         $template->param(
             password_reset_done => 1,