From a0b51b1e1426f7ffa897a71d61524bbe2c826c0c 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. --- C4/Auth_with_ldap.pm | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/C4/Auth_with_ldap.pm b/C4/Auth_with_ldap.pm index 7f6c99a9a4..915c7a8edf 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 { @@ -226,6 +228,37 @@ sub checkpw_ldap { die "Insert of new patron failed" unless $patron; $borrowernumber = $patron->borrowernumber; 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) { + eval { + my $letter = 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 = EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->id, + to_address => $emailaddr, + message_transport_type => 'email' + } + ); + }; + } + } } else { return 0; # B2, D2 } @@ -508,6 +541,7 @@ Example XML stanza for LDAP configuration in KOHA_CONF. cn=Manager,dc=metavore,dc=com metavore 1 + 1 1 0 -- 2.20.1