Skip to content

Commit 5af3bb7

Browse files
committed
✨ feat : 관리자 퀴즈 수정, 삭제 로직 추가 + User 하드코딩 삭제
1 parent d7911fe commit 5af3bb7

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

backend/src/main/java/io/f1/backend/domain/question/app/QuestionService.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
import io.f1.backend.domain.question.entity.TextQuestion;
1111
import io.f1.backend.domain.quiz.entity.Quiz;
1212
import io.f1.backend.global.exception.CustomException;
13+
import io.f1.backend.global.exception.errorcode.AuthErrorCode;
1314
import io.f1.backend.global.exception.errorcode.QuestionErrorCode;
1415

16+
import io.f1.backend.global.security.enums.Role;
17+
import io.f1.backend.global.util.SecurityUtils;
18+
import java.util.Objects;
1519
import lombok.RequiredArgsConstructor;
1620

1721
import org.springframework.stereotype.Service;
@@ -47,10 +51,25 @@ public void updateQuestionContent(Long questionId, String content) {
4751
.orElseThrow(
4852
() -> new CustomException(QuestionErrorCode.QUESTION_NOT_FOUND));
4953

54+
55+
verifyUserAuthority(question.getQuiz());
56+
5057
TextQuestion textQuestion = question.getTextQuestion();
5158
textQuestion.changeContent(content);
5259
}
5360

61+
private static void verifyUserAuthority(Quiz quiz) {
62+
if(SecurityUtils.getCurrentUserRole() == Role.USER) {
63+
validateOwner(quiz.getCreator().getId());
64+
}
65+
}
66+
67+
private static void validateOwner(Long creatorId) {
68+
if (!Objects.equals(SecurityUtils.getCurrentUserId(), creatorId)) {
69+
throw new CustomException(AuthErrorCode.FORBIDDEN);
70+
}
71+
}
72+
5473
@Transactional
5574
public void updateQuestionAnswer(Long questionId, String answer) {
5675

@@ -62,6 +81,8 @@ public void updateQuestionAnswer(Long questionId, String answer) {
6281
.orElseThrow(
6382
() -> new CustomException(QuestionErrorCode.QUESTION_NOT_FOUND));
6483

84+
verifyUserAuthority(question.getQuiz());
85+
6586
question.changeAnswer(answer);
6687
}
6788

@@ -74,6 +95,8 @@ public void deleteQuestion(Long questionId) {
7495
.orElseThrow(
7596
() -> new CustomException(QuestionErrorCode.QUESTION_NOT_FOUND));
7697

98+
verifyUserAuthority(question.getQuiz());
99+
77100
questionRepository.delete(question);
78101
}
79102

backend/src/main/java/io/f1/backend/domain/quiz/app/QuizService.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import io.f1.backend.global.exception.errorcode.AuthErrorCode;
2121
import 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;
2327
import lombok.RequiredArgsConstructor;
2428
import 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
}

backend/src/main/java/io/f1/backend/domain/quiz/mapper/QuizMapper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@
1818

1919
public class QuizMapper {
2020

21-
// TODO : 이후 파라미터에서 user 삭제하기
2221
public static Quiz quizCreateRequestToQuiz(
23-
QuizCreateRequest quizCreateRequest, String imgUrl, User user) {
22+
QuizCreateRequest quizCreateRequest, String imgUrl, User creator) {
2423

2524
return new Quiz(
2625
quizCreateRequest.getTitle(),
2726
quizCreateRequest.getDescription(),
2827
quizCreateRequest.getQuizType(),
2928
imgUrl,
30-
user // TODO : 이후 creator에 들어갈 User은 현재 로그인 중인 유저를 가져오도록 변경
29+
creator
3130
);
3231
}
3332

3433
public static QuizCreateResponse quizToQuizCreateResponse(Quiz quiz) {
35-
// TODO : creatorId 넣어주는 부분에서 Getter를 안 쓰고, 현재 로그인한 유저의 id를 담는 식으로 바꿔도 될 듯
3634
return new QuizCreateResponse(
3735
quiz.getId(),
3836
quiz.getTitle(),

backend/src/main/java/io/f1/backend/global/util/SecurityUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.f1.backend.global.exception.CustomException;
77
import io.f1.backend.global.exception.errorcode.AuthErrorCode;
88

9+
import io.f1.backend.global.security.enums.Role;
910
import jakarta.servlet.http.HttpSession;
1011

1112
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -43,6 +44,15 @@ public static String getCurrentUserNickname() {
4344
return getCurrentUserPrincipal().getUserNickname();
4445
}
4546

47+
public static Role getCurrentUserRole() {
48+
Authentication authentication = getAuthentication();
49+
if (authentication != null
50+
&& authentication.getPrincipal() instanceof UserPrincipal userPrincipal) {
51+
return Role.USER;
52+
}
53+
return Role.ADMIN;
54+
}
55+
4656
public static void logout(HttpSession session) {
4757
if (session != null) {
4858
session.invalidate();

0 commit comments

Comments
 (0)