From dbd121e17dbddfaf821f121073c3828aa2fc85a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Str=C3=B6der?= Date: Mon, 22 Feb 2021 18:03:33 +0100 Subject: [PATCH 1/4] set user attribute prior-login based on old value of last-login --- .../events/logging/LastLoginEventListenerProvider.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java index 933f3c5..2854230 100644 --- a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java +++ b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java @@ -10,6 +10,7 @@ import org.keycloak.models.RealmModel; import org.keycloak.models.RealmProvider; import org.keycloak.models.UserModel; +import java.util.Map; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -35,6 +36,12 @@ public class LastLoginEventListenerProvider implements EventListenerProvider { if (user != null) { log.info("Updating last login status for user: " + event.getUserId()); + Map userAttrs = user.getAttributes(); + if (userAttrs.containsKey("last-login")) { + String userLastLogin = userAttrs.get("last-login").toString(); + user.setSingleAttribute("prior-login", userLastLogin.substring(1, userLastLogin.length()-1)); + } + // Use current server time for login event LocalDateTime loginTime = LocalDateTime.now(); String loginTimeS = DateTimeFormatter.ISO_DATE_TIME.format(loginTime); From 8b5f3fe967ccd0bec68a50ab9134d945011d30fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Str=C3=B6der?= Date: Tue, 23 Feb 2021 01:26:15 +0100 Subject: [PATCH 2/4] properly extract List instance --- .../events/logging/LastLoginEventListenerProvider.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java index 2854230..81a23a6 100644 --- a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java +++ b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java @@ -10,6 +10,7 @@ import org.keycloak.models.RealmModel; import org.keycloak.models.RealmProvider; import org.keycloak.models.UserModel; +import java.util.List; import java.util.Map; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -36,10 +37,10 @@ public class LastLoginEventListenerProvider implements EventListenerProvider { if (user != null) { log.info("Updating last login status for user: " + event.getUserId()); - Map userAttrs = user.getAttributes(); + Map> userAttrs = user.getAttributes(); if (userAttrs.containsKey("last-login")) { - String userLastLogin = userAttrs.get("last-login").toString(); - user.setSingleAttribute("prior-login", userLastLogin.substring(1, userLastLogin.length()-1)); + String userLastLogin = userAttrs.get("last-login").get(0); + user.setSingleAttribute("prior-login", userLastLogin); } // Use current server time for login event From b0671c4d4cff416ef4116c7c7b7c8360b1fb031e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Str=C3=B6der?= Date: Tue, 23 Feb 2021 01:37:18 +0100 Subject: [PATCH 3/4] added sanity check for non-existent or empty last-login value --- .../events/logging/LastLoginEventListenerProvider.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java index 81a23a6..1c6cb38 100644 --- a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java +++ b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java @@ -40,7 +40,9 @@ public class LastLoginEventListenerProvider implements EventListenerProvider { Map> userAttrs = user.getAttributes(); if (userAttrs.containsKey("last-login")) { String userLastLogin = userAttrs.get("last-login").get(0); - user.setSingleAttribute("prior-login", userLastLogin); + if (userLastLogin != null && !userLastLogin.isEmpty()) { + user.setSingleAttribute("prior-login", userLastLogin); + } } // Use current server time for login event From 44aa04c7ea28956ce6d6ac7f520e452afc9ce11c Mon Sep 17 00:00:00 2001 From: Thore Date: Tue, 23 Feb 2021 02:03:29 +0100 Subject: [PATCH 4/4] Update src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java --- .../events/logging/LastLoginEventListenerProvider.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java index 1c6cb38..5b6bf5a 100644 --- a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java +++ b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java @@ -39,9 +39,9 @@ public class LastLoginEventListenerProvider implements EventListenerProvider { Map> userAttrs = user.getAttributes(); if (userAttrs.containsKey("last-login")) { - String userLastLogin = userAttrs.get("last-login").get(0); + List userLastLogin = userAttrs.get("last-login"); if (userLastLogin != null && !userLastLogin.isEmpty()) { - user.setSingleAttribute("prior-login", userLastLogin); + user.setSingleAttribute("prior-login", userLastLogin.get(0); } } @@ -62,4 +62,4 @@ public class LastLoginEventListenerProvider implements EventListenerProvider { // Nothing to close } -} \ No newline at end of file +}