Skip to content

Commit

Permalink
PI-1493 Add userId to response (#2362)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Oct 4, 2023
1 parent 9b313b8 commit ca0040b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -39,6 +40,7 @@ internal class UserDetailsIntegrationTest {
.andExpect(status().isOk)
.andExpectJson(
UserDetails(
userId = TEST_USER.id,
username = "test.user",
firstName = "Test",
surname = "User",
Expand All @@ -56,6 +58,7 @@ internal class UserDetailsIntegrationTest {
.andExpectJson(
listOf(
UserDetails(
userId = TEST_USER.id,
username = "test.user",
firstName = "Test",
surname = "User",
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LdapUser>(username)?.toUserDetails()

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ca0040b

Please sign in to comment.