added basic edit account
- add link in to edit a user in users.html - change to AccountController - update getCreateAccount for change to AccountForm - add getEditAccount - add edit.html - change to Mapping.kt - update AccountForm toAccountRequest to throw exception if password null - add Account Response toAccountForm - change password and passwordConfirm to be null in AccountForm - add PasswordException - add getAccount to AccountRegistryService - add accountRegistryAccount to WebClientCalls.kt
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package ltd.hlaeja.controller
|
||||
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.dto.Pagination
|
||||
import ltd.hlaeja.exception.PasswordException
|
||||
import ltd.hlaeja.exception.UsernameDuplicateException
|
||||
import ltd.hlaeja.form.AccountForm
|
||||
import ltd.hlaeja.service.AccountRegistryService
|
||||
import ltd.hlaeja.util.toAccountForm
|
||||
import ltd.hlaeja.util.toAccountRequest
|
||||
import org.springframework.stereotype.Controller
|
||||
import org.springframework.ui.Model
|
||||
@@ -24,11 +27,22 @@ class AccountController(
|
||||
const val DEFAULT_SIZE: Int = 25
|
||||
}
|
||||
|
||||
@GetMapping("/edit-{account}")
|
||||
fun getEditAccount(
|
||||
@PathVariable account: UUID,
|
||||
model: Model
|
||||
): Mono<String> = accountRegistryService.getAccount(account)
|
||||
.doOnNext {
|
||||
model.addAttribute("account", account)
|
||||
model.addAttribute("accountForm", it.toAccountForm())
|
||||
}
|
||||
.then(Mono.just("account/edit"))
|
||||
|
||||
@GetMapping("/create")
|
||||
fun getCreateAccount(
|
||||
model: Model,
|
||||
): Mono<String> = Mono.just("account/create")
|
||||
.doOnNext { model.addAttribute("accountForm", AccountForm("", "", "", "", true)) }
|
||||
.doOnNext { model.addAttribute("accountForm", AccountForm("", "")) }
|
||||
|
||||
@PostMapping("/create")
|
||||
fun postCreateAccount(
|
||||
@@ -43,6 +57,7 @@ class AccountController(
|
||||
.onErrorResume { error ->
|
||||
val errorMessage = when (error) {
|
||||
is UsernameDuplicateException -> "Username already exists. Please choose another."
|
||||
is PasswordException -> error.message
|
||||
else -> "An unexpected error occurred. Please try again later."
|
||||
}
|
||||
model.addAttribute("errorMessage", errorMessage)
|
||||
|
||||
23
src/main/kotlin/ltd/hlaeja/exception/PasswordException.kt
Normal file
23
src/main/kotlin/ltd/hlaeja/exception/PasswordException.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package ltd.hlaeja.exception
|
||||
|
||||
@Suppress("unused")
|
||||
open class PasswordException : HlaejaException {
|
||||
|
||||
constructor() : super()
|
||||
|
||||
constructor(message: String) : super(message)
|
||||
|
||||
constructor(cause: Throwable) : super(cause)
|
||||
|
||||
constructor(
|
||||
message: String,
|
||||
cause: Throwable,
|
||||
) : super(message, cause)
|
||||
|
||||
constructor(
|
||||
message: String,
|
||||
cause: Throwable,
|
||||
enableSuppression: Boolean,
|
||||
writableStackTrace: Boolean,
|
||||
) : super(message, cause, enableSuppression, writableStackTrace)
|
||||
}
|
||||
@@ -2,8 +2,8 @@ package ltd.hlaeja.form
|
||||
|
||||
data class AccountForm(
|
||||
val username: String,
|
||||
val password: CharSequence,
|
||||
val passwordConfirm: CharSequence,
|
||||
val role: String,
|
||||
val enabled: Boolean = false,
|
||||
val password: CharSequence? = null,
|
||||
val passwordConfirm: CharSequence? = null,
|
||||
)
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package ltd.hlaeja.service
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.exception.AccountRegistryException
|
||||
import ltd.hlaeja.library.accountRegistry.Account
|
||||
import ltd.hlaeja.library.accountRegistry.Authentication
|
||||
import ltd.hlaeja.property.AccountRegistryProperty
|
||||
import ltd.hlaeja.util.accountRegistryAccount
|
||||
import ltd.hlaeja.util.accountRegistryAccounts
|
||||
import ltd.hlaeja.util.accountRegistryAuthenticate
|
||||
import ltd.hlaeja.util.accountRegistryCreate
|
||||
@@ -66,4 +68,14 @@ class AccountRegistryService(
|
||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
||||
}
|
||||
}
|
||||
|
||||
fun getAccount(
|
||||
account: UUID,
|
||||
): Mono<Account.Response> = webClient.accountRegistryAccount(account, property)
|
||||
.onErrorResume { error ->
|
||||
when (error) {
|
||||
is ResponseStatusException -> Mono.error(error)
|
||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ltd.hlaeja.util
|
||||
|
||||
import ltd.hlaeja.exception.PasswordException
|
||||
import ltd.hlaeja.form.AccountForm
|
||||
import ltd.hlaeja.library.accountRegistry.Account
|
||||
import ltd.hlaeja.library.accountRegistry.Authentication
|
||||
@@ -12,7 +13,13 @@ fun SpringAuthentication.toAuthenticationRequest(): Authentication.Request = Aut
|
||||
|
||||
fun AccountForm.toAccountRequest(): Account.Request = Account.Request(
|
||||
username = username,
|
||||
password = password,
|
||||
password = password ?: throw PasswordException("Password requirements failed"),
|
||||
enabled = enabled,
|
||||
roles = listOf("ROLE_${role.uppercase()}"),
|
||||
)
|
||||
|
||||
fun Account.Response.toAccountForm(): AccountForm = AccountForm(
|
||||
username = username,
|
||||
enabled = enabled,
|
||||
role = roles.first().removePrefix("ROLE_").lowercase()
|
||||
)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ltd.hlaeja.util
|
||||
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.exception.AccountRegistryException
|
||||
import ltd.hlaeja.exception.UsernameDuplicateException
|
||||
import ltd.hlaeja.library.accountRegistry.Account
|
||||
@@ -14,6 +15,7 @@ import org.springframework.security.authentication.BadCredentialsException
|
||||
import org.springframework.security.authentication.LockedException
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException
|
||||
import org.springframework.web.reactive.function.client.WebClient
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@@ -48,3 +50,12 @@ fun WebClient.accountRegistryCreate(
|
||||
.onStatus(CONFLICT::equals) { throw UsernameDuplicateException() }
|
||||
.onStatus(BAD_REQUEST::equals) { throw AccountRegistryException("Remote service returned 400") }
|
||||
.bodyToMono(Account.Response::class.java)
|
||||
|
||||
fun WebClient.accountRegistryAccount(
|
||||
account: UUID,
|
||||
property: AccountRegistryProperty,
|
||||
): Mono<Account.Response> = get()
|
||||
.uri("${property.url}/account-$account".also(::logCall))
|
||||
.retrieve()
|
||||
.onStatus(NOT_FOUND::equals) { throw ResponseStatusException(NOT_FOUND) }
|
||||
.bodyToMono(Account.Response::class.java)
|
||||
|
||||
Reference in New Issue
Block a user