diff --git a/auth-core/src/main/kotlin/ru/touchin/auth/core/user/repositories/UserAccountRepository.kt b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/repositories/UserAccountRepository.kt index 9205467..c0ec860 100644 --- a/auth-core/src/main/kotlin/ru/touchin/auth/core/user/repositories/UserAccountRepository.kt +++ b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/repositories/UserAccountRepository.kt @@ -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 { } +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) diff --git a/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreService.kt b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreService.kt index 118c8f3..b3c4560 100644 --- a/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreService.kt +++ b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreService.kt @@ -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) } diff --git a/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreServiceImpl.kt b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreServiceImpl.kt index c3a29a5..6c8de43 100644 --- a/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreServiceImpl.kt +++ b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/UserCoreServiceImpl.kt @@ -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) diff --git a/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/dto/UserUpdatePassword.kt b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/dto/UserUpdatePassword.kt new file mode 100644 index 0000000..7ec262f --- /dev/null +++ b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/dto/UserUpdatePassword.kt @@ -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? +)