mirror of
https://github.com/gotson/komga.git
synced 2025-12-29 11:52:56 +01:00
refactor: make KomgaUser.roles a lazy property
This commit is contained in:
parent
ae43e72f59
commit
cf6d26196f
4 changed files with 13 additions and 11 deletions
|
|
@ -29,12 +29,14 @@ data class KomgaUser(
|
|||
override val lastModifiedDate: LocalDateTime = createdDate,
|
||||
) : Auditable, Serializable {
|
||||
|
||||
fun roles(): Set<String> {
|
||||
val roles = mutableSetOf(ROLE_USER)
|
||||
if (roleAdmin) roles.add(ROLE_ADMIN)
|
||||
if (roleFileDownload) roles.add(ROLE_FILE_DOWNLOAD)
|
||||
if (rolePageStreaming) roles.add(ROLE_PAGE_STREAMING)
|
||||
return roles
|
||||
@delegate:Transient
|
||||
val roles: Set<String> by lazy {
|
||||
buildSet {
|
||||
add(ROLE_USER)
|
||||
if (roleAdmin) add(ROLE_ADMIN)
|
||||
if (roleFileDownload) add(ROLE_FILE_DOWNLOAD)
|
||||
if (rolePageStreaming) add(ROLE_PAGE_STREAMING)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class KomgaUserLifecycle(
|
|||
logger.info { "Update user: $toUpdate" }
|
||||
userRepository.update(toUpdate)
|
||||
|
||||
val expireSessions = existing.roles() != user.roles() ||
|
||||
val expireSessions = existing.roles != user.roles ||
|
||||
existing.restrictions != user.restrictions ||
|
||||
existing.sharedAllLibraries != user.sharedAllLibraries ||
|
||||
existing.sharedLibrariesIds != user.sharedLibrariesIds
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class KomgaPrincipal(
|
|||
) : UserDetails, OAuth2User, OidcUser {
|
||||
|
||||
override fun getAuthorities(): MutableCollection<out GrantedAuthority> =
|
||||
user.roles()
|
||||
user.roles
|
||||
.map { SimpleGrantedAuthority("ROLE_$it") }
|
||||
.toMutableSet()
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ fun KomgaUser.toDto() =
|
|||
UserDto(
|
||||
id = id,
|
||||
email = email,
|
||||
roles = roles().toList(),
|
||||
roles = roles.toList(),
|
||||
)
|
||||
|
||||
@Deprecated("Deprecated since 0.153.0. Use toDtoV2() instead")
|
||||
|
|
@ -52,7 +52,7 @@ fun KomgaUser.toDtoV2() =
|
|||
UserDtoV2(
|
||||
id = id,
|
||||
email = email,
|
||||
roles = roles(),
|
||||
roles = roles,
|
||||
sharedAllLibraries = sharedAllLibraries,
|
||||
sharedLibrariesIds = sharedLibrariesIds,
|
||||
labelsAllow = restrictions.labelsAllow,
|
||||
|
|
@ -81,7 +81,7 @@ fun KomgaUser.toWithSharedLibrariesDto() =
|
|||
UserWithSharedLibrariesDto(
|
||||
id = id,
|
||||
email = email,
|
||||
roles = roles().toList(),
|
||||
roles = roles.toList(),
|
||||
sharedAllLibraries = sharedAllLibraries,
|
||||
sharedLibraries = sharedLibrariesIds.map { SharedLibraryDto(it) },
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue