From 2eeab2dadec8fbe9a9b674c4351f87cf5bb1e8f4 Mon Sep 17 00:00:00 2001 From: Mikhail Yasnov Date: Thu, 8 Jul 2021 16:51:26 +0300 Subject: [PATCH] Add UserCoreService logout method --- .../auth/core/user/services/UserCoreService.kt | 3 ++- .../core/user/services/UserCoreServiceImpl.kt | 17 +++++++++++++++-- .../auth/core/user/services/dto/UserLogout.kt | 8 ++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/dto/UserLogout.kt 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 ad7ff01..118c8f3 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 @@ -7,7 +7,7 @@ import ru.touchin.auth.core.user.services.dto.GetUserAccount 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 java.util.UUID +import ru.touchin.auth.core.user.services.dto.UserLogout interface UserCoreService { @@ -17,5 +17,6 @@ interface UserCoreService { fun getUserAccount(userAccount: GetUserAccount): UserAccount fun getOrNull(username: String, identifierType: IdentifierType): User? fun login(userLogin: UserLogin): User + fun logout(userLogout: UserLogout) } 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 6ec26e2..c3a29a5 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 @@ -2,7 +2,6 @@ package ru.touchin.auth.core.user.services -import org.springframework.data.repository.findByIdOrNull import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -25,13 +24,14 @@ import ru.touchin.auth.core.user.models.UserAccountEntity import ru.touchin.auth.core.user.models.UserEntity import ru.touchin.auth.core.user.repositories.UserAccountRepository import ru.touchin.auth.core.user.repositories.UserRepository +import ru.touchin.auth.core.user.repositories.findByIdOrThrow import ru.touchin.auth.core.user.repositories.findByUserIdOrThrow import ru.touchin.auth.core.user.repositories.findByUsernameOrThrow import ru.touchin.auth.core.user.services.dto.GetUserAccount 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 java.util.UUID +import ru.touchin.auth.core.user.services.dto.UserLogout @Service class UserCoreServiceImpl( @@ -114,6 +114,19 @@ class UserCoreServiceImpl( return user.toDto(device.toDto()) } + @Transactional + override fun logout(userLogout: UserLogout) { + val device = deviceRepository.findByIdWithLockOrThrow(userLogout.deviceId) + + resetDeviceUsers(device) + + userRepository.findByIdOrThrow(userLogout.userId) + .apply { + devices = hashSetOf() + } + .also(userRepository::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/UserLogout.kt b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/dto/UserLogout.kt new file mode 100644 index 0000000..9226704 --- /dev/null +++ b/auth-core/src/main/kotlin/ru/touchin/auth/core/user/services/dto/UserLogout.kt @@ -0,0 +1,8 @@ +package ru.touchin.auth.core.user.services.dto + +import java.util.UUID + +data class UserLogout( + val deviceId: UUID, + val userId: UUID, +)