Skip to content

Commit

Permalink
Adding organization membership provider events
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Igor <[email protected]>
  • Loading branch information
pedroigor committed Nov 8, 2024
1 parent e7bb589 commit 89c1395
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -430,6 +431,8 @@ public boolean removeMember(OrganizationModel organization, UserModel member) {
}
}

OrganizationModel.OrganizationMemberLeaveEvent.fire(organization, member, session);

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
}

Expand Down

0 comments on commit 89c1395

Please sign in to comment.