-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from chjcode/feat/delete-account
회원탈퇴 기능
- Loading branch information
Showing
14 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/chzzk/grassdiary/domain/member/entity/WithdrawnMember.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package chzzk.grassdiary.domain.member.entity; | ||
|
||
import chzzk.grassdiary.domain.base.BaseTimeEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class WithdrawnMember extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "withdrawn_member_id") | ||
private Long id; | ||
|
||
@Column(name = "hashed_email", nullable = false, unique = true) | ||
private String hashedEmail; | ||
|
||
@Builder | ||
public WithdrawnMember(String hashedEmail) { | ||
this.hashedEmail = hashedEmail; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/chzzk/grassdiary/domain/member/entity/WithdrawnMemberDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package chzzk.grassdiary.domain.member.entity; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface WithdrawnMemberDAO extends JpaRepository<WithdrawnMember, Long> { | ||
boolean existsByHashedEmail(String hashedEmail); | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/chzzk/grassdiary/domain/member/service/MemberService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/chzzk/grassdiary/domain/member/service/WithdrawnMemberService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package chzzk.grassdiary.domain.member.service; | ||
|
||
import chzzk.grassdiary.domain.comment.entity.Comment; | ||
import chzzk.grassdiary.domain.comment.entity.CommentDAO; | ||
import chzzk.grassdiary.domain.comment.service.CommentService; | ||
import chzzk.grassdiary.domain.diary.entity.Diary; | ||
import chzzk.grassdiary.domain.diary.entity.DiaryDAO; | ||
import chzzk.grassdiary.domain.diary.service.DiaryService; | ||
import chzzk.grassdiary.domain.member.entity.Member; | ||
import chzzk.grassdiary.domain.member.entity.MemberDAO; | ||
import chzzk.grassdiary.domain.member.entity.WithdrawnMember; | ||
import chzzk.grassdiary.domain.member.entity.WithdrawnMemberDAO; | ||
import chzzk.grassdiary.domain.reward.service.RewardService; | ||
import chzzk.grassdiary.global.common.error.exception.SystemException; | ||
import chzzk.grassdiary.global.common.response.ClientErrorCode; | ||
import chzzk.grassdiary.global.common.response.ServerErrorCode; | ||
import chzzk.grassdiary.global.util.hash.EmailHasher; | ||
import java.util.Base64; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class WithdrawnMemberService { | ||
private final MemberDAO memberDAO; | ||
private final DiaryDAO diaryDAO; | ||
private final CommentDAO commentDAO; | ||
private final WithdrawnMemberDAO withdrawnMemberDAO; | ||
private final DiaryService diaryService; | ||
private final CommentService commentService; | ||
private final RewardService rewardService; | ||
private final EmailHasher emailHasher; | ||
|
||
@Transactional | ||
public void withdrawMember(Long logInMemberId) { | ||
Member member = findMember(logInMemberId); | ||
|
||
deleteDiaries(logInMemberId); | ||
|
||
deleteComments(logInMemberId); | ||
|
||
deleteRewards(logInMemberId); | ||
|
||
//구입한 잔디 색깔 이력 삭제 | ||
|
||
//email 해싱 | ||
String hashedEmail = hashEmail(member.getEmail()); | ||
|
||
// 해싱된 이메일을 WithdrawnMember에 저장 | ||
saveWithdrawnMember(hashedEmail); | ||
|
||
// member의 값들 식별불가하게 변경 | ||
member.withdrawMember(); | ||
memberDAO.save(member); | ||
} | ||
|
||
@Transactional | ||
public void checkWithdrawnMember(String email) { | ||
String hashedEmail = hashEmail(email); | ||
if (withdrawnMemberDAO.existsByHashedEmail(hashedEmail)) { | ||
throw new SystemException(ClientErrorCode.ALREADY_WITHDRAWN_MEMBER_ERR); | ||
} | ||
} | ||
|
||
private Member findMember(Long memberId) { | ||
return memberDAO.findById(memberId) | ||
.orElseThrow(() -> new SystemException(ClientErrorCode.MEMBER_NOT_FOUND_ERR)); | ||
} | ||
|
||
private void deleteDiaries(long memberId) { | ||
List<Diary> diaries = diaryDAO.findAllByMemberId(memberId); | ||
|
||
for (Diary diary : diaries) { | ||
diaryService.delete(diary.getId(), memberId); | ||
} | ||
} | ||
|
||
private void deleteComments(long memberId) { | ||
List<Comment> comments = commentDAO.findAllByMemberId(memberId); | ||
|
||
for (Comment comment : comments) { | ||
commentService.delete(comment.getId(), memberId); | ||
} | ||
} | ||
|
||
private void deleteRewards(long memberId) { | ||
rewardService.deleteAllRewardHistory(memberId); | ||
} | ||
|
||
// 이메일 해싱 메서드 | ||
private String hashEmail(String email) { | ||
try { | ||
return emailHasher.hashEmail(email); // 이메일 해싱 | ||
} catch (Exception e) { | ||
throw new SystemException(ServerErrorCode.HASHING_FAILED, e); | ||
} | ||
} | ||
|
||
// WithdrawnMember 저장 메서드 | ||
private void saveWithdrawnMember(String hashedEmail) { | ||
WithdrawnMember withdrawnMember = WithdrawnMember.builder() | ||
.hashedEmail(hashedEmail) | ||
.build(); | ||
|
||
withdrawnMemberDAO.save(withdrawnMember); // WithdrawnMember 엔티티 저장 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/chzzk/grassdiary/global/util/hash/EmailHasher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package chzzk.grassdiary.global.util.hash; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.util.Base64; | ||
import javax.crypto.Mac; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.PBEKeySpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class EmailHasher { | ||
|
||
private static final String HMAC_ALGORITHM = "HmacSHA256"; | ||
|
||
@Value("${HASH_SECRET_KEY}") | ||
private String secretKey; | ||
|
||
public String hashEmail(String email) throws Exception { | ||
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), HMAC_ALGORITHM); | ||
Mac mac = Mac.getInstance(HMAC_ALGORITHM); | ||
mac.init(secretKeySpec); | ||
byte[] hmac = mac.doFinal(email.getBytes(StandardCharsets.UTF_8)); | ||
return Base64.getEncoder().encodeToString(hmac); | ||
} | ||
|
||
} |