8000 Add organization admin crud events by sguilhen · Pull Request #33281 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions js/apps/admin-ui/src/events/ResourceLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { toClientScope } from "../client-scopes/routes/ClientScope";
import { toUser } from "../user/routes/User";
import { toRealmRole } from "../realm-roles/routes/RealmRole";
import { toFlow } from "../authentication/routes/Flow";
import { toEditOrganization } from "../organizations/routes/EditOrganization";

type ResourceLinkProps = {
event: AdminEventRepresentation;
Expand Down Expand Up @@ -42,6 +43,8 @@ const isLinkable = (event: AdminEventRepresentation) => {
event.resourceType === "GROUP_MEMBERSHIP" ||
event.resourceType === "GROUP" ||
event.resourceType === "CLIENT" ||
event.resourceType === "ORGANIZATION" ||
event.resourceType === "ORGANIZATION_MEMBERSHIP" ||
event.resourceType?.startsWith("AUTHORIZATION_RESOURCE") ||
event.resourceType === "CLIENT_SCOPE" ||
event.resourceType === "AUTH_FLOW" ||
Expand Down Expand Up @@ -95,6 +98,14 @@ const createLink = (realm: string, event: AdminEventRepresentation) => {
return toRealmRole({ realm, id, tab: "details" });
}

if (event.resourceType === "ORGANIZATION") {
return toEditOrganization({ realm, id, tab: "settings" });
}

if (event.resourceType === "ORGANIZATION_MEMBERSHIP") {
return toEditOrganization({ realm, id, tab: "members" });
}

return "";
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class Organizations extends Resource<{ realm?: string }> {

public create = this.makeRequest<OrganizationRepresentation, { id: string }>({
method: "POST",
path: "/",
returnResourceIdInLocationHeader: { field: "id" },
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,15 @@ public enum ResourceType {
/**
* The user profile configuration
*/
, USER_PROFILE;
, USER_PROFILE

/**
*
*/
, ORGANIZATION

/**
*
*/
, ORGANIZATION_MEMBERSHIP;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.keycloak.email.EmailException;
import org.keycloak.email.EmailTemplateProvider;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.OrganizationModel;
Expand Down Expand Up @@ -59,7 +60,7 @@ public OrganizationInvitationResource(KeycloakSession session, OrganizationModel
this.session = session;
this.realm = session.getContext().getRealm();
this.organization = organization;
this.adminEvent = adminEvent;
this.adminEvent = adminEvent.resource(ResourceType.ORGANIZATION_MEMBERSHIP);
this.tokenExpiration = getTokenExpiration();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.resteasy.reactive.NoCache;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelException;
import org.keycloak.models.OrganizationModel;
Expand Down Expand Up @@ -79,7 +81,7 @@ public OrganizationMemberResource(KeycloakSession session, OrganizationModel org
this.realm = session.getContext().getRealm();
this.provider = session.getProvider(OrganizationProvider.class);
this.organization = organization;
this.adminEvent = adminEvent;
this.adminEvent = adminEvent.resource(ResourceType.ORGANIZATION_MEMBERSHIP);
}

@POST
Expand All @@ -97,6 +99,10 @@ public Response addMember(String id) {

try {
if (provider.addMember(organization, user)) {
adminEvent.operation(OperationType.CREATE).resource(ResourceType.ORGANIZATION_MEMBERSHIP)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Degoya-Studio - please create a new issue for this and reference it here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or update the issue you referenced accordingly.

.representation(ModelToRepresentation.toRepresentation(organization))
.resourcePath(session.getContext().getUri())
.success();
return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(user.getId()).build()).build();
}
} catch (ModelException me) {
Expand Down Expand Up @@ -171,6 +177,10 @@ public Response delete(@PathParam("id") String id) {
UserModel member = getMember(id);

if (provider.removeMember(organization, member)) {
adminEvent.operation(OperationType.DELETE).resource(ResourceType.ORGANIZATION_MEMBERSHIP)
.representation(ModelToRepresentation.toRepresentation(organization))
.resourcePath(session.getContext().getUri())
.success();
return Response.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.ext.Provider;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.extensions.Extension;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.resteasy.reactive.NoCache;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelValidationException;
import org.keycloak.models.OrganizationModel;
Expand Down Expand Up @@ -59,7 +62,7 @@ public OrganizationResource(KeycloakSession session, OrganizationModel organizat
this.session = session;
this.provider = session == null ? null : session.getProvider(OrganizationProvider.class);
this.organization = organization;
this.adminEvent = adminEvent;
this.adminEvent = adminEvent.resource(ResourceType.ORGANIZATION);
}

@GET
Expand All @@ -75,8 +78,13 @@ public OrganizationRepresentation get() {
@Tag(name = KeycloakOpenAPI.Admin.Tags.ORGANIZATIONS)
@Operation(summary = "Deletes the organization")
public Response delete() {
provider.remove(organization);
return Response.noContent().build();
boolean removed = provider.remove(organization);
if (removed) {
adminEvent.operation(OperationType.DELETE).resourcePath(session.getContext().getUri()).success();
return Response.noContent().build();
} else {
throw ErrorResponse.error("organization couldn't be deleted", Status.BAD_REQUEST);
}
}

@PUT
Expand All @@ -86,6 +94,7 @@ public Response delete() {
public Response update(OrganizationRepresentation organizationRep) {
try {
RepresentationToModel.toModel(organizationRep, organization);
adminEvent.operation(OperationType.UPDATE).resourcePath(session.getContext().getUri()).representation(organizationRep).success();
return Response.noContent().build();
} catch (ModelValidationException mve) {
throw ErrorResponse.error(mve.getMessage(), Response.Status.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
import org.eclipse.microprofile.openapi.annotations.extensions.Extension;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.NoCache;
A59C import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.ModelValidationException;
Expand All @@ -64,6 +67,8 @@ public class OrganizationsResource {
private final AdminPermissionEvaluator auth;
private final AdminEventBuilder adminEvent;

private static final Logger logger = Logger.getLogger(OrganizationsResource.class);

public OrganizationsResource() {
// needed for registering to the JAX-RS stack
this(null, null, null);
Expand All @@ -73,7 +78,7 @@ public OrganizationsResource(KeycloakSession session, AdminPermissionEvaluator a
this.session = session;
this.provider = session == null ? null : session.getProvider(OrganizationProvider.class);
this.auth = auth;
this.adminEvent = adminEvent;
this.adminEvent = adminEvent.resource(ResourceType.ORGANIZATION);
}

/**
Expand All @@ -99,6 +104,8 @@ public Response create(OrganizationRepresentation organization) {
try {
OrganizationModel model = provider.create(organization.getName(), organization.getAlias());
RepresentationToModel.toModel(organization, model);
organization.setId(model.getId());
adminEvent.operation(OperationType.CREATE).resourcePath(session.getContext().getUri(), model.getId()).representation(organization).success();
return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(model.getId()).build()).build();
} catch (ModelValidationException mve) {
throw ErrorResponse.error(mve.getMessage(), Response.Status.BAD_REQUEST);
Expand Down
0