refactor: AccountServiceImpl.kt

This commit is contained in:
SOR
2026-07-27 12:30:58 +08:00
parent 138024b517
commit 531659a891
@@ -10,104 +10,136 @@ import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import javax.security.auth.login.AccountNotFoundException
@Service @Service
class AccountServiceImpl : ServiceImpl<AccountMapper, Account>(), AccountService { class AccountServiceImpl : ServiceImpl<AccountMapper, Account>(), AccountService {
val logger = LoggerFactory.getLogger(this::class.java)!! private val logger = LoggerFactory.getLogger(this::class.java)
override fun findByUsername(
username: String
): Mono<UserDetails> {
override fun findByUsername(username: String): Mono<UserDetails> {
return findAccountByName(username) return findAccountByName(username)
.switchIfEmpty(Mono.error(UsernameNotFoundException("Account with name $username not found"))) .map {
.map { account -> User.withUsername(it.username)
User.withUsername(username) .password(it.password)
.password(account.password) .roles(it.role)
.roles(account.role)
.build() .build()
} }
.onErrorMap(
AccountNotFoundException::class.java
) {
UsernameNotFoundException(
"User not found"
)
}
} }
override fun findAccountByName(username: String): Mono<Account> = override fun findAccountByName(username: String): Mono<Account> =
findAccount("name", username) { findAccount("username") {
ktQuery() ktQuery()
.eq(Account::username, username) .eq(Account::username, username)
.one() .one()
} }
override fun findAccountByEmail(email: String): Mono<Account> = override fun findAccountByEmail(email: String): Mono<Account> =
findAccount("email", email) { findAccount("email") {
ktQuery() ktQuery()
.eq(Account::email, email) .eq(Account::email, email)
.one() .one()
} }
private fun <T : Any> blockingCall(
block: () -> T?
): Mono<T> =
Mono.fromCallable(block)
.subscribeOn(Schedulers.boundedElastic())
.flatMap {
Mono.just(it)
}
private fun findAccount( private fun findAccount(
fieldName: String, fieldName: String,
value: String,
query: () -> Account? query: () -> Account?
): Mono<Account> = ): Mono<Account> {
Mono.fromCallable { return blockingCall {
logger.info("Find account with {} {}", fieldName, value) logger.info(
"Searching account by {}",
fieldName
)
query() query()
}.switchIfEmpty( }.switchIfEmpty(
Mono.error( Mono.error(
UsernameNotFoundException("Account with $fieldName $value not found") AccountNotFoundException()
) )
) )
}
override fun findAccountByNameOrEmail(text: String): Mono<Account> = override fun findAccountByNameOrEmail(
findAccountByEmail(text).onErrorResume( text: String
UsernameNotFoundException::class.java ): Mono<Account> {
) { return blockingCall {
findAccountByName(text) logger.info(
} "Searching account by identifier"
)
ktQuery()
.and {
it.eq(Account::email, text)
.or()
.eq(Account::username, text)
}
.one()
}.switchIfEmpty(
Mono.error(AccountNotFoundException())
)
}
override fun existAccountByName(username: String): Mono<Boolean> = override fun existAccountByName(
Mono.fromCallable { username: String
logger.info("Determine if name {} exists", username) ): Mono<Boolean> {
return blockingCall {
ktQuery() ktQuery()
.eq(Account::username, username) .eq(Account::username, username)
.exists() .exists()
}
override fun existAccountByEmail(email: String): Mono<Boolean> = }
Mono.fromCallable { }
logger.info("Determine if email {} exists", email)
override fun existAccountByEmail(
email: String
): Mono<Boolean> {
return blockingCall {
ktQuery() ktQuery()
.eq(Account::email, email) .eq(Account::email, email)
.exists() .exists()
} }
}
override fun resetPasswordByEmail( override fun resetPasswordByEmail(
email: String, email: String,
encodedPassword: String encodedPassword: String
): Mono<Void> = ): Mono<Void> {
existAccountByEmail(email) return blockingCall {
.flatMap { exists -> ktUpdate()
if (!exists) { .eq(Account::email, email)
return@flatMap Mono.error( .set(
UsernameNotFoundException( Account::password,
"Account with email $email not found" encodedPassword
) )
) .update()
} }
.flatMap { updated ->
Mono.fromCallable { if (updated) {
ktUpdate()
.eq(Account::email, email)
.set(Account::password, encodedPassword)
.update()
}
}
.flatMap { success ->
if (success) {
Mono.empty() Mono.empty()
} else { } else {
Mono.error( Mono.error(
IllegalStateException( AccountNotFoundException()
"Failed to reset password"
)
) )
} }
} }
}
} }