Bug 12528 - Enable staff to deny message setting access to patrons on the OPAC
[koha.git] / opac / opac-password-recovery.pl
index 1cef3ed..2f7fbfe 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-use strict;
 use Modern::Perl;
 use CGI;
 
@@ -9,10 +8,10 @@ use C4::Koha;
 use C4::Members qw(changepassword);
 use C4::Output;
 use C4::Context;
-use C4::Passwordrecovery
+use Koha::Patron::Password::Recovery
   qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
 use Koha::AuthUtils qw(hash_password);
-use Koha::Borrowers;
+use Koha::Patrons;
 my $query = new CGI;
 use HTML::Entities;
 
@@ -59,34 +58,33 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
 
     # Find the borrower by his userid or email
     if ($username) {
-        $search_results = [ Koha::Borrowers->search( { userid => $username } ) ];
+        $search_results = [ Koha::Patrons->search( { userid => $username } ) ];
     }
     elsif ($email) {
-        $search_results = [ Koha::Borrowers->search( { -or => { email => $email, emailpro => $email, B_email  => $email } } ) ];
+        $search_results = [ Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email  => $email } } ) ];
     }
-    if ( not $search_results ) {
+    if ( not $search_results || scalar @$search_results > 1 ) {
         $hasError           = 1;
         $errNoBorrowerFound = 1;
     }
-    elsif ( scalar @$search_results > 1 ) {    # Many matching borrowers
-        $hasError             = 1;
-        $errTooManyEmailFound = 1;
-    }
     elsif ( $borrower = shift @$search_results ) {    # One matching borrower
         $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;
-            $errBadEmail = 1;
+            $errNoBorrowerFound = 1;
         }
 
 # 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;
             $errNoBorrowerEmail = 1;
         }