Skip to content

Commit

Permalink
Ser invalid password count limit (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 authored Oct 24, 2023
2 parents cad85e8 + b44f069 commit 6c08db7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,36 @@ public class Giftcard {
@Column(length = 60, nullable = false)
private String password;

@Column
private Integer invalidPasswordCount;

@Builder
public Giftcard(String id, Voucher voucher, String message, LocalDateTime expiresAt, String password) {
this.id = id;
this.voucher = voucher;
this.message = message;
this.password = password;
this.expiresAt = expiresAt;
this.invalidPasswordCount = 0;
}

public void expire() {
public Giftcard expire() {
this.expiresAt = LocalDateTime.now();
return this;
}

public Giftcard increaseInvalidPasswordCount() {
this.invalidPasswordCount++;
return this;

}

public Giftcard resetInvalidPasswordCount() {
this.invalidPasswordCount = 0;
return this;
}

public boolean isEnable() {
return this.expiresAt.isAfter(LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,21 @@ public Giftcard read(String id) {
public GiftcardResponseDto read(String id, String password) {
Giftcard giftcard = read(id);

if (giftcard.getExpiresAt().isBefore(LocalDateTime.now())) {
throw new BusinessException("만료된 링크입니다.", StatusEnum.BAD_REQUEST);
if (!giftcard.isEnable()) {
throw new BusinessException("기간이 만료된 링크입니다.", StatusEnum.BAD_REQUEST);
}

if (giftcard.getInvalidPasswordCount() >= 10) {
throw new BusinessException("비밀번호를 10회 이상 틀렸습니다. 이 링크는 더 이상 사용할 수 없습니다.", StatusEnum.FORBIDDEN);
}

if (!decryptPassword(giftcard.getPassword()).equals(password)) {
save(giftcard.increaseInvalidPasswordCount());
throw new BusinessException("비밀번호가 일치하지 않습니다.", StatusEnum.FORBIDDEN);
}

save(giftcard.resetInvalidPasswordCount());

return GiftcardResponseDto.builder()
.sender(giftcard.getVoucher().getUser().getNickname())
.message(giftcard.getMessage())
Expand All @@ -95,6 +102,10 @@ public GiftcardResponseDto read(String id, String password) {
.build();
}

public Giftcard save(Giftcard giftcard) {
return giftCardRepository.save(giftcard);
}

/**
* 기프트 카드의 소유자를 변경합니다.
* @param giftcardId: 변경할 기프트 카드의 id
Expand All @@ -112,9 +123,7 @@ public Giftcard changeVoucherUser(String giftcardId, String username) {
User newUser = userService.read(username);
updatedVoucher.setUser(newUser);
voucherRepository.save(updatedVoucher);

giftcard.expire();
giftCardRepository.save(giftcard);
giftCardRepository.save(giftcard.expire());
return giftcard;
}

Expand Down

0 comments on commit 6c08db7

Please sign in to comment.