From e9e10c5e72557f8a46fb224a979c01bf90e6935b Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 28 Apr 2022 15:56:55 +0100 Subject: [PATCH] Bug 30646: Add welcome email support to LDAP This patch adds the ability to enable the welcome email notice for new users added via the LDAP replicate option. Test plan 1) Configure LDAP for authentication 2) Enable the welcome email by adding '1' to your ldap config block 3) Confirm you have replicate enabled for your LDAP config 4) Attempt to login with an entirely new user to Koha using the LDAP connection (with a user who has a valid email address mapped to Koha borrower fields) 5) Confirm the email is sent by looking at the notices for the new user. Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- C4/Auth_with_ldap.pm | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/C4/Auth_with_ldap.pm b/C4/Auth_with_ldap.pm index 8bb14c3253..86cdb9185f 100644 --- a/C4/Auth_with_ldap.pm +++ b/C4/Auth_with_ldap.pm @@ -23,6 +23,7 @@ use Carp qw( croak ); use C4::Context; use C4::Members::Messaging; use C4::Auth qw( checkpw_internal ); +use C4::Letters qw( GetPreparedLetter EnqueueLetter ); use Koha::Patrons; use Koha::AuthUtils qw( hash_password ); use Net::LDAP; @@ -74,7 +75,8 @@ if(defined $ldap->{categorycode_mapping}) { my %config = ( anonymous => defined ($ldap->{anonymous_bind}) ? $ldap->{anonymous_bind} : 1, replicate => defined($ldap->{replicate}) ? $ldap->{replicate} : 1, # add from LDAP to Koha database for new user - update => defined($ldap->{update} ) ? $ldap->{update} : 1, # update from LDAP to Koha database for existing user + welcome => defined($ldap->{welcome}) ? $ldap->{welcome} : 0, # send welcome notice when patron is added via replicate + update => defined($ldap->{update}) ? $ldap->{update} : 1, # update from LDAP to Koha database for existing user ); sub description { @@ -225,7 +227,41 @@ sub checkpw_ldap { )->store; die "Insert of new patron failed" unless $patron; $borrowernumber = $patron->borrowernumber; - C4::Members::Messaging::SetMessagingPreferencesFromDefaults( { borrowernumber => $borrowernumber, categorycode => $borrower{'categorycode'} } ); + C4::Members::Messaging::SetMessagingPreferencesFromDefaults( + { + borrowernumber => $borrowernumber, + categorycode => $borrower{'categorycode'} + } + ); + + # Send welcome email if enabled + if ( $config{welcome} ) { + my $emailaddr = $patron->notice_email_address; + + # if we manage to find a valid email address, send notice + if ($emailaddr) { + my $letter = C4::Letters::GetPreparedLetter( + module => 'members', + letter_code => 'WELCOME', + branchcode => $patron->branchcode, + lang => $patron->lang || 'default', + tables => { + 'branches' => $patron->branchcode, + 'borrowers' => $patron->borrowernumber, + }, + want_librarian => 1, + ) or return; + + my $message_id = C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->id, + to_address => $emailaddr, + message_transport_type => 'email' + } + ); + } + } } else { return 0; # B2, D2 } @@ -508,6 +544,7 @@ Example XML stanza for LDAP configuration in KOHA_CONF. cn=Manager,dc=metavore,dc=com metavore 1 + 1 1 0 -- 2.39.5