Bug 22048: Use set_password opac/opac-password-recovery.pl
[koha.git] / opac / opac-password-recovery.pl
index 648c407..6429f7b 100755 (executable)
@@ -10,7 +10,6 @@ use C4::Context;
 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;
 use HTML::Entities;
@@ -74,16 +73,13 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
         $errMultipleAccountsForEmail = 1;
     }
     elsif ( $borrower = $search_results->next() ) {    # One matching borrower
-        my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
+        my @emails = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
 
-        my $firstNonEmptyEmail = '';
-        foreach my $address ( @emails ) {
-            $firstNonEmptyEmail = $address if length $address;
-            last if $firstNonEmptyEmail;
-        }
+        my $firstNonEmptyEmail;
+        $firstNonEmptyEmail = $emails[0] if @emails;
 
         # Is the given email one of the borrower's ?
-        if ( $email && !( grep { $_ eq $email } @emails ) ) {
+        if ( $email && !( grep /^$email$/i, @emails ) ) {
             $hasError    = 1;
             $errNoBorrowerFound = 1;
         }
@@ -104,6 +100,10 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
                 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
             }
         }
+        # Set the $email, if we don't have one.
+        if ( !$hasError && !$email ) {
+            $email = $firstNonEmptyEmail;
+        }
     }
     else {    # 0 matching borrower
         $hasError           = 1;
@@ -146,19 +146,26 @@ elsif ( $query->param('passwordReset') ) {
     } elsif ( $password ne $repeatPassword ) {
         $error = 'errPassNotMatch';
     } else {
-        my ( $is_valid, $err) = Koha::AuthUtils::is_password_valid( $password );
-        unless ( $is_valid ) {
-            $error = 'password_too_short' if $err eq 'too_short';
-            $error = 'password_too_weak' if $err eq 'too_weak';
-            $error = 'password_has_whitespaces' if $err eq 'has_whitespaces';
-        } else {
-            Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
+        try {
+            Koha::Patrons->find($borrower_number)->set_password({ password => $password });
+
             CompletePasswordRecovery($uniqueKey);
             $template->param(
                 password_reset_done => 1,
                 username            => $username
             );
         }
+        catch {
+            if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
+                $error = 'password_too_short';
+            }
+            elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
+                $error = 'password_has_whitespaces';
+            }
+            elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
+                $error = 'password_too_weak';
+            }
+        };
     }
     if ( $error ) {
         $template->param(