Skip to content

Commit

Permalink
[BACK-2779] Add endpoint that unlinks a single federated user. (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
lostlevels authored Jul 1, 2024
1 parent 05d09c1 commit 36d5ae0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ themes/base
# Environment
.env
.envrc

# OSX files
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import org.keycloak.representations.idm.UserRepresentation;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
Expand Down Expand Up @@ -46,6 +50,23 @@ public Response getUsersById(@QueryParam("ids") String ids) {
return Response.status(Response.Status.OK).entity(representations).build();
}

@POST
@Path("unlink-federated-user/{userId}")
public Response unlinkFederatedUser(@PathParam("userId") final String userId) {
auth.users().canManage();

UserModel user = session.users().getUserById(session.getContext().getRealm(), userId);
if (user == null) {
throw new NotFoundException("User not found");
}
if (user.getFederationLink() == null) {
throw new BadRequestException("User is not a federated user");
}
user.setFederationLink(null);

return Response.status(Response.Status.NO_CONTENT).build();
}

private UserRepresentation toRepresentation(UserModel user, RealmModel realm) {
UserRepresentation representation = ModelToRepresentation.toRepresentation(session, realm, user);
representation.setRealmRoles(getRoles(user));
Expand Down

0 comments on commit 36d5ae0

Please sign in to comment.