diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/dataservice/ProjectController.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/dataservice/ProjectController.java index b030f465d5..41d8f13e05 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/dataservice/ProjectController.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/dataservice/ProjectController.java @@ -51,6 +51,7 @@ import software.uncharted.terarium.hmiserver.models.dataservice.project.ProjectAsset; import software.uncharted.terarium.hmiserver.models.dataservice.project.ProjectExport; import software.uncharted.terarium.hmiserver.models.permissions.PermissionRelationships; +import software.uncharted.terarium.hmiserver.models.permissions.PermissionUser; import software.uncharted.terarium.hmiserver.security.Roles; import software.uncharted.terarium.hmiserver.service.ClientEventService; import software.uncharted.terarium.hmiserver.service.CurrentUserService; @@ -928,10 +929,10 @@ public ResponseEntity getProjectPermissions(@PathVariab try { for (final RebacPermissionRelationship permissionRelationship : rebacProject.getPermissionRelationships()) { if (permissionRelationship.getSubjectType().equals(Schema.Type.USER)) { - permissions.addUser( - reBACService.getUser(permissionRelationship.getSubjectId()), - permissionRelationship.getRelationship() - ); + PermissionUser user = reBACService.getUser(permissionRelationship.getSubjectId()); + if (user != null) { + permissions.addUser(user, permissionRelationship.getRelationship()); + } } else if (permissionRelationship.getSubjectType().equals(Schema.Type.GROUP)) { permissions.addGroup( reBACService.getGroup(permissionRelationship.getSubjectId()), diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/permissions/GroupsController.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/permissions/GroupsController.java index fea7e37c10..9077ba3aa8 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/permissions/GroupsController.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/controller/permissions/GroupsController.java @@ -61,7 +61,9 @@ public ResponseEntity getGroup(@PathVariable("groupId") final S for (final RebacPermissionRelationship permissionRelationship : relationships) { if (permissionRelationship.getSubjectType().equals(Schema.Type.USER)) { final PermissionUser user = reBACService.getUser(permissionRelationship.getSubjectId()); - permissions.addUser(user, permissionRelationship.getRelationship()); + if (user != null) { + permissions.addUser(user, permissionRelationship.getRelationship()); + } } else if (permissionRelationship.getSubjectType().equals(Schema.Type.GROUP)) { final PermissionGroup group = reBACService.getGroup(permissionRelationship.getSubjectId()); permissions.addGroup(group, permissionRelationship.getRelationship()); diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/ProjectPermissionsService.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/ProjectPermissionsService.java index 86bfba08c0..dff896a026 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/ProjectPermissionsService.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/ProjectPermissionsService.java @@ -39,9 +39,11 @@ private List getContributorsByRelationships( if (relationships.length == 0 || List.of(relationships).contains(relationship)) { if (permissionRelationship.getSubjectType().equals(Schema.Type.USER)) { final PermissionUser user = reBACService.getUser(permissionRelationship.getSubjectId()); - final String name = user.getFirstName() + " " + user.getLastName(); - if (!contributorMap.containsKey(name)) { - contributorMap.put(name, new Contributor(name, user.getId(), relationship)); + if (user != null) { + final String name = user.getFirstName() + " " + user.getLastName(); + if (!contributorMap.containsKey(name)) { + contributorMap.put(name, new Contributor(name, user.getId(), relationship)); + } } } else if (permissionRelationship.getSubjectType().equals(Schema.Type.GROUP)) { final PermissionGroup group = reBACService.getGroup(permissionRelationship.getSubjectId()); diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/utils/rebac/ReBACService.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/utils/rebac/ReBACService.java index 11bd0beeef..af667b7de3 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/utils/rebac/ReBACService.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/utils/rebac/ReBACService.java @@ -292,27 +292,32 @@ public PermissionUser getUser(final String id) { final PermissionUser result = userCache.get(id, key_id -> { final UsersResource usersResource = keycloak.realm(REALM_NAME).users(); final UserResource userResource = usersResource.get(key_id); - final UserRepresentation userRepresentation = userResource.toRepresentation(); - - final List roles = new ArrayList<>(); - for (final RoleRepresentation roleRepresentation : userResource.roles().getAll().getRealmMappings()) { - if (roleRepresentation.getDescription().isBlank()) { - final PermissionRole role = new PermissionRole( - roleRepresentation.getId(), - roleRepresentation.getName() - // no users are acquired (to avoid circular references etc) - ); - roles.add(role); + try { + final UserRepresentation userRepresentation = userResource.toRepresentation(); + + final List roles = new ArrayList<>(); + for (final RoleRepresentation roleRepresentation : userResource.roles().getAll().getRealmMappings()) { + if (roleRepresentation.getDescription().isBlank()) { + final PermissionRole role = new PermissionRole( + roleRepresentation.getId(), + roleRepresentation.getName() + // no users are acquired (to avoid circular references etc) + ); + roles.add(role); + } } - } - return new PermissionUser( - userRepresentation.getId(), - userRepresentation.getFirstName(), - userRepresentation.getLastName(), - userRepresentation.getEmail(), - roles - ); + return new PermissionUser( + userRepresentation.getId(), + userRepresentation.getFirstName(), + userRepresentation.getLastName(), + userRepresentation.getEmail(), + roles + ); + } catch (Exception e) { + log.error("User identified by SpiceDB with id \"{}\" is not found in Keycloak.", id); + return null; + } }); log.trace("User Cache hit: {}, miss: {}", userCache.stats().hitCount(), userCache.stats().missCount()); return result;