1616import com .warrenstrange .googleauth .GoogleAuthenticatorKey ;
1717import lombok .RequiredArgsConstructor ;
1818
19+ import lombok .extern .slf4j .Slf4j ;
1920import org .springframework .beans .factory .annotation .Value ;
21+ import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
2022import org .springframework .security .crypto .password .PasswordEncoder ;
2123import org .springframework .stereotype .Service ;
2224import org .springframework .transaction .annotation .Transactional ;
2527
2628@ Service
2729@ RequiredArgsConstructor
30+ @ Slf4j
2831public 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