diff --git a/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index f6ea94c9a7..9be1e46bcd 100644 --- a/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -1,6 +1,7 @@ package uk.gov.justice.digital.hmpps.data import jakarta.annotation.PostConstruct +import jakarta.transaction.Transactional import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.context.ApplicationListener @@ -19,7 +20,9 @@ class DataLoader( auditUserRepository.save(UserGenerator.AUDIT_USER) } + @Transactional override fun onApplicationEvent(are: ApplicationReadyEvent) { - // Perform dev/test database setup here, using JPA repositories and generator classes... + auditUserRepository.save(UserGenerator.TEST_USER) + auditUserRepository.save(UserGenerator.INACTIVE_USER) } } diff --git a/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt b/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt index bb06bf1530..5a993c33e4 100644 --- a/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt +++ b/projects/hmpps-auth-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/UserGenerator.kt @@ -4,4 +4,6 @@ import uk.gov.justice.digital.hmpps.user.AuditUser object UserGenerator { val AUDIT_USER = AuditUser(IdGenerator.getAndIncrement(), "HmppsAuthAndDelius") + val TEST_USER = AuditUser(IdGenerator.getAndIncrement(), "test.user") + val INACTIVE_USER = AuditUser(IdGenerator.getAndIncrement(), "test.user.inactive") } diff --git a/projects/hmpps-auth-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserDetailsIntegrationTest.kt b/projects/hmpps-auth-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserDetailsIntegrationTest.kt index 4b345b2662..902ec81e71 100644 --- a/projects/hmpps-auth-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserDetailsIntegrationTest.kt +++ b/projects/hmpps-auth-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserDetailsIntegrationTest.kt @@ -12,6 +12,7 @@ import org.springframework.test.web.servlet.ResultActions import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import uk.gov.justice.digital.hmpps.data.generator.UserGenerator.TEST_USER import uk.gov.justice.digital.hmpps.model.UserDetails import uk.gov.justice.digital.hmpps.security.withOAuth2Token @@ -39,6 +40,7 @@ internal class UserDetailsIntegrationTest { .andExpect(status().isOk) .andExpectJson( UserDetails( + userId = TEST_USER.id, username = "test.user", firstName = "Test", surname = "User", @@ -56,6 +58,7 @@ internal class UserDetailsIntegrationTest { .andExpectJson( listOf( UserDetails( + userId = TEST_USER.id, username = "test.user", firstName = "Test", surname = "User", diff --git a/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/UserDetails.kt b/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/UserDetails.kt index 7a0cd4b9db..344321fc10 100644 --- a/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/UserDetails.kt +++ b/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/model/UserDetails.kt @@ -1,6 +1,7 @@ package uk.gov.justice.digital.hmpps.model data class UserDetails( + val userId: Long, val username: String, val firstName: String, val surname: String, diff --git a/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt b/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt index a0addf7a73..2e33b4cfb5 100644 --- a/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt +++ b/projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt @@ -11,10 +11,14 @@ import uk.gov.justice.digital.hmpps.exception.NotFoundException import uk.gov.justice.digital.hmpps.ldap.byUsername import uk.gov.justice.digital.hmpps.ldap.findByUsername import uk.gov.justice.digital.hmpps.model.UserDetails +import uk.gov.justice.digital.hmpps.user.AuditUserService import javax.naming.Name @Service -class UserService(private val ldapTemplate: LdapTemplate) { +class UserService( + private val ldapTemplate: LdapTemplate, + private val auditUserService: AuditUserService +) { fun getUserDetails(username: String) = ldapTemplate.findByUsername(username)?.toUserDetails() @@ -44,6 +48,7 @@ class UserService(private val ldapTemplate: LdapTemplate) { ) private fun LdapUser.toUserDetails() = UserDetails( + userId = auditUserService.findUser(username)?.id ?: throw NotFoundException("User entity", "username", username), username = username, firstName = forename, surname = surname,