Skip to content

Commit

Permalink
feat: Forward newsletter user attribute to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
hangy committed Oct 12, 2024
1 parent ec60a8b commit 2f2abee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, String> additionalData = new HashMap<>();
putIfNotNull(additionalData, "newsletter", newsletter);
postUserEvent("user-registered", user, realm, additionalData);
}

public void postUserDeleted(final UserModel user, final RealmModel realm) {
Expand All @@ -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<String, String> additionalData) {
if (key == null) {
throw new IllegalArgumentException("key");
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package openfoodfacts.github.keycloak.utils;

public class UserAttributes {
public static final String NEWSLETTER = "newsletter";
}
18 changes: 14 additions & 4 deletions src/test/java/openfoodfacts/github/keycloak/events/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public Set<String> 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2f2abee

Please sign in to comment.