Skip to content

Commit fcc1cca

Browse files
authored
Merge pull request #301 from GSM-MSG/300-password-change-old-password-new-verify-if-the-password-same
🔀 :: 기존 비밀번호와 새 비밀번호와 다른지 검사하는 로직
2 parents d928e4f + 45d7b22 commit fcc1cca

File tree

7 files changed

+29
-19
lines changed

7 files changed

+29
-19
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.msg.gauth.domain.auth.exception
2+
3+
import com.msg.gauth.global.exception.ErrorCode
4+
import com.msg.gauth.global.exception.exceptions.BasicException
5+
6+
class PasswordAndNewPasswordSameException: BasicException(ErrorCode.SAME_PASSWORD_AND_NEW_PASSWORD)

src/main/kotlin/com/msg/gauth/domain/auth/presentation/AuthController.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import com.msg.gauth.domain.auth.presentation.dto.request.SignUpDto
44
import com.msg.gauth.domain.auth.presentation.dto.request.SignInRequestDto
55
import com.msg.gauth.domain.auth.presentation.dto.response.RefreshResponseDto
66
import com.msg.gauth.domain.auth.service.*
7-
import com.msg.gauth.domain.auth.presentation.dto.request.PasswordInitReqDto
8-
import com.msg.gauth.domain.auth.presentation.dto.request.PasswordUpdateRequestDto
7+
import com.msg.gauth.domain.auth.presentation.dto.request.InitPasswordRequestDto
8+
import com.msg.gauth.domain.auth.presentation.dto.request.UpdatePasswordRequestDto
99
import com.msg.gauth.domain.auth.presentation.dto.response.SignInResponseDto
1010
import com.msg.gauth.domain.auth.presentation.dto.response.SignUpImageResDto
1111
import com.msg.gauth.domain.auth.service.InitPasswordService
@@ -58,14 +58,14 @@ class AuthController(
5858

5959

6060
@PatchMapping("/password/initialize")
61-
fun initPassword(@Valid @RequestBody passwordInitReqDto: PasswordInitReqDto): ResponseEntity<Void> {
62-
initPasswordService.execute(passwordInitReqDto)
61+
fun initPassword(@Valid @RequestBody initPasswordRequestDto: InitPasswordRequestDto): ResponseEntity<Void> {
62+
initPasswordService.execute(initPasswordRequestDto)
6363
return ResponseEntity.noContent().build()
6464
}
6565

6666
@PatchMapping("/password")
67-
fun updatePassword(@RequestBody passwordUpdateRequestDto: PasswordUpdateRequestDto): ResponseEntity<Void> {
68-
updatePasswordService.execute(passwordUpdateRequestDto)
67+
fun updatePassword(@RequestBody updatePasswordRequestDto: UpdatePasswordRequestDto): ResponseEntity<Void> {
68+
updatePasswordService.execute(updatePasswordRequestDto)
6969
return ResponseEntity.noContent().build()
7070
}
7171
}

src/main/kotlin/com/msg/gauth/domain/auth/presentation/dto/request/PasswordInitReqDto.kt renamed to src/main/kotlin/com/msg/gauth/domain/auth/presentation/dto/request/InitPasswordRequestDto.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.msg.gauth.domain.user.User
44
import javax.validation.constraints.NotBlank
55
import javax.validation.constraints.Pattern
66

7-
data class PasswordInitReqDto(
7+
data class InitPasswordRequestDto(
88
@field:NotBlank
99
@field:Pattern(regexp = "^[a-zA-Z0-9.]+@gsm.hs.kr$")
1010
val email: String,

src/main/kotlin/com/msg/gauth/domain/auth/presentation/dto/request/PasswordUpdateRequestDto.kt renamed to src/main/kotlin/com/msg/gauth/domain/auth/presentation/dto/request/UpdatePasswordRequestDto.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.msg.gauth.domain.auth.presentation.dto.request
22

3-
import com.msg.gauth.domain.user.User
43
import javax.validation.constraints.NotBlank
54
import javax.validation.constraints.Pattern
65

7-
data class PasswordUpdateRequestDto(
6+
data class UpdatePasswordRequestDto(
87
@field:NotBlank
98
val password: String,
109
@field:NotBlank

src/main/kotlin/com/msg/gauth/domain/auth/service/InitPasswordService.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package com.msg.gauth.domain.auth.service
33
import com.msg.gauth.domain.email.repository.EmailAuthRepository
44
import com.msg.gauth.domain.user.exception.EmailNotVerifiedException
55
import com.msg.gauth.domain.user.exception.UserNotFoundException
6-
import com.msg.gauth.domain.auth.presentation.dto.request.PasswordInitReqDto
7-
import com.msg.gauth.domain.user.User
6+
import com.msg.gauth.domain.auth.presentation.dto.request.InitPasswordRequestDto
87
import com.msg.gauth.domain.user.repository.UserRepository
98
import com.msg.gauth.global.annotation.service.TransactionalService
109
import org.springframework.security.crypto.password.PasswordEncoder
@@ -15,17 +14,17 @@ class InitPasswordService(
1514
private val emailAuthRepository: EmailAuthRepository,
1615
private val passwordEncoder: PasswordEncoder,
1716
){
18-
fun execute(passwordInitReqDto: PasswordInitReqDto){
19-
val emailAuth = emailAuthRepository.findById(passwordInitReqDto.email)
17+
fun execute(initPasswordRequestDto: InitPasswordRequestDto){
18+
val emailAuth = emailAuthRepository.findById(initPasswordRequestDto.email)
2019
.orElseThrow { throw EmailNotVerifiedException() }
2120

2221
if(!emailAuth.authentication)
2322
throw EmailNotVerifiedException()
2423

25-
val user = userRepository.findByEmail(passwordInitReqDto.email)
24+
val user = userRepository.findByEmail(initPasswordRequestDto.email)
2625
?: throw UserNotFoundException()
2726

28-
userRepository.save(passwordInitReqDto.toEntity(user, passwordEncoder.encode(passwordInitReqDto.newPassword)))
27+
userRepository.save(initPasswordRequestDto.toEntity(user, passwordEncoder.encode(initPasswordRequestDto.newPassword)))
2928
emailAuthRepository.delete(emailAuth)
3029
}
3130
}

src/main/kotlin/com/msg/gauth/domain/auth/service/UpdatePasswordService.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.msg.gauth.domain.auth.service
22

3+
import com.msg.gauth.domain.auth.exception.PasswordAndNewPasswordSameException
34
import com.msg.gauth.domain.auth.exception.PasswordMismatchException
4-
import com.msg.gauth.domain.auth.presentation.dto.request.PasswordUpdateRequestDto
5+
import com.msg.gauth.domain.auth.presentation.dto.request.UpdatePasswordRequestDto
56
import com.msg.gauth.domain.user.User
67
import com.msg.gauth.domain.user.repository.UserRepository
78
import com.msg.gauth.domain.user.util.UserUtil
@@ -15,14 +16,18 @@ class UpdatePasswordService(
1516
private val userRepository: UserRepository
1617
) {
1718

18-
fun execute(passwordUpdateRequestDto: PasswordUpdateRequestDto) {
19+
fun execute(updatePasswordRequestDto: UpdatePasswordRequestDto) {
1920
val currentUser = userUtil.fetchCurrentUser()
2021

21-
if (!passwordEncoder.matches(passwordUpdateRequestDto.password, currentUser.password)) {
22+
if (!passwordEncoder.matches(updatePasswordRequestDto.password, currentUser.password)) {
2223
throw PasswordMismatchException()
2324
}
2425

25-
val newPassword = passwordEncoder.encode(passwordUpdateRequestDto.newPassword)
26+
if (updatePasswordRequestDto.password == updatePasswordRequestDto.newPassword) {
27+
throw PasswordAndNewPasswordSameException()
28+
}
29+
30+
val newPassword = passwordEncoder.encode(updatePasswordRequestDto.newPassword)
2631

2732
val user = User(
2833
id = currentUser.id,

src/main/kotlin/com/msg/gauth/global/exception/ErrorCode.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ enum class ErrorCode(
1010
FILE_EXTENSION_INVALID("파일 확장자가 유효하지 않습니다.", 400),
1111
ALREADY_AUTHENTICATED_MAIL("이미 인증된 메일 입니다.", 400),
1212
INVALID_DELEGATE_USER("유효하지 않은 위임자입니다.", 400),
13+
SAME_PASSWORD_AND_NEW_PASSWORD("기본 비밀번호와 새 비밀번호가 일치합니다.", 400),
1314

1415
AUTH_CODE_EXPIRED("메일 인증이 만료되었습니다.", 401),
1516
UNAUTHORIZED("권한 없음", 401),

0 commit comments

Comments
 (0)