Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge: (#103) 비밀번호 변경 기능 #117

Merged
merged 5 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package team.comit.simtong.domain.user.dto

/**
*
* 사용자의 비밀번호 변경 요청을 하는 ChangePasswordRequest
*
* @author Chokyunghyeon
* @date 2022/10/14
* @version 1.0.0
**/
data class ChangePasswordRequest(
val password: String,
val newPassword: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package team.comit.simtong.domain.user.usecase

import team.comit.simtong.domain.user.dto.ChangePasswordRequest
import team.comit.simtong.domain.user.exception.DifferentPasswordException
import team.comit.simtong.domain.user.exception.UserNotFoundException
import team.comit.simtong.domain.user.spi.CommandUserPort
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.domain.user.spi.UserSecurityPort
import team.comit.simtong.global.annotation.UseCase

/**
*
* 사용자의 비밀번호 변경을 담당하는 ChangePasswordUseCase
*
* @author Chokyunghyeon
* @date 2022/10/14
* @version 1.0.0
**/
@UseCase
class ChangePasswordUseCase(
private val queryUserPort: QueryUserPort,
private val userSecurityPort: UserSecurityPort,
private val commandUserPort: CommandUserPort
) {

fun execute(request: ChangePasswordRequest) {
val currentUserId = userSecurityPort.getCurrentUserId()
val user = queryUserPort.queryUserById(currentUserId) ?: throw UserNotFoundException.EXCEPTION

if (!userSecurityPort.compare(request.password, user.password)) {
throw DifferentPasswordException.EXCEPTION
}

commandUserPort.save(
user.copy(
password = userSecurityPort.encode(request.newPassword)
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.SecurityFilterChain
import team.comit.simtong.domain.user.model.Authority.ROLE_ADMIN
import team.comit.simtong.domain.user.model.Authority.ROLE_COMMON
import team.comit.simtong.global.filter.FilterConfig
import team.comit.simtong.global.security.token.JwtParser
Expand Down Expand Up @@ -53,7 +54,8 @@ class SecurityConfig(
// commons
.antMatchers(HttpMethod.GET, "/commons/employee-number").permitAll()
.antMatchers(HttpMethod.PUT, "/commons/token/reissue").permitAll()
.antMatchers(HttpMethod.PUT, "/commons/password").permitAll()
.antMatchers(HttpMethod.PUT, "/commons/password/initialization").permitAll()
.antMatchers(HttpMethod.PUT, "/commons/password").hasAnyRole(ROLE_COMMON.role, ROLE_ADMIN.role)
khcho0125 marked this conversation as resolved.
Show resolved Hide resolved

// emails
.antMatchers(HttpMethod.GET, "/emails").permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package team.comit.simtong.common

import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import team.comit.simtong.common.dto.request.WebChangePasswordRequest
import team.comit.simtong.common.dto.request.WebFindEmployeeNumberRequest
import team.comit.simtong.common.dto.request.WebResetPasswordRequest
import team.comit.simtong.common.dto.response.WebFindEmployeeNumberResponse
import team.comit.simtong.domain.auth.dto.TokenResponse
import team.comit.simtong.domain.auth.usecase.ReissueTokenUseCase
import team.comit.simtong.domain.user.dto.ChangePasswordRequest
import team.comit.simtong.domain.user.dto.FindEmployeeNumberRequest
import team.comit.simtong.domain.user.dto.ResetPasswordRequest
import team.comit.simtong.domain.user.usecase.ChangePasswordUseCase
import team.comit.simtong.domain.user.usecase.FindEmployeeNumberUseCase
import team.comit.simtong.domain.user.usecase.ResetPasswordUseCase
import team.comit.simtong.user.dto.request.WebResetPasswordRequest
import javax.validation.Valid

/**
Expand All @@ -26,7 +29,8 @@ import javax.validation.Valid
class WebCommonAdapter(
private val reissueTokenUseCase: ReissueTokenUseCase,
private val findEmployeeNumberUseCase: FindEmployeeNumberUseCase,
private val resetPasswordUseCase: ResetPasswordUseCase
private val resetPasswordUseCase: ResetPasswordUseCase,
private val changePasswordUseCase: ChangePasswordUseCase
) {

@GetMapping("/employee-number")
Expand All @@ -45,7 +49,7 @@ class WebCommonAdapter(
return reissueTokenUseCase.execute(request)
}

@PutMapping("/password")
@PutMapping("/password/initialization")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun resetPassword(@Valid @RequestBody request: WebResetPasswordRequest) {
resetPasswordUseCase.execute(
Expand All @@ -57,4 +61,15 @@ class WebCommonAdapter(
)
}

@PutMapping("/password")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun changePassword(@Valid @RequestBody request: WebChangePasswordRequest) {
changePasswordUseCase.execute(
ChangePasswordRequest(
password = request.password,
newPassword = request.newPassword
)
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package team.comit.simtong.common.dto.request

import org.hibernate.validator.constraints.Length
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Pattern

/**
*
* 비밀번호 변경을 요청하는 WebChangePasswordRequest
*
* @author Chokyunghyeon
* @date 2022/10/14
* @version 1.0.0
**/
data class WebChangePasswordRequest(

@field:NotBlank
val password: String,

/**
* $ , + , - , _ , a ~ z , A ~ Z , 0 ~ 9
**/
@field:NotBlank
@field:Pattern(regexp = """[+\-_$\w]*""")
@field:Length(max = 20, min = 8)
khcho0125 marked this conversation as resolved.
Show resolved Hide resolved
val newPassword: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team.comit.simtong.user.dto.request
package team.comit.simtong.common.dto.request

import org.hibernate.validator.constraints.Length
import javax.validation.constraints.Email
Expand Down