Skip to content

Commit

Permalink
Merge pull request #260 from Link-MIND/feat/#256
Browse files Browse the repository at this point in the history
fix(VerifiedAdmin): oneTooneํ•„๋“œ ์ œ๊ฑฐ
  • Loading branch information
sss4920 authored Feb 3, 2025
2 parents 2e329e2 + 76f46f1 commit 455e631
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
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
}

}

0 comments on commit 455e631

Please sign in to comment.