From c1ab903b5e735746d3c4454ecc323e55cca2361f Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Fri, 8 Jul 2022 08:53:22 +0200
Subject: [PATCH] Bug 28787: Don't request a token if no email address defined

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Sponsored-by: Rijksmuseum, Netherlands

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 C4/Auth.pm                                    |  2 ++
 .../intranet-tmpl/prog/en/modules/auth.tt     | 32 +++++++++++--------
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index 0e9e1d23a5e..7cf4c9dbc8d 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -1415,9 +1415,11 @@ sub checkauth {
     $template->param( OpacPublic => C4::Context->preference("OpacPublic") );
     $template->param( loginprompt => 1 ) unless $info{'nopermission'};
     if ( $auth_state eq 'additional-auth-needed' ) {
+        my $patron = Koha::Patrons->find( { userid => $userid } );
         $template->param(
             TwoFA_prompt => 1,
             invalid_otp_token => $invalid_otp_token,
+            notice_email_address => $patron->notice_email_address, # We could also pass logged_in_user if necessary
         );
     }
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt
index 41995bf423a..e2db5e0f30a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt
@@ -195,22 +195,26 @@
 
             $("#send_otp").on("click", function(e){
                 e.preventDefault();
+                [% UNLESS notice_email_address %]
+                    alert("Cannot send the notice, you don't have an email address defined.")
+                [% ELSE %]
                 $("#email_success").hide();
                 $("#email_error").hide();
-                $.ajax({
-                    url: '/api/v1/auth/otp/token_delivery',
-                    type: 'POST',
-                    success: function(data){
-                        let message = _("The code has been sent by email, please check your inbox.")
-                        $("#email_success").show().html(message);
-                    },
-                    error: function(data){
-                        let error = data.responseJSON && data.responseJSON.error == "email_not_sent"
-                            ? _("Email not sent, maybe you don't have an email address defined?")
-                            : _("Email not sent");
-                        $("#email_error").show().html(error);
-                    }
-                });
+                    $.ajax({
+                        url: '/api/v1/auth/otp/token_delivery',
+                        type: 'POST',
+                        success: function(data){
+                            let message = _("The code has been sent by email, please check your inbox.")
+                            $("#email_success").show().html(message);
+                        },
+                        error: function(data){
+                            let error = data.responseJSON && data.responseJSON.error == "email_not_sent"
+                                ? _("Email not sent, please contact the Koha administrator")
+                                : _("Something wrong happened, please contact the Koha administrator");
+                            $("#email_error").show().html(error);
+                        }
+                    });
+                [% END %]
             });
         });
     </script>
-- 
2.30.2