diff --git a/docsbuild/content/migrations/group.md b/docsbuild/content/migrations/group.md index cb5308e49..fcc19928a 100644 --- a/docsbuild/content/migrations/group.md +++ b/docsbuild/content/migrations/group.md @@ -65,6 +65,25 @@ Updates a group from keycloak. Fails if the group does not exist. - "1234" ``` +## renameGroup +Renames a group from keycloak. Fails if the group does not exist. + +### Parameters +- realm: String, optional +- name: String, not optional +- newName: String, not optional + +### Example +```yaml + id: test + author: klg71 + changes: + - updateGroup: + realm: master + name: child1 + newName: child2 +``` + ## assignRoleToGroup Assigns a role to a group in keycloak. Fails if the group or the role does not exist. diff --git a/src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/ActionFactory.kt b/src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/ActionFactory.kt index d336edbf1..4bd8744e9 100644 --- a/src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/ActionFactory.kt +++ b/src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/ActionFactory.kt @@ -40,6 +40,7 @@ import de.klg71.keycloakmigration.changeControl.actions.flow.UpdateFlowAction import de.klg71.keycloakmigration.changeControl.actions.group.AddGroupAction import de.klg71.keycloakmigration.changeControl.actions.group.AssignRoleToGroupAction import de.klg71.keycloakmigration.changeControl.actions.group.DeleteGroupAction +import de.klg71.keycloakmigration.changeControl.actions.group.RenameGroupAction import de.klg71.keycloakmigration.changeControl.actions.group.RevokeRoleFromGroupAction import de.klg71.keycloakmigration.changeControl.actions.group.UpdateGroupAction import de.klg71.keycloakmigration.changeControl.actions.identityprovider.AddIdentityProviderAction @@ -128,6 +129,7 @@ class ActionFactory(private val objectMapper: ObjectMapper) { "addGroup" -> objectMapper.readValue(actionJson) "deleteGroup" -> objectMapper.readValue(actionJson) "updateGroup" -> objectMapper.readValue(actionJson) + "renameGroup" -> objectMapper.readValue(actionJson) "assignRoleToGroup" -> objectMapper.readValue(actionJson) "revokeRoleFromGroup" -> objectMapper.readValue(actionJson) diff --git a/src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupAction.kt b/src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupAction.kt new file mode 100644 index 000000000..2444ee2a2 --- /dev/null +++ b/src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupAction.kt @@ -0,0 +1,43 @@ +package de.klg71.keycloakmigration.changeControl.actions.group + +import de.klg71.keycloakmigration.changeControl.actions.Action +import de.klg71.keycloakmigration.changeControl.actions.MigrationException +import de.klg71.keycloakmigration.keycloakapi.existsGroup +import de.klg71.keycloakmigration.keycloakapi.groupByName +import de.klg71.keycloakmigration.keycloakapi.model.Group +import de.klg71.keycloakmigration.keycloakapi.model.UpdateGroup + +class RenameGroupAction( + realm: String? = null, + private val name: String, + private val newName: String) : Action(realm) { + + private lateinit var group: Group + + private fun updateGroup() = UpdateGroup(newName, group.path, group.attributes, + //FIXME + group.access!!, + group.clientRoles, group.realmRoles, group.subGroups + ) + + override fun execute() { + if (!client.existsGroup(name, realm())) { + throw MigrationException("Group with name: $name does not exists in realm: ${realm()}!") + } + + group = client.groupByName(name, realm()).run { + client.group(realm(), id) + } + + client.updateGroup(updateGroup(), realm(), group.id) + } + + override fun undo() { + client.updateGroup(UpdateGroup(group.name, group.path, group.attributes, group.access!!, + group.clientRoles, group.realmRoles, group.subGroups), + realm(), group.id) + } + + override fun name() = "RenameGroup $name to $newName" + +} diff --git a/src/test/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupIntegTest.kt b/src/test/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupIntegTest.kt new file mode 100644 index 000000000..8b9349073 --- /dev/null +++ b/src/test/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupIntegTest.kt @@ -0,0 +1,44 @@ +package de.klg71.keycloakmigration.changeControl.actions.group + +import de.klg71.keycloakmigration.AbstractIntegrationTest +import de.klg71.keycloakmigration.keycloakapi.KeycloakApiException +import de.klg71.keycloakmigration.keycloakapi.KeycloakClient +import de.klg71.keycloakmigration.keycloakapi.groupByName +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.Test +import org.koin.core.component.inject + +class RenameGroupIntegTest : AbstractIntegrationTest() { + + val client by inject() + + @Test + fun testRenameGroup() { + AddGroupAction(testRealm, "integrationTest").executeIt() + val createdGroup = client.groupByName("integrationTest", testRealm) + + RenameGroupAction(testRealm, "integrationTest", "newName").executeIt() + + assertThat(client.groupByName("newName", testRealm).id).isEqualTo(createdGroup.id) + assertThatThrownBy { client.groupByName("integrationTest", testRealm) } + .hasMessage("Group with name: integrationTest does not exist in realm: $testRealm!") + .isInstanceOf(KeycloakApiException::class.java) + } + + @Test + fun testUndoRenameGroup() { + AddGroupAction(testRealm, "integrationTest").executeIt() + val createdGroup = client.groupByName("integrationTest", testRealm) + + val renameGroupAction = RenameGroupAction(testRealm, "integrationTest", "newName") + renameGroupAction.executeIt() + + renameGroupAction.undoIt() + + assertThat(client.groupByName("integrationTest", testRealm).id).isEqualTo(createdGroup.id) + assertThatThrownBy { client.groupByName("newName", testRealm) } + .hasMessage("Group with name: newName does not exist in realm: $testRealm!") + .isInstanceOf(KeycloakApiException::class.java) + } +}