p2-inc / keycloak-orgs

Single realm, multi-tenancy for SaaS apps
https://phasetwo.io
Other
417 stars 72 forks source link

Executing an action on invitation accept #268

Closed youssefbennour closed 3 months ago

youssefbennour commented 3 months ago

Hi, I have a requirement to execute some operation on my application when a user accept an organization invtation he's been sent. I have been reading the source code, but I can't find an event that's triggered on user invitation accept. Does such an event exists ? If not, could you give suggestions on how I could approach this requirement ?

xgp commented 3 months ago

There are events emitted when the user accepts or rejects an invitation:

https://github.com/p2-inc/keycloak-orgs/blob/main/src/main/java/io/phasetwo/service/auth/invitation/InvitationRequiredAction.java#L95-L111

youssefbennour commented 3 months ago

@xgp I was testing invitation to see the details of events so I can catch it, and I found that the triggered event is always of type CUSTOM_REQUIRED_ACTION_ERROR, even when the invitation is accepted and everything works fine : image

What do you think ?

xgp commented 3 months ago

@rtufisi Can you try to reproduce? In prod for us this is working fine, but we are on an earlier version.

youssefbennour commented 3 months ago

I'm using quay.io/phasetwo/phasetwo-keycloak:25.0.1

How to reproduce: 1 - Login to your keycloak server using admin credentials 2 - Navigate to your realm 3 - Create a new organization 4 - Invite an existing realm member by email 5 - Navigate to the account console and login with the invited user credintials. 6 - After successful login, accept organization(s) invitation(s)

Upon the last step, the logger logs a similar CUSTOM_REQUIRED_ACTION_ERROR as I've provided you with above. I've tried it more than once, but the it always triggers the same error event.

rtufisi commented 3 months ago

On it

rtufisi commented 3 months ago

Hei @youssefbennour ,

I've tried to reproduce the issue you mentioned.

If a user accepts an organization invitation a successful event is emitted like in the following screenshot:

Screenshot from 2024-08-19 17-52-42

If a user accepts the invitation to join a organization a successful CUSTOM_REQUIRED_ACTION event is emitted as in the given screenshot. At the end of the required action execution to perform a cleanup the invitation is removed and a "CUSTOM_REQUIRED_ACTION_ERROR" event is emitted to signal that the invitation was consumed.

Steps I've used to reproduce this issue:

I'm using quay.io/phasetwo/phasetwo-keycloak:25.0.2(25.0.1 has a UI bug in the events tab as described in the bug: https://github.com/keycloak/keycloak/issues/30306)

How to reproduce: 1 - Login to your keycloak server using admin credentials 2 - Navigate to your realm 3 - Create a new organization 4 - Invite an existing realm member by email 5.- Enable "Invitation" in the realm required action settings

    • Navigate to the account console and login with the invited user credentials.
    • After successful login, accept organization(s) invitation(s)

please let me know your thoughts

youssefbennour commented 3 months ago

@rtufisi I will test it again, I'm not sure whether the succeeding event was emitted in my case or not. Thanks.