[keycloak-user] keycloak-session returns userModel as null in eventListenerProvider SPI

Marek Posolda mposolda at redhat.com
Fri Nov 18 15:43:08 EST 2016


On 16/11/16 22:10, abhishek raghav wrote:
> Hi
>
> I am trying to implement a welcome email to newly registered users by admin.
> I am using event listener SPI and emailSenderProvider to do this.
>
> I am implementing a provider for the Event Listener SPI and notifying users
> by sending an email and react to create user OperationType using
> emailSenderProvider.
>
> Here i am overriding, onEvent method which gets triggered on any
> AdminEvents.
> Now inside this method, i am preparing the content and calling
> emailSenderProvider.send() which takes realmModel and userModel.
>
> The issue is, I am able to get the realmModel, but not the userModel.
> Code for reference:
> @Override
> public void onEvent(AdminEvent event, boolean includeRepresentation) {
> UserModel user =
> session.users().getUserById(event.getAuthDetails().getUserId(), realm);
> ......
event.getAuthDetails().getUserId() points to the details of the admin 
user, who is authenticated to the admin session. Not the user you are 
trying to edit. I bet that realm points to some custom realm you created 
(eg. "foo" ), however event.getAuthDetails().getUserId() points to the 
userID of admin user from "master" realm. Hence the 
session.users().getUserById returning null. Could that be the case?

If you want the ID of actual registered user, you likely need to decode 
it from resourcePath.

Hope it helps,
Marek
>
> Same works perfectly in the onEvent() for loginEvents.
>
> Is it that the newly created resource is not available in the session but
> when we try updating an existing resource, it is able to fetch it.
>
> Please help me where I am doing wrong. Or is there any other way to get the
> userModel object.
>
> Thanks in advance.
>
> Cheers
> Abhishek
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user




More information about the keycloak-user mailing list