Search user by id and fallback to username when needed

- prevents performance issues when reading policies as users are always stored by id.

Closes #35796

Signed-off-by: Stefan Guilhen <sguilhen@redhat.com>
This commit is contained in:
Stefan Guilhen
2024-12-11 09:37:53 -03:00
committed by Pedro Igor
parent 16a42d5a64
commit a43b65281d

View File

@@ -166,10 +166,11 @@ public class UserPolicyProviderFactory implements PolicyProviderFactory<UserPoli
KeycloakSession session = authorization.getKeycloakSession();
RealmModel realm = authorization.getRealm();
UserProvider userProvider = session.users();
UserModel user = userProvider.getUserByUsername(realm, userId);
UserModel user = userProvider.getUserById(realm, userId);
if (user == null) {
user = userProvider.getUserById(realm, userId);
// fallback - userId is possibly a username
user = userProvider.getUserByUsername(realm, userId);
}
return user;