From 7f83bc626e871aeb2e1f0051dace14de79021243 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 19 Jan 2023 12:01:42 +0100
Subject: [PATCH] Bug 32426: Changes for opac-registration-verify
Content-Type: text/plain; charset=utf-8

Similar to changes in opac-memberentry.

Test plan:
Now also enable  PatronSelfRegistrationVerifyByEmail.
Make the same change in Patron again with return $self.
Restart all. Self register. Check your email.
Follow the link. Verify that you have a similar alert.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 .../en/modules/opac-registration-invalid.tt   |  7 +++
 opac/opac-registration-verify.pl              | 48 +++++++++++--------
 2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-invalid.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-invalid.tt
index e1562f37d2..16bfc5e07c 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-invalid.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-invalid.tt
@@ -37,6 +37,13 @@
                             <h1>Registration invalid!</h1>
 
                             <p>There were problems processing your registration. Please contact your library for help.</p>
+                            [% IF error_type OR error_info %]
+                                [% IF error_type == 'Koha::Exceptions::Patron::InvalidUserid' %]
+                                    <p>Error: Userid is not valid</p>
+                                [% ELSE %]
+                                    <p>Error [% error_type %]: [% error_info %]</p>
+                                [% END %]
+                            [% END %]
                         </div>
                     </div> <!-- /#registration-confirmation-error -->
                 </div> <!-- /.col-lg-10/12 -->
diff --git a/opac/opac-registration-verify.pl b/opac/opac-registration-verify.pl
index 673507ab3e..8c186dd45a 100755
--- a/opac/opac-registration-verify.pl
+++ b/opac/opac-registration-verify.pl
@@ -18,6 +18,7 @@
 use Modern::Perl;
 
 use CGI qw ( -utf8 );
+use Try::Tiny;
 
 use C4::Auth qw( get_template_and_user );
 use C4::Output qw( output_html_with_http_headers );
@@ -42,6 +43,7 @@ my $token = $cgi->param('token');
 my $m = Koha::Patron::Modifications->find( { verification_token => $token } );
 
 my ( $template, $borrowernumber, $cookie );
+my ( $error_type, $error_info );
 
 if (
     $m # The token exists and the email is unique if requested
@@ -51,15 +53,6 @@ if (
     )
   )
 {
-    ( $template, $borrowernumber, $cookie ) = get_template_and_user(
-        {
-            template_name   => "opac-registration-confirmation.tt",
-            type            => "opac",
-            query           => $cgi,
-            authnotrequired => 1,
-        }
-    );
-
     my $patron_attrs = $m->unblessed;
     $patron_attrs->{password} ||= Koha::AuthUtils::generate_password(Koha::Patron::Categories->find($patron_attrs->{categorycode}));
     my $consent_dt = delete $patron_attrs->{gdpr_proc_consent};
@@ -68,9 +61,15 @@ if (
     delete $patron_attrs->{verification_token};
     delete $patron_attrs->{changed_fields};
     delete $patron_attrs->{extended_attributes};
-    my $patron = Koha::Patron->new( $patron_attrs )->store;
 
-    Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $consent_dt;
+    my $patron;
+    try {
+        $patron = Koha::Patron->new( $patron_attrs )->store;
+        Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $patron && $consent_dt;
+    } catch {
+        $error_type = ref($_);
+        $error_info = "$_";
+    };
 
     if ($patron) {
         if( $m->extended_attributes ){
@@ -80,6 +79,14 @@ if (
         } else {
             $m->delete();
         }
+        ( $template, $borrowernumber, $cookie ) = get_template_and_user(
+            {
+                template_name   => "opac-registration-confirmation.tt",
+                type            => "opac",
+                query           => $cgi,
+                authnotrequired => 1,
+            }
+        );
         C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $patron->borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if C4::Context->preference('EnhancedMessagingPreferences');
 
         $template->param( password_cleartext => $patron->plain_text_password );
@@ -132,17 +139,16 @@ if (
         my ($theme, $news_lang, $availablethemes) = C4::Templates::themelanguage(C4::Context->config('opachtdocs'),'opac-registration-confirmation.tt','opac',$cgi);
         $template->param( news_lang => $news_lang );
     }
-
 }
-else {
-    ( $template, $borrowernumber, $cookie ) = get_template_and_user(
-        {
-            template_name   => "opac-registration-invalid.tt",
-            type            => "opac",
-            query           => $cgi,
-            authnotrequired => 1,
-        }
-    );
+
+if( !$template ) { # Missing token, patron exception, etc.
+    ( $template, $borrowernumber, $cookie ) = get_template_and_user({
+        template_name   => "opac-registration-invalid.tt",
+        type            => "opac",
+        query           => $cgi,
+        authnotrequired => 1,
+    });
+    $template->param( error_type => $error_type, error_info => $error_info );
 }
 
 output_html_with_http_headers $cgi, $cookie, $template->output;
-- 
2.30.2