Skip to content

Commit 76f46f1

Browse files
committed
fix(VerifiedAdmin): oneToone필드 제거
1 parent 9017398 commit 76f46f1

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

linkmind/src/main/java/com/app/toaster/admin/entity/VerifiedAdmin.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import lombok.Builder;
88
import lombok.Getter;
99
import lombok.NoArgsConstructor;
10+
import org.hibernate.annotations.OnDelete;
11+
import org.hibernate.annotations.OnDeleteAction;
1012

1113
import java.util.Objects;
1214

@@ -26,8 +28,9 @@ public class VerifiedAdmin {
2628
@Column
2729
private boolean authorized;
2830

29-
@OneToOne(optional = false)
30-
@JoinColumn(name="admin_id", unique=true, nullable=false, updatable=false)
31+
@ManyToOne(optional = false)
32+
@OnDelete(action = OnDeleteAction.CASCADE)
33+
@JoinColumn(name="admin_id")
3134
private ToasterAdmin admin;
3235

3336
@Builder

linkmind/src/main/java/com/app/toaster/admin/service/AdminService.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
1717
import lombok.RequiredArgsConstructor;
1818

19+
import lombok.extern.slf4j.Slf4j;
1920
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
2022
import org.springframework.security.crypto.password.PasswordEncoder;
2123
import org.springframework.stereotype.Service;
2224
import org.springframework.transaction.annotation.Transactional;
@@ -25,11 +27,12 @@
2527

2628
@Service
2729
@RequiredArgsConstructor
30+
@Slf4j
2831
public class AdminService {
2932

3033
private final UserRepository userRepository;
3134
private final JwtService jwtService;
32-
private final PasswordEncoder passwordEncoder;
35+
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
3336
private final VerifiedAdminRepository verifiedAdminRepository;
3437
private final AdminRepository adminRepository;
3538
private final GoogleAuthenticator googleAuthenticator;
@@ -62,25 +65,34 @@ public VerifyNewAdminCommand registerVerifiedUser(final ToasterAdmin toasterAdmi
6265
String otpKey = null;
6366
Long id = null;
6467

68+
Optional<VerifiedAdmin> existVerifiedAdmin = verifiedAdminRepository.findByAdmin(toasterAdmin);
69+
6570
if (isNewAdmin) { //새로운 어드민의 경우 등록.
71+
log.info("갱신해야되는 케이스.");
72+
73+
deletePastVerify(existVerifiedAdmin);
6674

6775
GoogleAuthenticatorKey key = googleAuthenticator.createCredentials();
6876

6977
VerifiedAdmin verifiedAdmin = VerifiedAdmin.builder()
7078
.admin(toasterAdmin)
7179
.build();
7280

81+
7382
otpKey = key.getKey();
7483
verifiedAdmin.changeOtpSecretKey(otpKey);
7584

7685
id = verifiedAdminRepository.save(verifiedAdmin).getId();
7786

7887
} else { //기존 경우의 경우는 그냥 찾기.
88+
log.info("기존의 경우로 넘어왔숨.");
89+
90+
if (existVerifiedAdmin.isEmpty()){
91+
throw new CustomException(Error.NOT_FOUND_USER_EXCEPTION, "찾을 수 없는 어드민 증명");
92+
}
7993

80-
VerifiedAdmin existVerifiedAdmin = verifiedAdminRepository.findByAdmin(toasterAdmin)
81-
.orElseThrow(() -> new CustomException(Error.NOT_FOUND_USER_EXCEPTION, "찾을 수 없는 어드민 증명"));
82-
id = existVerifiedAdmin.getId();
83-
otpKey = existVerifiedAdmin.getOtpSecretKey();
94+
id = existVerifiedAdmin.get().getId();
95+
otpKey = existVerifiedAdmin.get().getOtpSecretKey();
8496

8597
}
8698

@@ -94,17 +106,20 @@ public VerifyNewAdminCommand registerAdmin(String username, String password) {
94106

95107
if (adminString.equals(username)) {
96108

97-
ToasterAdmin existAdmin = findExistAdminPreVerification(username, password);
109+
ToasterAdmin existAdmin = findExistAdminPreVerification(username, password); //암호화 된 패스워드로 이미 했던적있는지 확인.
98110

99111
if (existAdmin != null) {
112+
log.info("존재합니다. 전 이 게임을 해봤어요.");
100113
if (existAdmin.verifyLastDate()) { //검증된 경우면 걍 어드민을 리턴.
101114
return registerVerifiedUser(existAdmin, false);
115+
}else{
116+
return registerVerifiedUser(existAdmin, true); //아닌 경우는 갱신을 해야됨.
102117
}
103-
return registerVerifiedUser(existAdmin, true);
104118
}
105119

106-
107-
String encPassword = passwordEncoder.encode(password);
120+
//id는 알고있음. Password를 통한 관리자 회원가입 시키기.
121+
log.info("디비에 어드민이 존재하지않아 어드민 회원가입 진행.");
122+
String encPassword = passwordEncoder.encode(password.toLowerCase());
108123

109124
ToasterAdmin toasterAdmin = ToasterAdmin.builder()
110125
.username(username)
@@ -116,18 +131,26 @@ public VerifyNewAdminCommand registerAdmin(String username, String password) {
116131
}
117132
throw new CustomException(Error.NOT_FOUND_USER_EXCEPTION, "어드민이 아닙니다.");
118133
}
134+
@Transactional
135+
public void deletePastVerify(Optional<VerifiedAdmin> existVerifiedAdmin){
136+
if(existVerifiedAdmin.isPresent()){
137+
verifiedAdminRepository.delete(existVerifiedAdmin.get());
138+
}
139+
}
119140

120141
public ToasterAdmin findExistAdminPreVerification(String username, String password) {
121142
Optional<ToasterAdmin> admin = adminRepository.findByUsername(username);
143+
log.info("admin이 이미 존재하는지 password match 진행.");
122144
if (admin.isEmpty()){
123145
return null;
124146
}
125147

126-
if (passwordEncoder.matches(password, admin.get().getPassword())) {
148+
if (passwordEncoder.matches(password.toLowerCase(), admin.get().getPassword())) {
127149
return admin.get();
150+
}else{
151+
throw new CustomException(Error.NOT_FOUND_USER_EXCEPTION, "비밀번호가 틀립니다.");
128152
}
129153

130-
return null; //TODO: 다른 엣지 케이스가 더 있는지 생각해보고 없으면 걍 바로 에러 throw
131154
}
132155

133156
}

0 commit comments

Comments
 (0)