added basic create account

- add link in to creat in users.html
- AccountController
  - add getCreateAccount
  - add postCreateAccount
- add create.html
- add AccountForm to Account Request in Mapping.kt
- add AccountForm
- add addAccount to AccountRegistryService
- add accountRegistryCreate to WebClientCalls.kt
- add UsernameDuplicateException
- add AccountRegistryException
- add HlaejaException
This commit is contained in:
2025-01-27 11:39:04 +01:00
parent 34349653db
commit 8e65de0350
11 changed files with 240 additions and 6 deletions

View File

@@ -35,7 +35,7 @@ class SecurityConfiguration {
.anyExchange().authenticated()
private fun AuthorizeExchangeSpec.adminPaths(): AuthorizeExchangeSpec.Access = pathMatchers(
"/account/**"
"/account/**",
)
private fun AuthorizeExchangeSpec.publicPaths(): AuthorizeExchangeSpec.Access = pathMatchers(

View File

@@ -1,11 +1,16 @@
package ltd.hlaeja.controller
import ltd.hlaeja.dto.Pagination
import ltd.hlaeja.exception.UsernameDuplicateException
import ltd.hlaeja.form.AccountForm
import ltd.hlaeja.service.AccountRegistryService
import ltd.hlaeja.util.toAccountRequest
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import reactor.core.publisher.Mono
@@ -19,6 +24,32 @@ class AccountController(
const val DEFAULT_SIZE: Int = 25
}
@GetMapping("/create")
fun getCreateAccount(
model: Model,
): Mono<String> = Mono.just("account/create")
.doOnNext { model.addAttribute("accountForm", AccountForm("", "", "", "", true)) }
@PostMapping("/create")
fun postCreateAccount(
@ModelAttribute("accountForm") accountForm: AccountForm,
model: Model,
): Mono<String> {
return accountRegistryService.addAccount(accountForm.toAccountRequest())
.map {
model.addAttribute("success", true)
"redirect:/account"
}
.onErrorResume { error ->
val errorMessage = when (error) {
is UsernameDuplicateException -> "Username already exists. Please choose another."
else -> "An unexpected error occurred. Please try again later."
}
model.addAttribute("errorMessage", errorMessage)
Mono.just("account/create")
}
}
@GetMapping
fun getDefaultAccounts(
model: Model,
@@ -49,4 +80,3 @@ class AccountController(
}
.then(Mono.just("account/users"))
}

View File

@@ -0,0 +1,23 @@
package ltd.hlaeja.exception
@Suppress("unused")
open class AccountRegistryException : 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)
}

View File

@@ -0,0 +1,23 @@
package ltd.hlaeja.exception
@Suppress("unused")
open class HlaejaException : RuntimeException {
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)
}

View File

@@ -0,0 +1,23 @@
package ltd.hlaeja.exception
@Suppress("unused")
open class UsernameDuplicateException : AccountRegistryException {
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)
}

View File

@@ -0,0 +1,9 @@
package ltd.hlaeja.form
data class AccountForm(
val username: String,
val password: CharSequence,
val passwordConfirm: CharSequence,
val role: String,
val enabled: Boolean = false,
)

View File

@@ -1,11 +1,13 @@
package ltd.hlaeja.service
import io.github.oshai.kotlinlogging.KotlinLogging
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.accountRegistryAccounts
import ltd.hlaeja.util.accountRegistryAuthenticate
import ltd.hlaeja.util.accountRegistryCreate
import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.security.authentication.AuthenticationServiceException
import org.springframework.security.core.AuthenticationException
@@ -54,4 +56,14 @@ class AccountRegistryService(
size: Int,
): Flux<Account.Response> = webClient.accountRegistryAccounts(page, size, property)
.onErrorResume { error -> Flux.error(ResponseStatusException(BAD_REQUEST, error.message, error)) }
fun addAccount(
request: Account.Request,
): Mono<Account.Response> = webClient.accountRegistryCreate(request, property)
.onErrorResume { error ->
when (error) {
is AccountRegistryException -> Mono.error(error)
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
}
}
}

View File

@@ -1,10 +1,18 @@
package ltd.hlaeja.util
import org.springframework.security.core.Authentication as SpringAuthentication
import ltd.hlaeja.form.AccountForm
import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.library.accountRegistry.Authentication
import org.springframework.security.core.Authentication as SpringAuthentication
fun SpringAuthentication.toAuthenticationRequest(): Authentication.Request = Authentication.Request(
principal as String,
credentials as String,
)
fun AccountForm.toAccountRequest(): Account.Request = Account.Request(
username = username,
password = password,
enabled = enabled,
roles = listOf("ROLE_${role.uppercase()}"),
)

View File

@@ -1,8 +1,12 @@
package ltd.hlaeja.util
import ltd.hlaeja.exception.AccountRegistryException
import ltd.hlaeja.exception.UsernameDuplicateException
import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.library.accountRegistry.Authentication
import ltd.hlaeja.property.AccountRegistryProperty
import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.http.HttpStatus.CONFLICT
import org.springframework.http.HttpStatus.LOCKED
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.http.HttpStatus.UNAUTHORIZED
@@ -28,8 +32,19 @@ fun WebClient.accountRegistryAuthenticate(
fun WebClient.accountRegistryAccounts(
page: Int,
size: Int,
property: AccountRegistryProperty
property: AccountRegistryProperty,
): Flux<Account.Response> = get()
.uri("${property.url}/accounts?page=$page&size=$size".also(::logCall))
.retrieve()
.bodyToFlux(Account.Response::class.java)
fun WebClient.accountRegistryCreate(
request: Account.Request,
property: AccountRegistryProperty,
): Mono<Account.Response> = post()
.uri("${property.url}/account".also(::logCall))
.bodyValue(request)
.retrieve()
.onStatus(CONFLICT::equals) { throw UsernameDuplicateException() }
.onStatus(BAD_REQUEST::equals) { throw AccountRegistryException("Remote service returned 400") }
.bodyToMono(Account.Response::class.java)