8000 feat: implemented partial import of organization memberships by VonRehberg · Pull Request #46547 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2026 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.keycloak.representations.idm;

/**
* Representation for organization membership used in partial imports.
* This allows importing user-to-organization memberships independently from users or organizations.
*/
public class OrganizationMembershipRepresentation {

private String organizationId;
private String username;
private MembershipType membershipType;

public OrganizationMembershipRepresentation() {
}

public OrganizationMembershipRepresentation(String organizationId, String username, MembershipType membershipType) {
this.organizationId = organizationId;
this.username = username;
this.membershipType = membershipType;
}

public String getOrganizationId() {
return organizationId;
}

public void setOrganizationId(String organizationId) {
this.organizationId = organizationId;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public MembershipType getMembershipType() {
return membershipType;
}

public void setMembershipType(MembershipType membershipType) {
this.membershipType = membershipType;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

OrganizationMembershipRepresentation that = (OrganizationMembershipRepresentation) o;

if (organizationId != null ? !organizationId.equals(that.organizationId) : that.organizationId != null)
return false;
if (username != null ? !username.equals(that.username) : that.username != null) return false;
return membershipType == that.membershipType;
}

@Override
public int hashCode() {
int result = organizationId != null ? organizationId.hashCode() : 0;
result = 31 * result + (username != null ? username.hashCode() : 0);
result = 31 * result + (membershipType != null ? membershipType.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "OrganizationMembershipRepresentation{" +
"organizationId='" + organizationId + '\'' +
", username='" + username + '\'' +
", membershipType=" + membershipType +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum Policy { SKIP, OVERWRITE, FAIL };
protected List<IdentityProviderRepresentation> identityProviders;
protected List<IdentityProviderMapperRepresentation> identityProviderMappers;
protected RolesRepresentation roles;
protected List<OrganizationMembershipRepresentation> organizationMemberships;

public boolean hasUsers() {
return (users != null) && !users.isEmpty();
Expand All @@ -63,6 +64,10 @@ public boolean hasClientRoles() {
return (roles != null) && (roles.getClient() != null) && (!roles.getClient().isEmpty());
}

public boolean hasOrganizationMemberships() {
return (organizationMemberships != null) && !organizationMemberships.isEmpty();
}

public String getIfResourceExists() {
return ifResourceExists;
}
Expand Down Expand Up @@ -123,4 +128,12 @@ public RolesRepresentation getRoles() {
public void setRoles(RolesRepresentation roles) {
this.roles = roles;
}

public List<OrganizationMembershipRepresentation> getOrganizationMemberships() {
return organizationMemberships;
}

public void setOrganizationMemberships(List<OrganizationMembershipRepresentation> organizationMemberships) {
this.organizationMemberships = organizationMemberships;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3311,6 +3311,7 @@ organizationDeleteConfirm=Are you sure you want to permanently delete this organ
organizationDeletedSuccess=The organization has been deleted
orgainzatinoDeleteError=Could not delete client\: {{error}}
createOrganization=Create organization
organizationMemberships=Organization memberships
domain=Domain
organizationDomainHelp=A set of one or more internet domains associated with the organization. The domain is used to map users to an organization based on their email domain and to authenticate them accordingly in the scope of the organization.
addDomain=Add domain
Expand Down
20 changes: 18 additions & 2 deletions js/apps/admin-ui/src/realm-settings/PartialImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export type PartialImportProps = {
// or a single realm object.
type ImportedMultiRealm = RealmRepresentation | RealmRepresentation[];

type NonRoleResource = "users" | "clients" | "groups" | "identityProviders";
type NonRoleResource =
| "users"
| "clients"
| "groups"
| "identityProviders"
| "organizationMemberships";
type RoleResource = "realmRoles" | "clientRoles";
type Resource = NonRoleResource | RoleResource;

Expand All @@ -58,6 +63,7 @@ const INITIAL_RESOURCES: Readonly<ResourceChecked> = {
identityProviders: false,
realmRoles: false,
clientRoles: false,
organizationMemberships: false,
};

export const PartialImportDialog = (props: PartialImportProps) => {
Expand Down Expand Up @@ -166,7 +172,8 @@ export const PartialImportDialog = (props: PartialImportProps) => {
targetHasResource("clients") ||
targetHasResource("identityProviders") ||
targetHasRealmRoles() ||
targetHasClientRoles()
targetHasClientRoles() ||
targetHasResource("organizationMemberships")
);
};

Expand Down Expand Up @@ -248,6 +255,9 @@ export const PartialImportDialog = (props: PartialImportProps) => {
jsonToImport.identityProviders = targetRealm.identityProviders;
if (resourcesToImport["clients"])
jsonToImport.clients = targetRealm.clients;
if (resourcesToImport["organizationMemberships"])
jsonToImport.organizationMemberships =
targetRealm.organizationMemberships;
if (resourcesToImport["realmRoles"] || resourcesToImport["clientRoles"]) {
jsonToImport.roles = targetRealm.roles;
if (!resourcesToImport["realmRoles"]) delete jsonToImport.roles?.realm;
Expand Down Expand Up @@ -360,6 +370,11 @@ export const PartialImportDialog = (props: PartialImportProps) => {
resourceDataListItem("realmRoles", t("realmRoles"))}
{targetHasClientRoles() &&
resourceDataListItem("clientRoles", t("clientRoles"))}
{targetHasResource("organizationMemberships") &&
resourceDataListItem(
"organizationMemberships",
t("organizationMemberships"),
)}
</DataList>
</StackItem>
<StackItem>
Expand Down Expand Up @@ -437,6 +452,7 @@ export const PartialImportDialog = (props: PartialImportProps) => {
["CLIENT_ROLE", t("clientRoles")],
["IDP", t("identityProviders")],
["GROUP", t("groups")],
["ORGANIZATION_MEMBERSHIP", t("organizationMemberships")],
]);

return <span>{typeMap.get(importRecord.resourceType)}</span>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Representation for organization membership used in partial imports.
*/
export default interface OrganizationMembershipRepresentation {
organizationId?: string;
username?: string;
membershipType?: "MANAGED" | "UNMANAGED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type RolesRepresentation from "./rolesRepresentation.js";
import type ClientProfilesRepresentation from "./clientProfilesRepresentation.js";
import type ClientPoliciesRepresentation from "./clientPoliciesRepresentation.js";
import type RoleRepresentation from "./roleRepresentation.js";
import type OrganizationMembershipRepresentation from "./organizationMembershipRepresentation.js";

/**
* https://www.keycloak.org/docs-api/11.0/rest-api/index.html#_realmrepresentation
Expand Down Expand Up @@ -131,6 +132,7 @@ export default interface RealmRepresentation {
users?: UserRepresentation[];
verifyEmail?: boolean;
waitIncrementSeconds?: number;
organizationMemberships?: OrganizationMembershipRepresentation[];
}

export type PartialImportRealmRepresentation = RealmRepresentation & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public enum ResourceType {
USER, GROUP, CLIENT, IDP, IDP_MAPPER, REALM_ROLE, CLIENT_ROLE;
USER, GROUP, CLIENT, IDP, IDP_MAPPER, REALM_ROLE, CLIENT_ROLE, ORGANIZATION_MEMBERSHIP;

/**
* Used to create the admin path in events.
Expand All @@ -39,6 +39,7 @@ public String getPath() {
case IDP_MAPPER: return "mappers";
case REALM_ROLE: return "realms";
case CLIENT_ROLE: return "clients";
case ORGANIZATION_MEMBERSHIP: return "organizations";
default: return "";
}
}
Expand All @@ -53,6 +54,7 @@ public String toString() {
case IDP_MAPPER: return "Identity Provider Mapper";
case REALM_ROLE: return "Realm Role";
case CLIENT_ROLE: return "Client Role";
case ORGANIZATION_MEMBERSHIP: return "Organization Membership";
default: return super.toString();
}
}
Expand Down
Loading
0