mirror of
https://github.com/gotson/komga.git
synced 2025-01-09 04:08:00 +08:00
feat(api): get latest activity by user
This commit is contained in:
parent
622f725896
commit
b118959775
@ -9,6 +9,7 @@ import java.time.LocalDateTime
|
||||
interface AuthenticationActivityRepository {
|
||||
fun findAll(pageable: Pageable): Page<AuthenticationActivity>
|
||||
fun findAllByUser(user: KomgaUser, pageable: Pageable): Page<AuthenticationActivity>
|
||||
fun findMostRecentByUser(user: KomgaUser): AuthenticationActivity?
|
||||
|
||||
fun insert(activity: AuthenticationActivity)
|
||||
|
||||
|
@ -43,6 +43,15 @@ class AuthenticationActivityDao(
|
||||
return findAll(conditions, pageable)
|
||||
}
|
||||
|
||||
override fun findMostRecentByUser(user: KomgaUser): AuthenticationActivity? =
|
||||
dsl.selectFrom(aa)
|
||||
.where(aa.USER_ID.eq(user.id))
|
||||
.or(aa.EMAIL.eq(user.email))
|
||||
.orderBy(aa.DATE_TIME.desc())
|
||||
.limit(1)
|
||||
.fetchOne()
|
||||
?.toDomain()
|
||||
|
||||
private fun findAll(conditions: Condition, pageable: Pageable): PageImpl<AuthenticationActivity> {
|
||||
val count = dsl.fetchCount(aa, conditions)
|
||||
|
||||
|
@ -31,7 +31,6 @@ import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PatchMapping
|
||||
@ -198,4 +197,15 @@ class UserController(
|
||||
|
||||
return authenticationActivityRepository.findAll(pageRequest).map { it.toDto() }
|
||||
}
|
||||
|
||||
@GetMapping("{id}/authentication-activity/latest")
|
||||
@PreAuthorize("hasRole('$ROLE_ADMIN') or #principal.user.id == #id")
|
||||
fun getLatestAuthenticationActivityForUser(
|
||||
@PathVariable id: String,
|
||||
@AuthenticationPrincipal principal: KomgaPrincipal,
|
||||
): AuthenticationActivityDto =
|
||||
userRepository.findByIdOrNull(id)?.let { user ->
|
||||
authenticationActivityRepository.findMostRecentByUser(user)?.toDto()
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
|
||||
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user