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 '<welcome>1</welcome>' 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 <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Martin Renvoize 2022-04-28 15:56:55 +01:00 committed by Tomas Cohen Arazi
parent 63119a8686
commit e9e10c5e72
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
1 changed files with 39 additions and 2 deletions

View File

@ -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.
<user>cn=Manager,dc=metavore,dc=com</user> <!-- DN, if not anonymous -->
<pass>metavore</pass> <!-- password, if not anonymous -->
<replicate>1</replicate> <!-- add new users from LDAP to Koha database -->
<welcome>1</welcome> <!-- send new users the welcome email when added via replicate -->
<update>1</update> <!-- update existing users in Koha database -->
<auth_by_bind>0</auth_by_bind> <!-- set to 1 to authenticate by binding instead of
password comparison, e.g., to use Active Directory -->