From 1f956b4e6476d8d845a74a64c824d42e82a0c3e8 Mon Sep 17 00:00:00 2001 From: cgeorgilakis-grnet Date: Fri, 22 Nov 2024 12:41:18 +0200 Subject: [PATCH] Fix problem sending email from background process --- CHANGELOG.md | 6 +++ pom.xml | 2 +- ...CustomFreeMarkerEmailTemplateProvider.java | 47 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f46f9e..515dca9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes in keycloak-group-management will be documented in this file The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.2] - 2024-11-21 + +### Fixed +- Fix problem sending email from background process + + ## [1.0.1] - 2024-11-21 ### Changed diff --git a/pom.xml b/pom.xml index 4f3b113..3a1100c 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ 3.2.7.Final 17 17 - 1.0.1 + 1.0.2 diff --git a/src/main/java/org/rciam/plugins/groups/email/CustomFreeMarkerEmailTemplateProvider.java b/src/main/java/org/rciam/plugins/groups/email/CustomFreeMarkerEmailTemplateProvider.java index f0df6a6..6eb754a 100644 --- a/src/main/java/org/rciam/plugins/groups/email/CustomFreeMarkerEmailTemplateProvider.java +++ b/src/main/java/org/rciam/plugins/groups/email/CustomFreeMarkerEmailTemplateProvider.java @@ -1,17 +1,26 @@ package org.rciam.plugins.groups.email; import java.net.URI; +import java.text.MessageFormat; import java.time.LocalDate; import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; import org.keycloak.email.EmailException; import org.keycloak.email.freemarker.FreeMarkerEmailTemplateProvider; +import org.keycloak.email.freemarker.beans.ProfileBean; +import org.keycloak.forms.login.freemarker.model.UrlBean; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakUriInfo; import org.keycloak.models.UserModel; +import org.keycloak.theme.FreeMarkerException; +import org.keycloak.theme.Theme; +import org.keycloak.theme.beans.MessageFormatterMethod; import org.rciam.plugins.groups.helpers.Utils; public class CustomFreeMarkerEmailTemplateProvider extends FreeMarkerEmailTemplateProvider { @@ -392,5 +401,43 @@ private String rolesHtmlStrCalculation(List roles) { return sb.toString(); } + //override method in order not to have problem with finding Keycloak url from request + //This does not work with background process like notification and membership expiration emails + @Override + protected EmailTemplate processTemplate(String subjectKey, List subjectAttributes, String template, Map attributes) throws EmailException { + try { + Theme theme = getTheme(); + Locale locale = session.getContext().resolveLocale(user); + attributes.put("locale", locale); + + Properties messages = theme.getEnhancedMessages(realm, locale); + attributes.put("msg", new MessageFormatterMethod(locale, messages)); + + attributes.put("properties", theme.getProperties()); + attributes.put("realmName", getRealmName()); + attributes.put("user", new ProfileBean(user)); + + String subject = new MessageFormat(messages.getProperty(subjectKey, subjectKey), locale).format(subjectAttributes.toArray()); + String textTemplate = String.format("text/%s", template); + String textBody; + try { + textBody = freeMarker.processTemplate(attributes, textTemplate, theme); + } catch (final FreeMarkerException e) { + throw new EmailException("Failed to template plain text email.", e); + } + String htmlTemplate = String.format("html/%s", template); + String htmlBody; + try { + htmlBody = freeMarker.processTemplate(attributes, htmlTemplate, theme); + } catch (final FreeMarkerException e) { + throw new EmailException("Failed to template html email.", e); + } + + return new EmailTemplate(subject, textBody, htmlBody); + } catch (Exception e) { + throw new EmailException("Failed to template email", e); + } + } + }