set user attribute prior-login based on old value of last-login

This commit is contained in:
Michael Ströder 2021-02-22 18:03:33 +01:00
parent 4fd7a56a4e
commit dbd121e17d

View file

@ -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);