13
13
import org .springframework .data .mongodb .core .query .Query ;
14
14
import org .springframework .data .mongodb .core .query .Update ;
15
15
import org .springframework .scheduling .annotation .Scheduled ;
16
+ import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
17
+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
18
+ import org .springframework .security .core .Authentication ;
19
+ import org .springframework .security .crypto .password .PasswordEncoder ;
16
20
import org .springframework .stereotype .Service ;
17
21
import org .springframework .transaction .annotation .Transactional ;
18
22
import org .springframework .web .multipart .MultipartFile ;
19
23
import swm .betterlife .antifragile .common .exception .ExcessRecommendLimitException ;
20
24
import swm .betterlife .antifragile .common .exception .MemberNotFoundException ;
25
+ import swm .betterlife .antifragile .common .exception .PasswordSameException ;
21
26
import swm .betterlife .antifragile .common .util .S3ImageComponent ;
27
+ import swm .betterlife .antifragile .domain .auth .dto .request .PasswordModifyRequest ;
22
28
import swm .betterlife .antifragile .domain .member .controller .MemberNicknameDuplResponse ;
23
29
import swm .betterlife .antifragile .domain .member .dto .request .MemberProfileModifyRequest ;
24
30
import swm .betterlife .antifragile .domain .member .dto .response .MemberDetailInfoResponse ;
@@ -40,6 +46,10 @@ public class MemberService {
40
46
private final MemberPointService memberPointService ;
41
47
private final MemberDiaryService memberDiaryService ;
42
48
private final S3ImageComponent s3ImageComponent ;
49
+ private final PasswordEncoder passwordEncoder ;
50
+ private final AuthenticationManagerBuilder authenticationManagerBuilder ;
51
+
52
+
43
53
44
54
@ Transactional (readOnly = true )
45
55
public MemberInfoResponse findMemberById (String id ) {
@@ -149,6 +159,30 @@ public MemberStatusResponse checkMemberStatus(
149
159
150
160
}
151
161
162
+ @ Transactional
163
+ public void modifyPassword (
164
+ String email , String memberId ,
165
+ LoginType loginType , PasswordModifyRequest passwordModifyRequest
166
+ ) {
167
+ String curPassword = passwordModifyRequest .curPassword ();
168
+ String newPassword = passwordModifyRequest .newPassword ();
169
+ Authentication authentication
170
+ = getAuthenticate (email , curPassword , loginType );
171
+
172
+ if (curPassword .equals (newPassword )) {
173
+ throw new PasswordSameException ();
174
+ }
175
+ String encodedPassword = passwordEncoder .encode (newPassword );
176
+ Query query = new Query (Criteria .where ("id" ).is (memberId ));
177
+ Update update = new Update ().set ("password" , encodedPassword );
178
+
179
+ UpdateResult result = mongoTemplate .updateFirst (query , update , Member .class );
180
+
181
+ if (result .getMatchedCount () == 0 ) {
182
+ throw new MemberNotFoundException ();
183
+ }
184
+ }
185
+
152
186
@ Scheduled (cron = "0 0 0 * * *" )
153
187
public void resetRemainRecommendNumber () {
154
188
Query query = new Query ();
@@ -157,4 +191,14 @@ public void resetRemainRecommendNumber() {
157
191
mongoTemplate .updateMulti (query , update , Member .class );
158
192
}
159
193
194
+ private Authentication getAuthenticate (
195
+ String email , String password ,
196
+ LoginType loginType
197
+ ) {
198
+ String username = loginType .name () + ":" + email ;
199
+ UsernamePasswordAuthenticationToken authenticationToken
200
+ = new UsernamePasswordAuthenticationToken (username , password );
201
+ return authenticationManagerBuilder .getObject ().authenticate (authenticationToken );
202
+ }
203
+
160
204
}
0 commit comments