From 2f2abee670f4f89ecd5a87bf9045af6360793d68 Mon Sep 17 00:00:00 2001 From: hangy Date: Sat, 12 Oct 2024 13:16:01 +0200 Subject: [PATCH] feat: Forward `newsletter` user attribute to Redis --- .../github/keycloak/events/RedisClient.java | 15 ++++++++++++--- .../github/keycloak/utils/UserAttributes.java | 5 +++++ .../github/keycloak/events/Utils.java | 18 ++++++++++++++---- 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/main/java/openfoodfacts/github/keycloak/utils/UserAttributes.java diff --git a/src/main/java/openfoodfacts/github/keycloak/events/RedisClient.java b/src/main/java/openfoodfacts/github/keycloak/events/RedisClient.java index 92e3d48e6..ee10c1f8d 100644 --- a/src/main/java/openfoodfacts/github/keycloak/events/RedisClient.java +++ b/src/main/java/openfoodfacts/github/keycloak/events/RedisClient.java @@ -7,6 +7,7 @@ import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; +import openfoodfacts.github.keycloak.utils.UserAttributes; import redis.clients.jedis.JedisPooled; import redis.clients.jedis.StreamEntryID; @@ -28,7 +29,10 @@ public void postUserRegistered(final UserModel user, final RealmModel realm) { throw new IllegalArgumentException("realm"); } - postUserEvent("user-registered", user, realm); + final String newsletter = user.getFirstAttribute(UserAttributes.NEWSLETTER); + final HashMap additionalData = new HashMap<>(); + putIfNotNull(additionalData, "newsletter", newsletter); + postUserEvent("user-registered", user, realm, additionalData); } public void postUserDeleted(final UserModel user, final RealmModel realm) { @@ -40,10 +44,11 @@ public void postUserDeleted(final UserModel user, final RealmModel realm) { throw new IllegalArgumentException("realm"); } - postUserEvent("user-deleted", user, realm); + postUserEvent("user-deleted", user, realm, null); } - private void postUserEvent(final String key, final UserModel user, final RealmModel realm) { + private void postUserEvent(final String key, final UserModel user, final RealmModel realm, + final HashMap additionalData) { if (key == null) { throw new IllegalArgumentException("key"); } @@ -62,6 +67,10 @@ private void postUserEvent(final String key, final UserModel user, final RealmMo putIfNotNull(data, "userName", user.getUsername()); putIfNotNull(data, "realm", realm.getName()); + if (additionalData != null) { + data.putAll(additionalData); + } + try { this.addEntriesToRedisStream(key, data); log.debugf("A new %s event has been forwarded to Redis", key); diff --git a/src/main/java/openfoodfacts/github/keycloak/utils/UserAttributes.java b/src/main/java/openfoodfacts/github/keycloak/utils/UserAttributes.java new file mode 100644 index 000000000..0fbe4063b --- /dev/null +++ b/src/main/java/openfoodfacts/github/keycloak/utils/UserAttributes.java @@ -0,0 +1,5 @@ +package openfoodfacts.github.keycloak.utils; + +public class UserAttributes { + public static final String NEWSLETTER = "newsletter"; +} diff --git a/src/test/java/openfoodfacts/github/keycloak/events/Utils.java b/src/test/java/openfoodfacts/github/keycloak/events/Utils.java index d7befbd92..80862c4e1 100644 --- a/src/test/java/openfoodfacts/github/keycloak/events/Utils.java +++ b/src/test/java/openfoodfacts/github/keycloak/events/Utils.java @@ -125,7 +125,8 @@ public Set getPropertyNames() { }; } - public static KeycloakSession createKeycloakSession(final boolean isVerifyEmail, final boolean isEmailVerified) { + public static KeycloakSession createKeycloakSession(final boolean isVerifyEmail, final boolean isEmailVerified, + final String userNewsletterAttribute) { return new KeycloakSession() { @Override @@ -1850,7 +1851,11 @@ public void removeAttribute(String name) { @Override public String getFirstAttribute(String name) { - throw new UnsupportedOperationException("Unimplemented method 'getFirstAttribute'"); + if ("newsletter".equals(name)) { + return userNewsletterAttribute; + } + + return null; } @Override @@ -2191,11 +2196,16 @@ public IdentityProviderStorageProvider identityProviders() { } public static KeycloakSessionFactory createKeycloakSessionFactory() { - return createKeycloakSessionFactory(true, false); + return createKeycloakSessionFactory(true, false, null); } public static KeycloakSessionFactory createKeycloakSessionFactory(final boolean isVerifyEmail, final boolean isEmailVerified) { + return createKeycloakSessionFactory(isVerifyEmail, isEmailVerified, null); + } + + public static KeycloakSessionFactory createKeycloakSessionFactory(final boolean isVerifyEmail, + final boolean isEmailVerified, final String userNewsletterAttribute) { return new KeycloakSessionFactory() { private ProviderEventListener listener; @@ -2225,7 +2235,7 @@ public void invalidate(KeycloakSession session, InvalidableObjectType type, Obje @Override public KeycloakSession create() { - return createKeycloakSession(isVerifyEmail, isEmailVerified); + return createKeycloakSession(isVerifyEmail, isEmailVerified, userNewsletterAttribute); } @Override