Skip to content

Commit

Permalink
Merge pull request #81 from mfolnovic/issue-80-rename-group
Browse files Browse the repository at this point in the history
#80: implement support for renaming group
  • Loading branch information
klg71 authored Aug 4, 2024
2 parents b3dcbf7 + ad67972 commit 1820132
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docsbuild/content/migrations/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,6 +129,7 @@ class ActionFactory(private val objectMapper: ObjectMapper) {
"addGroup" -> objectMapper.readValue<AddGroupAction>(actionJson)
"deleteGroup" -> objectMapper.readValue<DeleteGroupAction>(actionJson)
"updateGroup" -> objectMapper.readValue<UpdateGroupAction>(actionJson)
"renameGroup" -> objectMapper.readValue<RenameGroupAction>(actionJson)
"assignRoleToGroup" -> objectMapper.readValue<AssignRoleToGroupAction>(actionJson)
"revokeRoleFromGroup" -> objectMapper.readValue<RevokeRoleFromGroupAction>(actionJson)

Expand Down
Original file line number Diff line number Diff line change
@@ -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"

}
Original file line number Diff line number Diff line change
@@ -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<KeycloakClient>()

@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)
}
}

0 comments on commit 1820132

Please sign in to comment.