From 89c1395623098af04de9f1eadb3afc96af86922f Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Mon, 4 Nov 2024 16:30:05 -0300 Subject: [PATCH] Adding organization membership provider events Signed-off-by: Pedro Igor --- .../jpa/JpaOrganizationProvider.java | 3 ++ .../keycloak/models/OrganizationModel.java | 50 +++++++++++++++++++ .../resources/admin/UserResource.java | 16 +++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/organization/jpa/JpaOrganizationProvider.java b/model/jpa/src/main/java/org/keycloak/organization/jpa/JpaOrganizationProvider.java index 926f6c7b9c07..5fbe98098658 100644 --- a/model/jpa/src/main/java/org/keycloak/organization/jpa/JpaOrganizationProvider.java +++ b/model/jpa/src/main/java/org/keycloak/organization/jpa/JpaOrganizationProvider.java @@ -191,6 +191,7 @@ private boolean addMember(OrganizationModel organization, UserModel user, Member } user.joinGroup(group, metadata); + OrganizationModel.OrganizationMemberJoinEvent.fire(organization, user, session); } finally { if (current == null) { session.getContext().setOrganization(null); @@ -430,6 +431,8 @@ public boolean removeMember(OrganizationModel organization, UserModel member) { } } + OrganizationModel.OrganizationMemberLeaveEvent.fire(organization, member, session); + return true; } diff --git a/server-spi/src/main/java/org/keycloak/models/OrganizationModel.java b/server-spi/src/main/java/org/keycloak/models/OrganizationModel.java index 1765c3e2c6a1..4899239144ad 100644 --- a/server-spi/src/main/java/org/keycloak/models/OrganizationModel.java +++ b/server-spi/src/main/java/org/keycloak/models/OrganizationModel.java @@ -22,6 +22,8 @@ import java.util.Set; import java.util.stream.Stream; +import org.keycloak.provider.ProviderEvent; + public interface OrganizationModel { String ORGANIZATION_ATTRIBUTE = "kc.org"; @@ -47,6 +49,54 @@ public String getKey() { } } + interface OrganizationMembershipEvent extends ProviderEvent { + OrganizationModel getOrganization(); + UserModel getUser(); + KeycloakSession getSession(); + } + + interface OrganizationMemberJoinEvent extends OrganizationMembershipEvent { + static void fire(OrganizationModel organization, UserModel user, KeycloakSession session) { + session.getKeycloakSessionFactory().publish(new OrganizationModel.OrganizationMemberJoinEvent() { + @Override + public UserModel getUser() { + return user; + } + + @Override + public OrganizationModel getOrganization() { + return organization; + } + + @Override + public KeycloakSession getSession() { + return session; + } + }); + } + } + + interface OrganizationMemberLeaveEvent extends OrganizationMembershipEvent { + static void fire(OrganizationModel organization, UserModel user, KeycloakSession session) { + session.getKeycloakSessionFactory().publish(new OrganizationModel.OrganizationMemberLeaveEvent() { + @Override + public UserModel getUser() { + return user; + } + + @Override + public OrganizationModel getOrganization() { + return organization; + } + + @Override + public KeycloakSession getSession() { + return session; + } + }); + } + } + String getId(); void setName(String name); diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java index 16921dac7fb1..658c79e7a3b6 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java @@ -1030,7 +1030,13 @@ public void removeMembership(@PathParam("groupId") String groupId) { try { if (user.isMemberOf(group)){ user.leaveGroup(group); - adminEvent.operation(OperationType.DELETE).resource(ResourceType.GROUP_MEMBERSHIP).representation(ModelToRepresentation.toRepresentation(group, true)).resourcePath(session.getContext().getUri()).success(); + adminEvent.operation(OperationType.DELETE) + .resource(ResourceType.GROUP_MEMBERSHIP) + .representation(ModelToRepresentation.toRepresentation(group, true)) + .resourcePath(session.getContext().getUri()) + .detail(UserModel.USERNAME, user.getUsername()) + .detail(UserModel.EMAIL, user.getEmail()) + .success(); } } catch (ModelIllegalStateException e) { logger.error(e.getMessage(), e); @@ -1057,7 +1063,13 @@ public void joinGroup(@PathParam("groupId") String groupId) { if (!RoleUtils.isDirectMember(user.getGroupsStream(),group)){ user.joinGroup(group); - adminEvent.operation(OperationType.CREATE).resource(ResourceType.GROUP_MEMBERSHIP).representation(ModelToRepresentation.toRepresentation(group, true)).resourcePath(session.getContext().getUri()).success(); + adminEvent.operation(OperationType.CREATE) + .resource(ResourceType.GROUP_MEMBERSHIP) + .representation(ModelToRepresentation.toRepresentation(group, true)) + .resourcePath(session.getContext().getUri()) + .detail(UserModel.USERNAME, user.getUsername()) + .detail(UserModel.EMAIL, user.getEmail()) + .success(); } }