Skip to content

Commit

Permalink
PI-1785: Added get user details by id (#3008)
Browse files Browse the repository at this point in the history
* PI-1785: Added get user details by id
  • Loading branch information
pmcphee77 authored Jan 12, 2024
1 parent 4fbd058 commit d438e99
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.ApplicationListener
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
import uk.gov.justice.digital.hmpps.user.AuditUserRepository
import uk.gov.justice.digital.hmpps.entity.UserRepository

@Component
@ConditionalOnProperty("seed.database")
class DataLoader(
private val auditUserRepository: AuditUserRepository
private val userRepository: UserRepository
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
fun saveAuditUser() {
auditUserRepository.save(UserGenerator.AUDIT_USER)
userRepository.save(UserGenerator.USER)
}

@Transactional
override fun onApplicationEvent(are: ApplicationReadyEvent) {
auditUserRepository.save(UserGenerator.TEST_USER)
auditUserRepository.save(UserGenerator.INACTIVE_USER)
userRepository.save(UserGenerator.TEST_USER)
userRepository.save(UserGenerator.INACTIVE_USER)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.user.AuditUser
import uk.gov.justice.digital.hmpps.entity.User

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")
val USER = User(IdGenerator.getAndIncrement(), "HmppsAuthAndDelius")
val TEST_USER = User(IdGenerator.getAndIncrement(), "test.user")
val INACTIVE_USER = User(IdGenerator.getAndIncrement(), "test.user.inactive")
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ internal class UserDetailsIntegrationTest {
)
}

@Test
fun `calling user by id without userId returns 400`() {
mockMvc.perform(get("/user").withToken())
.andExpect(status().isBadRequest)
}

@Test
fun `missing user by id returns 404`() {
mockMvc.perform(get("/user/details/99999").withToken())
.andExpect(status().isNotFound)
}

@Test
fun `get user by id`() {
mockMvc.perform(get("/user/details/" + TEST_USER.id).withToken())
.andExpect(status().isOk)
.andExpectJson(
UserDetails(
userId = TEST_USER.id,
username = "test.user",
firstName = "Test",
surname = "User",
email = "[email protected]",
enabled = true,
roles = listOf("ABC001", "ABC002")
)
)
}

@Test
fun `search by email`() {
mockMvc.perform(get("/[email protected]").withToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class UserController(private val userService: UserService) {
fun getUserDetails(@PathVariable username: String) = userService.getUserDetails(username)
?: throw NotFoundException("User", "username", username)

@GetMapping(value = ["/user/details/{userId}"])
@PreAuthorize("hasAnyRole('ROLE_DELIUS_USER_AUTH', 'ROLE_DELIUS_USER_DETAILS')")
@Operation(description = "Get user details by Id. Requires `ROLE_DELIUS_USER_AUTH` or `ROLE_DELIUS_USER_DETAILS`.")
fun getUserDetailsById(@PathVariable(required = true) userId: Long) = userService.getUserDetailsById(userId)
?: throw NotFoundException("User", "userId", userId)

@GetMapping(value = ["/user"])
@PreAuthorize("hasAnyRole('ROLE_DELIUS_USER_AUTH')")
@Operation(description = "Get users by email. Requires `ROLE_DELIUS_USER_AUTH`.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uk.gov.justice.digital.hmpps.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import org.hibernate.annotations.Immutable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

@Immutable
@Entity
@Table(name = "user_")
class User(
@Id
@Column(name = "user_id")
val id: Long,

@Column(name = "distinguished_name")
val username: String
)

interface UserRepository : JpaRepository<User, Long> {
fun findUserById(userId: Long): User?

@Query("select u from User u where upper(u.username) = upper(:username)")
fun findUserByUsername(username: String): User?
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ import org.springframework.ldap.query.LdapQueryBuilder.query
import org.springframework.ldap.query.SearchScope
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.entity.LdapUser
import uk.gov.justice.digital.hmpps.entity.UserRepository
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,
private val auditUserService: AuditUserService
private val userRepository: UserRepository
) {

fun getUserDetails(username: String) = ldapTemplate.findByUsername<LdapUser>(username)?.toUserDetails()

fun getUserDetailsById(userId: Long) =
userRepository.findUserById(userId)?.let {
ldapTemplate.findByUsername<LdapUser>(it.username)?.toUserDetails(it.id)
}

fun getUsersByEmail(email: String) = ldapTemplate.find(
query()
.base("ou=Users")
Expand All @@ -47,12 +52,11 @@ class UserService(
AttributesMapper { it["cn"].get().toString() }
)

private fun LdapUser.toUserDetails() = UserDetails(
userId = auditUserService.findUser(username)?.id ?: throw NotFoundException(
"User entity",
"username",
username
),
private fun LdapUser.toUserDetails() = userRepository.findUserByUsername(username)?.let { toUserDetails(it.id) }
?: throw NotFoundException("User entity", "username", username)

private fun LdapUser.toUserDetails(userId: Long) = UserDetails(
userId = userId,
username = username,
firstName = forename,
surname = surname,
Expand Down

0 comments on commit d438e99

Please sign in to comment.