feat: 패스워드 변경 메소드 구현
This commit is contained in:
@@ -4,7 +4,9 @@ import com.ticketing.server.global.dto.repository.AbstractEntity;
|
||||
import com.ticketing.server.global.exception.AlreadyDeletedException;
|
||||
import com.ticketing.server.global.exception.PasswordMismatchException;
|
||||
import com.ticketing.server.global.validator.constraints.Phone;
|
||||
import com.ticketing.server.user.service.dto.ChangePassword;
|
||||
import com.ticketing.server.user.service.dto.DeleteUser;
|
||||
import com.ticketing.server.user.service.dto.PasswordMatches;
|
||||
import java.time.LocalDateTime;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
@@ -61,13 +63,24 @@ public class User extends AbstractEntity {
|
||||
throw new AlreadyDeletedException("이미 탈퇴된 회원 입니다.");
|
||||
}
|
||||
|
||||
if (!deleteUser.passwordEquals(password)) {
|
||||
throw new PasswordMismatchException();
|
||||
}
|
||||
comparePassword(deleteUser);
|
||||
|
||||
isDeleted = true;
|
||||
deletedAt = LocalDateTime.now();
|
||||
return this;
|
||||
}
|
||||
|
||||
public User modifyPassword(ChangePassword changePassword) {
|
||||
comparePassword(changePassword);
|
||||
|
||||
this.password = changePassword.getEncodePassword();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void comparePassword(PasswordMatches passwordMatches) {
|
||||
if (!passwordMatches.passwordMatches(password)) {
|
||||
throw new PasswordMismatchException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.ticketing.server.user.service.dto;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
public class ChangePassword implements PasswordMatches {
|
||||
|
||||
public ChangePassword(String email, String oldPassword, String newPassword, PasswordEncoder passwordEncoder) {
|
||||
this.email = email;
|
||||
this.oldPassword = oldPassword;
|
||||
this.newPassword = newPassword;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
@NotEmpty(message = "{validation.not.empty.email}")
|
||||
@Email(message = "{validation.email}")
|
||||
private String email;
|
||||
|
||||
@NotEmpty(message = "{validation.not.empty.oldpassword}")
|
||||
private String oldPassword;
|
||||
|
||||
@NotEmpty(message = "{validation.not.empty.newpassword}")
|
||||
private String newPassword;
|
||||
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean passwordMatches(String password) {
|
||||
return passwordEncoder.matches(oldPassword, password);
|
||||
}
|
||||
|
||||
public String getEncodePassword() {
|
||||
return passwordEncoder.encode(newPassword);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
public class DeleteUser {
|
||||
public class DeleteUser implements PasswordMatches {
|
||||
|
||||
public DeleteUser(String email, String inputPassword, PasswordEncoder passwordEncoder) {
|
||||
this.email = email;
|
||||
@@ -21,7 +21,8 @@ public class DeleteUser {
|
||||
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
public boolean passwordEquals(String password) {
|
||||
@Override
|
||||
public boolean passwordMatches(String password) {
|
||||
return passwordEncoder.matches(this.inputPassword, password);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.ticketing.server.user.service.dto;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PasswordMatches {
|
||||
|
||||
boolean passwordMatches(String password);
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class DeleteUserTest {
|
||||
|
||||
// when
|
||||
// then
|
||||
assertThat(user.passwordEquals("123456")).isTrue();
|
||||
assertThat(user.passwordMatches("123456")).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user