Skip to content

Commit

Permalink
Filter users who have already been disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhz committed Feb 11, 2025
1 parent a4d1159 commit 99c2100
Showing 1 changed file with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,36 @@ object KeycloakInactiveUsers extends App {
.toEpochMilli

users.flatMap { user =>
val loginEvents = realmResource
.getEvents(
java.util.Arrays.asList("LOGIN"),
null, //client
user.getId,
null, // dateFrom
null, // dateTo
null, // ipAddress
0, // firstResult
1 // maxResults - we only need the most recent
).asScala.toList

val lastLogin = loginEvents.headOption.map(_.getTime)

if (lastLogin.isEmpty || lastLogin.exists(_ < cutoffTime)) {
Some(InactiveUser(
id = user.getId,
username = user.getUsername,
email = Option(user.getEmail),
firstName = Option(user.getFirstName),
lastName = Option(user.getLastName),
lastLoginDate = lastLogin.map(formatDate)
))
} else None
val userResource = usersResource.get(user.getId)
val userRep = userResource.toRepresentation()

if (!userRep.isEnabled) {
Nil
} else {
val loginEvents = realmResource
.getEvents(
java.util.Arrays.asList("LOGIN"),
null, //client
user.getId,
null, // dateFrom
null, // dateTo
null, // ipAddress
0, // firstResult
1 // maxResults - we only need the most recent
).asScala.toList

val lastLogin = loginEvents.headOption.map(_.getTime)
if (lastLogin.isEmpty || lastLogin.exists(_ < cutoffTime)) {
Some(InactiveUser(
id = user.getId,
username = user.getUsername,
email = Option(user.getEmail),
firstName = Option(user.getFirstName),
lastName = Option(user.getLastName),
lastLoginDate = lastLogin.map(formatDate)
))
} else None
}
}
}

Expand Down

0 comments on commit 99c2100

Please sign in to comment.