From 7c4e8eac99452cc9810b62fefcba09034519c45c Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Thu, 5 Jun 2025 05:41:44 +0000 Subject: [PATCH] Bug 25090: Enable library to verify OPAC self registrations This enhancement changes the PatronSelfRegistrationVerifyByEmail system preference to have the following options: * don't require (same as 0 previously) * patron verifies (same as 1 previously) * library verifies, based on the EmailPatronRegistrations syspref setting If the syspref is set to "based on the EmailPatronRegistrations" setting, this allows the library to redirect the verification email so that they can essentially moderate accounts that self-register via the OPAC. This enhancement also introduces a new notice OPAC_REG_VERIFY_LIB which includes the new account's details and the verification link. When the library confirms a self-registered account, this triggers a password recovery email to the new patron. To test: 1. Apply patches, install database updates, and restart services (if using ktd, do `koha-upgrade-schema kohadev` then `restart_all`) 2. Go to the staff interface, Koha administration, system preferences. Go to the OPAC tab and find the self-registration and modification section. 3. By default, the PatronSelfRegistrationVerifyByEmail is set to "not required". Change this to "must verify themselves via email". 4. Change EmailPatronRegistrations to anything other than "none" (we're not specifically testing this functionality). 5. Confirm that PatronSelfRegistration is set to "Allow", as are other required preferences (these should be set by default in ktd). 6. Go to the OPAC in another window. You may be automatically logged in, if so, log out. 7. On the homepage, click the "Create an account" link under the login section. 8. Fill in the required fields and submit. 9. In your commandline, go to the database and view the results from the message_queue table (`koha-mysql kohadev` then `select * from message_queue\G`) 10. Confirm there is a queued message to the new account holder with a verification link. Open the verification link in your browser (you may need to swap out the OPACBaseURL if that isn't set correctly) 11. Confirm the account's registration. 12. Repeat step 9. Confirm there is a queued message to whoever you set EmailPatronRegistrations to, confirming a new account was created. 13. Go to your staff interface tab (you may need to log in again). 14. Go to the staff interface, Koha administration, system preferences. Go to the OPAC tab and find the self-registration and modification section. 15. Change PatronSelfRegistrationVerifyByEmail to "must be verified via email sent to EmailPatronRegistrations" 16. Repeat steps 6-9. 17. Confirm there is a queued message to the EmailPatronRegistrations setting with a verification link. Open the verification link in your browser (you may need to swap out the OPACBaseURL if that isn't set correctly) 18. Confirm the account's registration. 19. Confirm the screen says a password recovery email has been generated. 20. Repeat step 9. Confirm there is a queued recovery email message to the new patron. 21. Confirm tests pass t/db_dependent/Koha/Patron/Modifications.t Sponsored-by: Mental Health Education & Resource Centre Signed-off-by: David Nind --- Koha/Patron/Modification.pm | 50 +++++++++++++++++++ .../modules/opac-registration-confirmation.tt | 28 +++++++---- .../modules/opac-registration-email-sent.tt | 10 ++-- opac/opac-memberentry.pl | 46 ++++++++++------- opac/opac-registration-verify.pl | 16 ++++-- t/db_dependent/Koha/Patron/Modifications.t | 31 +++++++++++- 6 files changed, 145 insertions(+), 36 deletions(-) diff --git a/Koha/Patron/Modification.pm b/Koha/Patron/Modification.pm index 5c36a71cbc..c2df2c8d47 100644 --- a/Koha/Patron/Modification.pm +++ b/Koha/Patron/Modification.pm @@ -155,6 +155,56 @@ sub approve { return $self->delete(); } +=head3 notify_library_of_registration + + my $message_id = $patron_to_be_verified->notify_library_of_registration( $email_patron_registrations ); + +Send patron registration verification email to library if EmailPatronRegistrations system preference is enabled. + +Returns message ID. + +=cut + +sub notify_library_of_registration { + my ( $self, $email_patron_registrations ) = @_; + + if ( + my $letter = C4::Letters::GetPreparedLetter( + module => 'members', + letter_code => 'OPAC_REG_VERIFY_LIB', + branchcode => $self->branchcode, + lang => $self->lang || 'default', + tables => { 'borrower_modifications' => $self->verification_token }, + ) + ) + { + my $to_address; + if ( $email_patron_registrations eq "BranchEmailAddress" ) { + my $library = Koha::Libraries->find( $self->branchcode ); + $to_address = $library->inbound_email_address; + } elsif ( $email_patron_registrations eq "KohaAdminEmailAddress" ) { + $to_address = C4::Context->preference('ReplytoDefault') + || C4::Context->preference('KohaAdminEmailAddress'); + } else { + $to_address = + C4::Context->preference('EmailAddressForPatronRegistrations') + || C4::Context->preference('ReplytoDefault') + || C4::Context->preference('KohaAdminEmailAddress'); + } + + my $message_id = C4::Letters::EnqueueLetter( + { + letter => $letter, + to_address => $to_address, + message_transport_type => 'email' + } + ) or warn "can't enqueue letter $letter"; + if ($message_id) { + return $message_id; + } + } +} + =head3 type =cut diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-confirmation.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-confirmation.tt index 6493a6ee45..d5506c247c 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-confirmation.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-confirmation.tt @@ -52,6 +52,7 @@
[% INCLUDE 'csrf-token.inc' %] +
@@ -60,8 +61,11 @@

Registration complete!

-

You have successfully registered your new account.

- [% IF Koha.Preference('PatronSelfRegistrationPrefillForm') %] +

You have successfully registered a new account.

+ + [% IF library_approval %] +

An password recovery email has been sent.

+ [% ELSIF Koha.Preference('PatronSelfRegistrationPrefillForm') %]

To log in, use the following credentials:

@@ -80,13 +84,15 @@

[% END %] -

- [% IF borrower.category.effective_change_password %] - For your convenience, the login box on this page has been pre-filled with this data. Please log in and change your password. - [% ELSE %] - For your convenience, the login box on this page has been pre-filled with this data. Please log in. - [% END %] -

+ [% UNLESS Koha.Preference('PatronSelfRegistrationVerifyByEmail') == "library" %] +

+ [% IF borrower.category.effective_change_password %] + For your convenience, the login box on this page has been pre-filled with this data. Please log in and change your password. + [% ELSE %] + For your convenience, the login box on this page has been pre-filled with this data. Please log in. + [% END %] +

+ [% END %] [% END %] [% IF ( PatronSelfRegistrationAdditionalInstructions ) %] @@ -109,13 +115,13 @@
Log in to your account: [% PROCESS login_label for="userid" %] - [% IF Koha.Preference('PatronSelfRegistrationPrefillForm') %] + [% IF Koha.Preference('PatronSelfRegistrationPrefillForm') && Koha.Preference('PatronSelfRegistrationVerifyByEmail') != "library" %] [% ELSE %] [% END %] - [% IF Koha.Preference('PatronSelfRegistrationPrefillForm') %] + [% IF Koha.Preference('PatronSelfRegistrationPrefillForm') && Koha.Preference('PatronSelfRegistrationVerifyByEmail') != "library" %] [% ELSE %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-email-sent.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-email-sent.tt index 00f80b21b4..a473d25ec2 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-email-sent.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-registration-email-sent.tt @@ -31,11 +31,15 @@
-

Please confirm your registration

+

Your account is pending confirmation

-

A confirmation email will be sent shortly to the email address [% email | html %].

+ [% IF Koha.Preference('PatronSelfRegistrationVerifyByEmail') == "patron" %] +

A confirmation email will be sent shortly to the email address [% email | html %].

-

Your account will not be activated until you follow the link provided in the confirmation email.

+

Your account will not be activated until you follow the link provided in the confirmation email.

+ [% ELSE %] +

An email will be sent shortly to the library to confirm the activation of your account.

+ [% END %]
diff --git a/opac/opac-memberentry.pl b/opac/opac-memberentry.pl index 14ec05e753..83ebb5a73f 100755 --- a/opac/opac-memberentry.pl +++ b/opac/opac-memberentry.pl @@ -175,7 +175,7 @@ if ( $op eq 'cud-create' ) { } elsif ( !$libraries->find( $borrower{branchcode} ) ) { die "Branchcode not allowed"; # They hack the form } else { - if ( C4::Context->preference('PatronSelfRegistrationVerifyByEmail') ) { + if ( C4::Context->preference('PatronSelfRegistrationVerifyByEmail') ne "off" ) { ( $template, $borrowernumber, $cookie ) = get_template_and_user( { template_name => "opac-registration-email-sent.tt", @@ -198,27 +198,37 @@ if ( $op eq 'cud-create' ) { $borrower{extended_attributes} = to_json($attributes); $borrower{borrowernumber} = 0; # prevent warn Missing value for PK column - Koha::Patron::Modification->new( \%borrower )->store(); + my $mod_borrower = Koha::Patron::Modification->new( \%borrower )->store(); #Send verification email - my $letter = C4::Letters::GetPreparedLetter( - module => 'members', - letter_code => 'OPAC_REG_VERIFY', - lang => 'default', # Patron does not have a preferred language defined yet - tables => { - borrower_modifications => $verification_token, - }, - ); + if ( C4::Context->preference('PatronSelfRegistrationVerifyByEmail') eq "library" ) { - my $message_id = C4::Letters::EnqueueLetter( - { - letter => $letter, - message_transport_type => 'email', - to_address => $borrower{'email'}, - from_address => C4::Context->preference('KohaAdminEmailAddress'), + # Send new patron registration verification to library + my $notify_library = C4::Context->preference('EmailPatronRegistrations'); + if ($notify_library) { + my $message_id = $mod_borrower->notify_library_of_registration($notify_library); + C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id; } - ); - C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id; + } else { + my $letter = C4::Letters::GetPreparedLetter( + module => 'members', + letter_code => 'OPAC_REG_VERIFY', + lang => 'default', # Patron does not have a preferred language defined yet + tables => { + borrower_modifications => $verification_token, + }, + ); + + my $message_id = C4::Letters::EnqueueLetter( + { + letter => $letter, + message_transport_type => 'email', + to_address => $borrower{'email'}, + from_address => C4::Context->preference('KohaAdminEmailAddress'), + } + ); + C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id; + } } else { $borrower{password} ||= Koha::AuthUtils::generate_password( Koha::Patron::Categories->find( $borrower{categorycode} ) ); diff --git a/opac/opac-registration-verify.pl b/opac/opac-registration-verify.pl index d7fea85843..208d2ba9ae 100755 --- a/opac/opac-registration-verify.pl +++ b/opac/opac-registration-verify.pl @@ -32,6 +32,7 @@ use Koha::Patrons; use Koha::Patron::Consent; use Koha::Patron::Modifications; use Koha::Patron::Categories; +use Koha::Patron::Password::Recovery qw( SendPasswordRecoveryEmail ); my $cgi = CGI->new; my $dbh = C4::Context->dbh; @@ -41,8 +42,9 @@ unless ( C4::Context->preference('PatronSelfRegistration') ) { exit; } -my $token = $cgi->param('token'); -my $op = $cgi->param('op'); +my $token = $cgi->param('token'); +my $library_approval = $cgi->param("library_approval"); +my $op = $cgi->param('op'); my $confirmed; if ( $op && $op eq 'cud-confirmed' ) { $confirmed = 1; @@ -73,7 +75,10 @@ if ( $rego_found authnotrequired => C4::Context->preference("OpacPublic") ? 1 : 0, } ); - $template->param( "token" => $token ); + $template->param( + "token" => $token, + library_approval => $library_approval, + ); } elsif ( $rego_found and $confirmed ) { @@ -121,6 +126,11 @@ if ( $rego_found $template->param( password_cleartext => $patron->plain_text_password ); $template->param( borrower => $patron ); + if ($library_approval) { + SendPasswordRecoveryEmail( $patron, $patron->notice_email_address ); + $template->param( library_approval => 1 ); + } + # If 'AutoEmailNewUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode. if ( C4::Context->preference("AutoEmailNewUser") ) { diff --git a/t/db_dependent/Koha/Patron/Modifications.t b/t/db_dependent/Koha/Patron/Modifications.t index f430fa452d..a4381cdd1a 100755 --- a/t/db_dependent/Koha/Patron/Modifications.t +++ b/t/db_dependent/Koha/Patron/Modifications.t @@ -19,7 +19,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 7; +use Test::More tests => 8; use Test::Exception; use t::lib::TestBuilder; @@ -508,3 +508,32 @@ subtest 'dateofbirth tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'notify_library_of_registration() tests' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + Koha::Patron::Modifications->search->delete; + + t::lib::Mocks::mock_preference( 'EmailAddressForPatronRegistrations', 'library@localhost' ); + t::lib::Mocks::mock_preference( 'EmailPatronRegistrations', 'EmailAddressForPatronRegistrations' ); + t::lib::Mocks::mock_preference( 'PatronSelfRegistrationVerifyByEmail', 'library' ); + + # Create new pending modification + my $borrower = Koha::Patron::Modification->new( + { + verification_token => '1234567890', + changed_fields => 'surname,firstname', + surname => 'Hall', + firstname => 'Kyle' + } + )->store(); + + my $message_id = $borrower->notify_library_of_registration( C4::Context->preference('EmailPatronRegistrations') ); + + ok( $message_id, "Verification email is generated" ); + + $schema->storage->txn_rollback; +}; -- 2.39.5