Add UserCoreService method for password update

This commit is contained in:
Mikhail Yasnov 2021-07-08 18:01:27 +03:00
parent 2eeab2dade
commit 1f5b98b80d
4 changed files with 33 additions and 0 deletions

View File

@ -4,6 +4,7 @@ package ru.touchin.auth.core.user.repositories
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.findByIdOrNull
import ru.touchin.auth.core.user.dto.enums.IdentifierType
import ru.touchin.auth.core.user.exceptions.UserAccountNotFoundException
import ru.touchin.auth.core.user.models.UserAccountEntity
@ -27,6 +28,11 @@ interface UserAccountRepository: JpaRepository<UserAccountEntity, UUID> {
}
fun UserAccountRepository.findByIdOrThrow(userAccountId: UUID): UserAccountEntity {
return findByIdOrNull(userAccountId)
?: throw UserAccountNotFoundException(userAccountId.toString())
}
fun UserAccountRepository.findByUsernameOrThrow(username: String, identifierType: IdentifierType): UserAccountEntity {
return findByUsername(username, identifierType)
?: throw UserAccountNotFoundException(username)

View File

@ -8,6 +8,7 @@ import ru.touchin.auth.core.user.services.dto.NewAnonymousUser
import ru.touchin.auth.core.user.services.dto.NewUser
import ru.touchin.auth.core.user.services.dto.UserLogin
import ru.touchin.auth.core.user.services.dto.UserLogout
import ru.touchin.auth.core.user.services.dto.UserUpdatePassword
interface UserCoreService {
@ -18,5 +19,6 @@ interface UserCoreService {
fun getOrNull(username: String, identifierType: IdentifierType): User?
fun login(userLogin: UserLogin): User
fun logout(userLogout: UserLogout)
fun updatePassword(update: UserUpdatePassword)
}

View File

@ -32,6 +32,7 @@ import ru.touchin.auth.core.user.services.dto.NewAnonymousUser
import ru.touchin.auth.core.user.services.dto.NewUser
import ru.touchin.auth.core.user.services.dto.UserLogin
import ru.touchin.auth.core.user.services.dto.UserLogout
import ru.touchin.auth.core.user.services.dto.UserUpdatePassword
@Service
class UserCoreServiceImpl(
@ -127,6 +128,21 @@ class UserCoreServiceImpl(
.also(userRepository::save)
}
@Transactional
override fun updatePassword(update: UserUpdatePassword) {
val userAccount = userAccountRepository.findByIdOrThrow(update.userAccountId)
if (userAccount.password != null) {
if (!passwordEncoder.matches(update.oldPassword, userAccount.password!!)) {
throw WrongPasswordException("userAccountId=${update.userAccountId}")
}
}
userAccount.apply {
password = update.newPassword?.let(passwordEncoder::encode)
}.also(userAccountRepository::save)
}
@Transactional(readOnly = true)
override fun get(username: String, identifierType: IdentifierType): User {
return getOrNull(username, identifierType)

View File

@ -0,0 +1,9 @@
package ru.touchin.auth.core.user.services.dto
import java.util.UUID
data class UserUpdatePassword(
val userAccountId: UUID,
val oldPassword: String?,
val newPassword: String?
)