2020import io .f1 .backend .global .exception .errorcode .AuthErrorCode ;
2121import io .f1 .backend .global .exception .errorcode .QuizErrorCode ;
2222
23+ import io .f1 .backend .global .exception .errorcode .UserErrorCode ;
24+ import io .f1 .backend .global .security .enums .Role ;
25+ import io .f1 .backend .global .util .SecurityUtils ;
26+ import java .util .Objects ;
2327import lombok .RequiredArgsConstructor ;
2428import lombok .extern .slf4j .Slf4j ;
2529
@@ -52,7 +56,6 @@ public class QuizService {
5256 private final String DEFAULT = "default" ;
5357 private static final long MAX_FILE_SIZE = 5 * 1024 * 1024 ; // 5MB
5458
55- // TODO : 시큐리티 구현 이후 삭제해도 되는 의존성 주입
5659 private final UserRepository userRepository ;
5760 private final QuestionService questionService ;
5861 private final QuizRepository quizRepository ;
@@ -66,10 +69,11 @@ public QuizCreateResponse saveQuiz(MultipartFile thumbnailFile, QuizCreateReques
6669 thumbnailPath = convertToThumbnailPath (thumbnailFile );
6770 }
6871
69- // TODO : 시큐리티 구현 이후 삭제 (data.sql로 초기 저장해둔 유저 get), 나중엔 현재 로그인한 유저의 아이디를 받아오도록 수정
70- User user = userRepository .findById (1L ).orElseThrow (RuntimeException ::new );
72+ Long creatorId = SecurityUtils .getCurrentUserId ();
73+ User creator = userRepository .findById (creatorId )
74+ .orElseThrow (() -> new CustomException (UserErrorCode .USER_NOT_FOUND ));
7175
72- Quiz quiz = quizCreateRequestToQuiz (request , thumbnailPath , user );
76+ Quiz quiz = quizCreateRequestToQuiz (request , thumbnailPath , creator );
7377
7478 Quiz savedQuiz = quizRepository .save (quiz );
7579
@@ -125,22 +129,33 @@ public void deleteQuiz(Long quizId) {
125129 .findById (quizId )
126130 .orElseThrow (() -> new CustomException (QuizErrorCode .QUIZ_NOT_FOUND ));
127131
128- // TODO : util 메서드에서 사용자 ID 꺼내쓰는 식으로 수정하기
129- if (1L != quiz .getCreator ().getId ()) {
130- throw new CustomException (AuthErrorCode .FORBIDDEN );
131- }
132+ verifyUserAuthority (quiz );
132133
133134 deleteThumbnailFile (quiz .getThumbnailUrl ());
134135 quizRepository .deleteById (quizId );
135136 }
136137
138+ private static void verifyUserAuthority (Quiz quiz ) {
139+ if (SecurityUtils .getCurrentUserRole () == Role .USER ) {
140+ validateOwner (quiz .getCreator ().getId ());
141+ }
142+ }
143+
144+ private static void validateOwner (Long creatorId ) {
145+ if (!Objects .equals (SecurityUtils .getCurrentUserId (), creatorId )) {
146+ throw new CustomException (AuthErrorCode .FORBIDDEN );
147+ }
148+ }
149+
137150 @ Transactional
138151 public void updateQuizTitle (Long quizId , String title ) {
139152 Quiz quiz =
140153 quizRepository
141154 .findById (quizId )
142155 .orElseThrow (() -> new CustomException (QuizErrorCode .QUIZ_NOT_FOUND ));
143156
157+ verifyUserAuthority (quiz );
158+
144159 validateTitle (title );
145160 quiz .changeTitle (title );
146161 }
@@ -153,6 +168,8 @@ public void updateQuizDesc(Long quizId, String description) {
153168 .findById (quizId )
154169 .orElseThrow (() -> new CustomException (QuizErrorCode .QUIZ_NOT_FOUND ));
155170
171+ verifyUserAuthority (quiz );
172+
156173 validateDesc (description );
157174 quiz .changeDescription (description );
158175 }
@@ -165,6 +182,8 @@ public void updateThumbnail(Long quizId, MultipartFile thumbnailFile) {
165182 .findById (quizId )
166183 .orElseThrow (() -> new CustomException (QuizErrorCode .QUIZ_NOT_FOUND ));
167184
185+ verifyUserAuthority (quiz );
186+
168187 validateImageFile (thumbnailFile );
169188 String newThumbnailPath = convertToThumbnailPath (thumbnailFile );
170189
@@ -256,8 +275,6 @@ public List<Question> getRandomQuestionsWithoutAnswer(Long quizId, Integer round
256275 .findById (quizId )
257276 .orElseThrow (() -> new NoSuchElementException ("존재하지 않는 퀴즈입니다." ));
258277
259- List <Question > randomQuestions = quizRepository .findRandQuestionsByQuizId (quizId , round );
260-
261- return randomQuestions ;
278+ return quizRepository .findRandQuestionsByQuizId (quizId , round );
262279 }
263280}
0 commit comments