-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HAI-1527 Audit logging for permission updates
- Loading branch information
Showing
6 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import assertk.assertThat | |
import assertk.assertions.containsExactly | ||
import assertk.assertions.containsExactlyInAnyOrder | ||
import assertk.assertions.each | ||
import assertk.assertions.first | ||
import assertk.assertions.hasClass | ||
import assertk.assertions.hasSize | ||
import assertk.assertions.isEmpty | ||
|
@@ -23,7 +24,12 @@ import fi.hel.haitaton.hanke.factory.HankeFactory | |
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withGeneratedOmistaja | ||
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withYhteystiedot | ||
import fi.hel.haitaton.hanke.factory.HankeYhteystietoFactory | ||
import fi.hel.haitaton.hanke.logging.AuditLogRepository | ||
import fi.hel.haitaton.hanke.logging.ObjectType | ||
import fi.hel.haitaton.hanke.logging.Operation | ||
import fi.hel.haitaton.hanke.logging.UserRole | ||
import fi.hel.haitaton.hanke.test.Asserts.isRecent | ||
import fi.hel.haitaton.hanke.toChangeLogJsonString | ||
import java.time.OffsetDateTime | ||
import java.util.UUID | ||
import org.junit.jupiter.api.Nested | ||
|
@@ -51,6 +57,7 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
@Autowired private lateinit var hankeKayttajaRepository: HankeKayttajaRepository | ||
@Autowired private lateinit var permissionRepository: PermissionRepository | ||
@Autowired private lateinit var roleRepository: RoleRepository | ||
@Autowired private lateinit var auditLogRepository: AuditLogRepository | ||
|
||
@Autowired private lateinit var permissionService: PermissionService | ||
|
||
|
@@ -391,6 +398,36 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
} | ||
} | ||
|
||
@Test | ||
fun `Writes permission update to audit log`() { | ||
val hanke = hankeFactory.save() | ||
val kayttaja = saveUserAndPermission(hanke.id!!) | ||
val updates = mapOf(kayttaja.id to Role.HANKEMUOKKAUS) | ||
auditLogRepository.deleteAll() | ||
|
||
hankeKayttajaService.updatePermissions(hanke, updates, false, USERNAME) | ||
|
||
val logs = auditLogRepository.findAll() | ||
assertThat(logs).hasSize(1) | ||
assertThat(logs) | ||
.first() | ||
.transform { it.message.auditEvent } | ||
.all { | ||
transform { it.operation }.isEqualTo(Operation.UPDATE) | ||
transform { it.actor.role }.isEqualTo(UserRole.USER) | ||
transform { it.actor.userId }.isEqualTo(USERNAME) | ||
transform { it.target.id }.isEqualTo(kayttaja.permission?.id.toString()) | ||
transform { it.target.type }.isEqualTo(ObjectType.PERMISSION) | ||
val permission = kayttaja.permission!!.toDomain() | ||
transform { it.target.objectBefore } | ||
.isEqualTo(permission.toChangeLogJsonString()) | ||
transform { it.target.objectAfter } | ||
.isEqualTo( | ||
permission.copy(role = Role.HANKEMUOKKAUS).toChangeLogJsonString() | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Updates role to tunniste if permission doesn't exist`() { | ||
val hanke = hankeFactory.save() | ||
|
@@ -406,6 +443,34 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
} | ||
} | ||
|
||
@Test | ||
fun `Writes tunniste update to audit log`() { | ||
val hanke = hankeFactory.save() | ||
val kayttaja = saveUserAndToken(hanke.id!!, "Toinen Tohelo", "[email protected]") | ||
val updates = mapOf(kayttaja.id to Role.HANKEMUOKKAUS) | ||
auditLogRepository.deleteAll() | ||
|
||
hankeKayttajaService.updatePermissions(hanke, updates, false, USERNAME) | ||
|
||
val logs = auditLogRepository.findAll() | ||
assertThat(logs).hasSize(1) | ||
assertThat(logs) | ||
.first() | ||
.transform { it.message.auditEvent } | ||
.all { | ||
transform { it.operation }.isEqualTo(Operation.UPDATE) | ||
transform { it.actor.role }.isEqualTo(UserRole.USER) | ||
transform { it.actor.userId }.isEqualTo(USERNAME) | ||
transform { it.target.id }.isEqualTo(kayttaja.kayttajaTunniste?.id.toString()) | ||
transform { it.target.type }.isEqualTo(ObjectType.KAYTTAJA_TUNNISTE) | ||
val tunniste = | ||
kayttaja.kayttajaTunniste!!.toDomain().copy(hankeKayttajaId = kayttaja.id) | ||
transform { it.target.objectBefore }.isEqualTo(tunniste.toChangeLogJsonString()) | ||
transform { it.target.objectAfter } | ||
.isEqualTo(tunniste.copy(role = Role.HANKEMUOKKAUS).toChangeLogJsonString()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Updates role to only permission if both permission and tunniste exist`() { | ||
val hanke = hankeFactory.save() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...anke-service/src/main/kotlin/fi/hel/haitaton/hanke/logging/HankeKayttajaLoggingService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fi.hel.haitaton.hanke.logging | ||
|
||
import fi.hel.haitaton.hanke.application.Application | ||
import fi.hel.haitaton.hanke.permissions.KayttajaTunniste | ||
import fi.hel.haitaton.hanke.permissions.PermissionEntity | ||
import fi.hel.haitaton.hanke.permissions.Role | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Propagation | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class HankeKayttajaLoggingService(private val auditLogService: AuditLogService) { | ||
|
||
@Transactional(propagation = Propagation.MANDATORY) | ||
fun logCreate(savedApplication: Application, userId: String) { | ||
auditLogService.create( | ||
AuditLogService.createEntry(userId, ObjectType.APPLICATION, savedApplication) | ||
) | ||
} | ||
|
||
@Transactional(propagation = Propagation.MANDATORY) | ||
fun logUpdate(roleBefore: Role, permissionEntityAfter: PermissionEntity, userId: String) { | ||
val permissionAfter = permissionEntityAfter.toDomain() | ||
val permissionBefore = permissionAfter.copy(role = roleBefore) | ||
|
||
AuditLogService.updateEntry( | ||
userId, | ||
ObjectType.PERMISSION, | ||
permissionBefore, | ||
permissionAfter, | ||
) | ||
?.let { auditLogService.create(it) } | ||
} | ||
|
||
@Transactional(propagation = Propagation.MANDATORY) | ||
fun logUpdate( | ||
kayttajaTunnisteBefore: KayttajaTunniste, | ||
kayttajaTunnisteAfter: KayttajaTunniste, | ||
userId: String | ||
) { | ||
AuditLogService.updateEntry( | ||
userId, | ||
ObjectType.KAYTTAJA_TUNNISTE, | ||
kayttajaTunnisteBefore, | ||
kayttajaTunnisteAfter, | ||
) | ||
?.let { auditLogService.create(it) } | ||
} | ||
|
||
@Transactional(propagation = Propagation.MANDATORY) | ||
fun logDelete(applicationBefore: Application, userId: String) { | ||
auditLogService.create( | ||
AuditLogService.deleteEntry(userId, ObjectType.APPLICATION, applicationBefore) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters