Skip to content

Commit

Permalink
Fix problem sending email from background process
Browse files Browse the repository at this point in the history
  • Loading branch information
cgeorgilakis-grnet committed Nov 22, 2024
1 parent 42be883 commit 1f956b4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<quarkus.version>3.2.7.Final</quarkus.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<agm-version>1.0.1</agm-version>
<agm-version>1.0.2</agm-version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -392,5 +401,43 @@ private String rolesHtmlStrCalculation(List<String> 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<Object> subjectAttributes, String template, Map<String, Object> 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);
}
}


}

0 comments on commit 1f956b4

Please sign in to comment.