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

fix(VerifiedAdmin): oneToone필드 제거 #260

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -7,6 +7,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import java.util.Objects;

Expand All @@ -26,8 +28,9 @@ public class VerifiedAdmin {
@Column
private boolean authorized;

@OneToOne(optional = false)
@JoinColumn(name="admin_id", unique=true, nullable=false, updatable=false)
@ManyToOne(optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name="admin_id")
private ToasterAdmin admin;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,11 +27,12 @@

@Service
@RequiredArgsConstructor
@Slf4j
public class AdminService {

private final UserRepository userRepository;
private final JwtService jwtService;
private final PasswordEncoder passwordEncoder;
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
private final VerifiedAdminRepository verifiedAdminRepository;
private final AdminRepository adminRepository;
private final GoogleAuthenticator googleAuthenticator;
Expand Down Expand Up @@ -62,25 +65,34 @@ public VerifyNewAdminCommand registerVerifiedUser(final ToasterAdmin toasterAdmi
String otpKey = null;
Long id = null;

Optional<VerifiedAdmin> existVerifiedAdmin = verifiedAdminRepository.findByAdmin(toasterAdmin);

if (isNewAdmin) { //새로운 어드민의 경우 등록.
log.info("갱신해야되는 케이스.");

deletePastVerify(existVerifiedAdmin);

GoogleAuthenticatorKey key = googleAuthenticator.createCredentials();

VerifiedAdmin verifiedAdmin = VerifiedAdmin.builder()
.admin(toasterAdmin)
.build();


otpKey = key.getKey();
verifiedAdmin.changeOtpSecretKey(otpKey);

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

} else { //기존 경우의 경우는 그냥 찾기.
log.info("기존의 경우로 넘어왔숨.");

if (existVerifiedAdmin.isEmpty()){
throw new CustomException(Error.NOT_FOUND_USER_EXCEPTION, "찾을 수 없는 어드민 증명");
}

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

}

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

if (adminString.equals(username)) {

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

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


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

ToasterAdmin toasterAdmin = ToasterAdmin.builder()
.username(username)
Expand All @@ -116,18 +131,26 @@ public VerifyNewAdminCommand registerAdmin(String username, String password) {
}
throw new CustomException(Error.NOT_FOUND_USER_EXCEPTION, "어드민이 아닙니다.");
}
@Transactional
public void deletePastVerify(Optional<VerifiedAdmin> existVerifiedAdmin){
if(existVerifiedAdmin.isPresent()){
verifiedAdminRepository.delete(existVerifiedAdmin.get());
}
}

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

if (passwordEncoder.matches(password, admin.get().getPassword())) {
if (passwordEncoder.matches(password.toLowerCase(), admin.get().getPassword())) {
return admin.get();
}else{
throw new CustomException(Error.NOT_FOUND_USER_EXCEPTION, "비밀번호가 틀립니다.");
}

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

}