Skip to content

Commit

Permalink
feat(#53): converted user service classes java to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
KimDoubleB committed Jan 22, 2023
1 parent 7ad0d73 commit 0e3c52a
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 268 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package site.archive.service.user

import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import site.archive.common.exception.common.ResourceNotFoundException
import site.archive.common.exception.common.UnauthorizedResourceException
import site.archive.common.exception.user.LoginFailException
import site.archive.common.exception.user.OAuthUserHasNotPasswordException
import site.archive.domain.user.BaseUser
import site.archive.domain.user.PasswordUser
import site.archive.domain.user.PasswordUserRepository
import site.archive.domain.user.UserInfo
import site.archive.domain.user.UserRepository
import site.archive.dto.v1.auth.LoginCommandV1
import site.archive.dto.v1.user.UserPasswordResetRequestDtoV1
import site.archive.infra.mail.MailService

@Service
@Transactional(readOnly = true)
class UserAuthService(
val userRepository: UserRepository,
val passwordUserRepository: PasswordUserRepository,
val encoder: PasswordEncoder,
val mailService: MailService
) {

fun tryLoginAndReturnInfo(command: LoginCommandV1): UserInfo {
val user = verifyPasswordReturnUser(command.email, command.password)
return user.convertToUserInfo()
}

@Transactional
fun updateTemporaryPassword(email: String, temporaryPassword: String) {
val passwordUser = userRepository.findByMailAddress(email)
.map { convertPasswordUser(it) }
.orElseThrow { ResourceNotFoundException("Email") }
passwordUser.updatePassword(encoder.encode(temporaryPassword), true)
mailService.sendTemporaryPassword(email, temporaryPassword)
}

@Transactional
fun resetPassword(userInfo: UserInfo, userPasswordResetRequestDtoV1: UserPasswordResetRequestDtoV1) {
if (userInfo.mailAddress != userPasswordResetRequestDtoV1.email) {
throw UnauthorizedResourceException("ํ•ด๋‹น ์ด๋ฉ”์ผ์— ๋Œ€ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ดˆ๊ธฐํ™” ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.")
}
this.resetPassword(userPasswordResetRequestDtoV1)
}

@Transactional
fun resetPassword(userPasswordResetRequestDtoV1: UserPasswordResetRequestDtoV1) {
val passwordUser = verifyPasswordReturnUser(userPasswordResetRequestDtoV1.email, userPasswordResetRequestDtoV1.currentPassword)
passwordUser.updatePassword(encoder.encode(userPasswordResetRequestDtoV1.newPassword), false)
}

fun isTemporaryPasswordLogin(userId: Long): Boolean {
return passwordUserRepository.findById(userId)
.map(PasswordUser::isCurrentTemporaryPassword)
.orElse(false)
}

private fun verifyPasswordReturnUser(email: String, password: String): PasswordUser {
val user = passwordUserRepository.findByMailAddress(email)
.orElseThrow { ResourceNotFoundException("Email") }
if (!encoder.matches(password, user.password)) {
throw LoginFailException("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค")
}
return user
}

private fun convertPasswordUser(user: BaseUser): PasswordUser {
if (user !is PasswordUser) {
throw OAuthUserHasNotPasswordException()
}
return user
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package site.archive.service.user

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import site.archive.domain.user.UserRepository

@Service
@Transactional(readOnly = true)
class UserProfileImageService(val userRepository: UserRepository) {

@Transactional
fun updateUserProfileImage(userId: Long, profileImageUri: String) {
userRepository.updateUserProfileImage(userId, profileImageUri)
}

}
Loading

0 comments on commit 0e3c52a

Please sign in to comment.