From 530a3db46868b40d989dc74897b4f74856da9a56 Mon Sep 17 00:00:00 2001 From: Carol Alexandru Date: Thu, 22 Feb 2024 22:28:48 +0900 Subject: [PATCH] debugging participation issue --- .../ch/uzh/ifi/access/config/SecurityConfig.kt | 14 ++++++++++---- .../ch/uzh/ifi/access/service/RoleService.kt | 7 ++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ch/uzh/ifi/access/config/SecurityConfig.kt b/src/main/kotlin/ch/uzh/ifi/access/config/SecurityConfig.kt index b304c81..fcfa325 100644 --- a/src/main/kotlin/ch/uzh/ifi/access/config/SecurityConfig.kt +++ b/src/main/kotlin/ch/uzh/ifi/access/config/SecurityConfig.kt @@ -28,7 +28,9 @@ import org.springframework.web.filter.CommonsRequestLoggingFilter import java.nio.file.Path import java.time.Duration import java.time.Instant +import java.time.LocalDate import java.time.LocalDateTime +import java.time.ZoneId @AllArgsConstructor @@ -149,6 +151,7 @@ class AuthenticationSuccessListener( override fun onApplicationEvent(event: AuthenticationSuccessEvent) { val username = event.authentication.name + logger.debug { "USER [$username] LOGGED IN" } roleService.getUserRepresentationForUsername(username)?.let { user -> // TODO: clean up this horrible mess val currentAttributes = user.attributes ?: mutableMapOf() @@ -159,13 +162,16 @@ class AuthenticationSuccessListener( else { try { val lastSync = currentAttributes["roles_synced_at"]!!.first() - val timeStamp = lastSync.substring(0, Math.min(lastSync.length, lastSync.indexOf('.') + 4)) + "Z" - val dateTime = Instant.parse(timeStamp) - val now = Instant.now() - if (Duration.between(dateTime, now).toMinutes() > 10) { + val lastSyncInstant = LocalDateTime.parse(lastSync) + val now = LocalDateTime.now() + val diff = Duration.between(lastSyncInstant, now).toMinutes() + if (diff > 10) { logger.debug { "syncing $username to courses after more than 10min" } courseService.updateStudentRoles(username) } + else { + logger.debug { "only $diff minutes elapsed since last sync of $username at ${lastSync} (now: $now)" } + } } catch (e: Exception) { logger.debug { "problem ($e, ${e.stackTrace}) with sync calculation; syncing $username to courses anyway" } courseService.updateStudentRoles(username) diff --git a/src/main/kotlin/ch/uzh/ifi/access/service/RoleService.kt b/src/main/kotlin/ch/uzh/ifi/access/service/RoleService.kt index 2ec565d..2f14c52 100644 --- a/src/main/kotlin/ch/uzh/ifi/access/service/RoleService.kt +++ b/src/main/kotlin/ch/uzh/ifi/access/service/RoleService.kt @@ -30,7 +30,11 @@ class RoleService( } fun getUserRepresentationForUsername(username: String): UserRepresentation? { - return accessRealm.users().search(username, true).firstOrNull() + val res = accessRealm.users().search(username, true).firstOrNull() + if (res == null) { + logger.debug { "RoleService: Could not find user $username" } + } + return res } fun createCourseRoles(courseSlug: String?): String? { @@ -144,6 +148,7 @@ class RoleService( fun updateStudentRoles(course: Course, registrationIDs: Set, username: String) { val role = accessRealm.roles()[Role.STUDENT.withCourse(course.slug)] val rolesToAdd = listOf(role.toRepresentation()) + logger.debug { "registered students for ${course.slug}: $registrationIDs"} logger.debug { "B: updating roles for ${username} (roles to add: ${rolesToAdd})"} logger.debug { "Searching if ${username} is in ${role.getUserMembers(0, -1).size} members of ${role} in keycloak" } role.getUserMembers(0, -1)