From c02688473484bc6ee8185f613fa13c39dbbd7ea7 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Fri, 23 Jun 2023 21:42:13 +0200 Subject: [PATCH] Use a different API method to move groups (#20820) Closes #20615 Co-authored-by: daniel-brannon --- .../cypress/support/util/AdminClient.ts | 2 +- js/apps/admin-ui/src/groups/GroupsModal.tsx | 4 ++- .../src/groups/components/MoveDialog.tsx | 34 +++---------------- .../src/resources/groups.ts | 34 ++++++++++++++++++- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/js/apps/admin-ui/cypress/support/util/AdminClient.ts b/js/apps/admin-ui/cypress/support/util/AdminClient.ts index bde07dbef9..60707027bb 100644 --- a/js/apps/admin-ui/cypress/support/util/AdminClient.ts +++ b/js/apps/admin-ui/cypress/support/util/AdminClient.ts @@ -81,7 +81,7 @@ class AdminClient { if (!parentGroup) { parentGroup = await this.client.groups.create({ name: group }); } else { - parentGroup = await this.client.groups.setOrCreateChild( + parentGroup = await this.client.groups.createChildGroup( { id: parentGroup.id }, { name: group } ); diff --git a/js/apps/admin-ui/src/groups/GroupsModal.tsx b/js/apps/admin-ui/src/groups/GroupsModal.tsx index 509ce1ddce..84f188a07d 100644 --- a/js/apps/admin-ui/src/groups/GroupsModal.tsx +++ b/js/apps/admin-ui/src/groups/GroupsModal.tsx @@ -48,7 +48,9 @@ export const GroupsModal = ({ } else if (rename) { await adminClient.groups.update({ id }, group); } else { - await adminClient.groups.setOrCreateChild({ id }, group); + await (group.id + ? adminClient.groups.updateChildGroup({ id }, group) + : adminClient.groups.createChildGroup({ id }, group)); } refresh(rename ? group : undefined); diff --git a/js/apps/admin-ui/src/groups/components/MoveDialog.tsx b/js/apps/admin-ui/src/groups/components/MoveDialog.tsx index 117e086fe1..ef927c0686 100644 --- a/js/apps/admin-ui/src/groups/components/MoveDialog.tsx +++ b/js/apps/admin-ui/src/groups/components/MoveDialog.tsx @@ -11,39 +11,15 @@ type MoveDialogProps = { refresh: () => void; }; -const moveToRoot = async (source: GroupRepresentation) => { - await adminClient.groups.del({ id: source.id! }); - const { id } = await adminClient.groups.create({ - ...source, - id: undefined, - }); - if (source.subGroups) { - await Promise.all( - source.subGroups.map((s) => - adminClient.groups.setOrCreateChild( - { id: id! }, - { - ...s, - id: undefined, - } - ) - ) - ); - } -}; +const moveToRoot = (source: GroupRepresentation) => + source.id + ? adminClient.groups.updateRoot(source) + : adminClient.groups.create(source); const moveToGroup = async ( source: GroupRepresentation, dest: GroupRepresentation -) => { - try { - await adminClient.groups.setOrCreateChild({ id: dest.id! }, source); - } catch (error: any) { - if (error.response) { - throw error; - } - } -}; +) => adminClient.groups.updateChildGroup({ id: dest.id! }, source); export const MoveDialog = ({ source, onClose, refresh }: MoveDialogProps) => { const { t } = useTranslation("groups"); diff --git a/js/libs/keycloak-admin-client/src/resources/groups.ts b/js/libs/keycloak-admin-client/src/resources/groups.ts index f2de81dece..4587fc2c55 100644 --- a/js/libs/keycloak-admin-client/src/resources/groups.ts +++ b/js/libs/keycloak-admin-client/src/resources/groups.ts @@ -29,6 +29,10 @@ export class Groups extends Resource<{ realm?: string }> { returnResourceIdInLocationHeader: { field: "id" }, }); + public updateRoot = this.makeRequest({ + method: "POST", + }); + /** * Single user */ @@ -67,8 +71,8 @@ export class Groups extends Resource<{ realm?: string }> { /** * Set or create child. * This will just set the parent if it exists. Create it and set the parent if the group doesn’t exist. + * @deprecated Use `createChildGroup` or `updateChildGroup` instead. */ - public setOrCreateChild = this.makeUpdateRequest< { id: string }, GroupRepresentation, @@ -80,6 +84,34 @@ export class Groups extends Resource<{ realm?: string }> { returnResourceIdInLocationHeader: { field: "id" }, }); + /** + * Creates a child group on the specified parent group. If the group already exists, then an error is returned. + */ + public createChildGroup = this.makeUpdateRequest< + { id: string }, + Omit, + { id: string } + >({ + method: "POST", + path: "/{id}/children", + urlParamKeys: ["id"], + returnResourceIdInLocationHeader: { field: "id" }, + }); + + /** + * Updates a child group on the specified parent group. If the group doesn’t exist, then an error is returned. + * Can be used to move a group from one parent to another. + */ + public updateChildGroup = this.makeUpdateRequest< + { id: string }, + GroupRepresentation, + void + >({ + method: "POST", + path: "/{id}/children", + urlParamKeys: ["id"], + }); + /** * Members */