Skip to content

Commit

Permalink
Release (#131)
Browse files Browse the repository at this point in the history
* test : 문제 통합테스트 추가 (#124)

* Refactor/login user argument resolver (#129)
  • Loading branch information
ekzm8523 authored Oct 21, 2023
1 parent dd908e1 commit f61e5db
Show file tree
Hide file tree
Showing 54 changed files with 543 additions and 455 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ src/main/resources/static/

## test ##
spy.log
src/test/resources/*.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package io.csbroker.apiserver.auth

import io.csbroker.apiserver.common.enums.ErrorCode
import io.csbroker.apiserver.common.exception.UnAuthorizedException
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.repository.user.UserRepository
import org.springframework.core.MethodParameter
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.User
import org.springframework.stereotype.Component
import org.springframework.web.bind.support.WebDataBinderFactory
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer

@Component
class LoginUserArgumentResolver : HandlerMethodArgumentResolver {
class LoginUserArgumentResolver(
private val userRepository: UserRepository,
) : HandlerMethodArgumentResolver {
override fun supportsParameter(parameter: MethodParameter): Boolean {
parameter.getParameterAnnotation(LoginUser::class.java) ?: return false
return parameter.parameterType.isAssignableFrom(User::class.java)
Expand All @@ -24,13 +27,14 @@ class LoginUserArgumentResolver : HandlerMethodArgumentResolver {
webRequest: NativeWebRequest,
binderFactory: WebDataBinderFactory?,
): Any {
val principal = SecurityContextHolder.getContext().authentication?.principal
val authentication = SecurityContextHolder.getContext().authentication

// 유저 검증이 되지 않으면, anonymousUser 라는 String으로 넘어옴. 그것을 방지하기 위한 검증.
if (principal is String) {
if (authentication == null || authentication.principal.equals("anonymousUser")) {
throw UnAuthorizedException(ErrorCode.UNAUTHORIZED, "로그인이 필요합니다.")
}
val email = authentication.name

return principal ?: throw UnAuthorizedException(ErrorCode.UNAUTHORIZED, "로그인이 필요합니다.")
return userRepository.findByEmail(email)
?: throw UnAuthorizedException(ErrorCode.NOT_FOUND_ENTITY, "알 수 없는 유저의 요청입니다.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import io.csbroker.apiserver.dto.problem.AdminProblemSearchDto
import io.csbroker.apiserver.dto.problem.longproblem.LongProblemResponseDto
import io.csbroker.apiserver.dto.problem.longproblem.LongProblemSearchResponseDto
import io.csbroker.apiserver.dto.problem.longproblem.LongProblemUpsertRequestDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.problem.admin.AdminLongProblemService
import org.springframework.data.domain.Pageable
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand Down Expand Up @@ -37,7 +37,7 @@ class AdminLongProblemController(
@RequestBody createRequestDto: LongProblemUpsertRequestDto,
@LoginUser loginUser: User,
): ApiResponse<UpsertSuccessResponseDto> {
val createProblemId = longProblemService.createProblem(createRequestDto, loginUser.username)
val createProblemId = longProblemService.createProblem(createRequestDto, loginUser)
return ApiResponse.success(UpsertSuccessResponseDto(createProblemId))
}

Expand All @@ -47,7 +47,7 @@ class AdminLongProblemController(
@RequestBody updateRequestDto: LongProblemUpsertRequestDto,
@LoginUser loginUser: User,
): ApiResponse<UpsertSuccessResponseDto> {
val updateProblemId = longProblemService.updateProblem(id, updateRequestDto, loginUser.username)
val updateProblemId = longProblemService.updateProblem(id, updateRequestDto)
return ApiResponse.success(UpsertSuccessResponseDto(updateProblemId))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import io.csbroker.apiserver.dto.problem.AdminProblemSearchDto
import io.csbroker.apiserver.dto.problem.multiplechoiceproblem.MultipleChoiceProblemResponseDto
import io.csbroker.apiserver.dto.problem.multiplechoiceproblem.MultipleChoiceProblemSearchResponseDto
import io.csbroker.apiserver.dto.problem.multiplechoiceproblem.MultipleChoiceProblemUpsertRequestDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.problem.admin.AdminMultipleProblemService
import org.springframework.data.domain.Pageable
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand Down Expand Up @@ -37,7 +37,7 @@ class AdminMultipleProblemController(
@RequestBody createRequestDto: MultipleChoiceProblemUpsertRequestDto,
@LoginUser loginUser: User,
): ApiResponse<UpsertSuccessResponseDto> {
val createProblemId = multipleProblemService.createProblem(createRequestDto, loginUser.username)
val createProblemId = multipleProblemService.createProblem(createRequestDto, loginUser)
return ApiResponse.success(UpsertSuccessResponseDto(createProblemId))
}

Expand All @@ -47,7 +47,7 @@ class AdminMultipleProblemController(
@RequestBody updateRequestDto: MultipleChoiceProblemUpsertRequestDto,
@LoginUser loginUser: User,
): ApiResponse<UpsertSuccessResponseDto> {
val updateProblemId = multipleProblemService.updateProblem(id, updateRequestDto, loginUser.username)
val updateProblemId = multipleProblemService.updateProblem(id, updateRequestDto)
return ApiResponse.success(UpsertSuccessResponseDto(updateProblemId))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import io.csbroker.apiserver.dto.problem.AdminProblemSearchDto
import io.csbroker.apiserver.dto.problem.shortproblem.ShortProblemResponseDto
import io.csbroker.apiserver.dto.problem.shortproblem.ShortProblemSearchResponseDto
import io.csbroker.apiserver.dto.problem.shortproblem.ShortProblemUpsertRequestDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.problem.admin.AdminShortProblemService
import org.springframework.data.domain.Pageable
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand Down Expand Up @@ -37,7 +37,7 @@ class AdminShortProblemController(
@RequestBody createRequestDto: ShortProblemUpsertRequestDto,
@LoginUser loginUser: User,
): ApiResponse<UpsertSuccessResponseDto> {
val createProblemId = shortProblemService.createProblem(createRequestDto, loginUser.username)
val createProblemId = shortProblemService.createProblem(createRequestDto, loginUser)
return ApiResponse.success(UpsertSuccessResponseDto(createProblemId))
}

Expand All @@ -47,7 +47,7 @@ class AdminShortProblemController(
@RequestBody updateRequestDto: ShortProblemUpsertRequestDto,
@LoginUser loginUser: User,
): ApiResponse<UpsertSuccessResponseDto> {
val updateProblemId = shortProblemService.updateProblem(id, updateRequestDto, loginUser.username)
val updateProblemId = shortProblemService.updateProblem(id, updateRequestDto)
return ApiResponse.success(UpsertSuccessResponseDto(updateProblemId))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import io.csbroker.apiserver.dto.useranswer.UserAnswerLabelRequestDto
import io.csbroker.apiserver.dto.useranswer.UserAnswerResponseDto
import io.csbroker.apiserver.dto.useranswer.UserAnswerSearchResponseDto
import io.csbroker.apiserver.dto.useranswer.UserAnswerUpsertDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.problem.UserAnswerService
import org.springframework.data.domain.Pageable
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
Expand Down Expand Up @@ -62,14 +62,14 @@ class AdminUserAnswerController(
val answerId = when (type) {
"label" ->
userAnswerService.labelUserAnswer(
loginUser.username,
loginUser.email,
id,
userAnswerLabelRequestDto.selectedGradingStandardIds,
)

"validate" ->
userAnswerService.validateUserAnswer(
loginUser.username,
loginUser.email,
id,
userAnswerLabelRequestDto.selectedGradingStandardIds,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import io.csbroker.apiserver.dto.common.UpsertSuccessResponseDto
import io.csbroker.apiserver.dto.user.UserInfoResponseDto
import io.csbroker.apiserver.dto.user.UserLoginRequestDto
import io.csbroker.apiserver.dto.user.UserSignUpDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.repository.common.REFRESH_TOKEN
import io.csbroker.apiserver.service.auth.AuthService
import io.csbroker.apiserver.service.common.MailService
import kotlinx.coroutines.runBlocking
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import io.csbroker.apiserver.dto.StatsDto
import io.csbroker.apiserver.dto.common.ApiResponse
import io.csbroker.apiserver.dto.common.ChatCompletionRequestDto
import io.csbroker.apiserver.dto.common.RankListDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.common.ChatService
import io.csbroker.apiserver.service.common.CommonService
import io.csbroker.apiserver.service.common.S3Service
import io.csbroker.apiserver.service.user.UserService
import kotlinx.coroutines.runBlocking
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand All @@ -37,7 +37,7 @@ class CommonController(
fun uploadImg(@RequestPart("image") multipartFile: MultipartFile, @LoginUser loginUser: User): ApiResponse<String> {
return runBlocking {
val imgUrl = s3Service.uploadProfileImg(multipartFile)
userService.updateUserProfileImg(loginUser.username, imgUrl)
userService.updateUserProfileImg(loginUser, imgUrl)
ApiResponse.success(imgUrl)
}
}
Expand Down Expand Up @@ -65,6 +65,6 @@ class CommonController(
@LoginUser loginUser: User,
@RequestBody chatCompletionRequestDto: ChatCompletionRequestDto,
): ApiResponse<String> {
return ApiResponse.success(chatService.completeChat(loginUser.username, chatCompletionRequestDto.content))
return ApiResponse.success(chatService.completeChat(loginUser.email, chatCompletionRequestDto.content))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import io.csbroker.apiserver.dto.notification.NotificationBulkReadDto
import io.csbroker.apiserver.dto.notification.NotificationPageResponseDto
import io.csbroker.apiserver.dto.notification.NotificationReadResponseDto
import io.csbroker.apiserver.dto.notification.UnReadNotificationCountDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.common.NotificationService
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
Expand All @@ -34,13 +34,13 @@ class NotificationController(
direction = Sort.Direction.DESC,
) pageable: Pageable,
): ApiResponse<NotificationPageResponseDto> {
val notifications = notificationService.getNotification(loginUser.username, pageable)
val notifications = notificationService.getNotification(loginUser.id!!, pageable)
return ApiResponse.success(NotificationPageResponseDto(notifications))
}

@GetMapping("/count")
fun getUnreadNotificationCount(@LoginUser loginUser: User): ApiResponse<UnReadNotificationCountDto> {
val count = notificationService.getUnreadNotificationCount(loginUser.username)
val count = notificationService.getUnreadNotificationCount(loginUser.id!!)
return ApiResponse.success(UnReadNotificationCountDto(count))
}

Expand All @@ -49,7 +49,7 @@ class NotificationController(
@LoginUser loginUser: User,
@RequestBody notificationBulkReadDto: NotificationBulkReadDto,
): ApiResponse<NotificationReadResponseDto> {
notificationService.readNotifications(loginUser.username, notificationBulkReadDto.ids)
notificationService.readNotifications(loginUser.id!!, notificationBulkReadDto.ids)
return ApiResponse.success(NotificationReadResponseDto())
}

Expand All @@ -58,7 +58,7 @@ class NotificationController(
@LoginUser loginUser: User,
@PathVariable("id") id: Long,
): ApiResponse<NotificationReadResponseDto> {
notificationService.readNotificationById(loginUser.username, id)
notificationService.readNotificationById(loginUser.id!!, id)
return ApiResponse.success(NotificationReadResponseDto())
}

Expand All @@ -67,7 +67,7 @@ class NotificationController(
@LoginUser loginUser: User,
@RequestBody notificationBulkDeleteDto: NotificationBulkDeleteDto,
): ApiResponse<Boolean> {
notificationService.deleteNotifications(loginUser.username, notificationBulkDeleteDto.ids)
notificationService.deleteNotifications(loginUser, notificationBulkDeleteDto.ids)
return ApiResponse.success(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package io.csbroker.apiserver.controller.v1.post
import io.csbroker.apiserver.auth.LoginUser
import io.csbroker.apiserver.controller.v1.post.request.CommentCreateRequestDto
import io.csbroker.apiserver.dto.common.ApiResponse
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.post.CommentService
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import io.csbroker.apiserver.common.util.getEmailFromSecurityContextHolder
import io.csbroker.apiserver.controller.v1.post.request.PostCreateRequestDto
import io.csbroker.apiserver.controller.v1.post.response.PostResponseDto
import io.csbroker.apiserver.dto.common.ApiResponse
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.post.PostService
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import io.csbroker.apiserver.dto.problem.grade.LongProblemGradingRequestDto
import io.csbroker.apiserver.dto.problem.longproblem.LongProblemAnswerDto
import io.csbroker.apiserver.dto.problem.longproblem.LongProblemDetailResponseDto
import io.csbroker.apiserver.dto.problem.longproblem.LongProblemGradingHistoryDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.post.PostService
import io.csbroker.apiserver.service.problem.LongProblemService
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand Down Expand Up @@ -41,7 +41,7 @@ class LongProblemController(
@RequestParam("isGrading", required = false) isGrading: Boolean?,
): ApiResponse<LongProblemGradingHistoryDto> {
val gradingRequestDto = LongProblemGradingRequestDto(
loginUser.username,
loginUser.email,
id,
answerDto.answer,
isGrading ?: true,
Expand All @@ -52,13 +52,17 @@ class LongProblemController(

@PostMapping("{id}/submit")
fun submitLongProblem(
@LoginUser user: User,
@LoginUser loginUser: User,
@PathVariable("id") problemId: Long,
@RequestBody answerDto: LongProblemAnswerDto,
): ApiResponse<SubmitLongProblemResponseDto> {
val submitRequestDto = SubmitLongProblemDto(user.username, problemId, answerDto.answer)
val submitRequestDto = SubmitLongProblemDto(loginUser.email, problemId, answerDto.answer)
val submitResponseDto = longProblemService.submitProblem(submitRequestDto)
postService.create(problemId = submitRequestDto.problemId, content = answerDto.answer, email = user.username)
postService.create(
problemId = submitRequestDto.problemId,
content = answerDto.answer,
email = loginUser.email,
)
return ApiResponse.success(submitResponseDto)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import io.csbroker.apiserver.dto.problem.grade.MultipleProblemGradingRequestDto
import io.csbroker.apiserver.dto.problem.multiplechoiceproblem.MultipleChoiceProblemAnswerDto
import io.csbroker.apiserver.dto.problem.multiplechoiceproblem.MultipleChoiceProblemDetailResponseDto
import io.csbroker.apiserver.dto.problem.multiplechoiceproblem.MultipleChoiceProblemGradingHistoryDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.problem.MultipleProblemService
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand All @@ -35,7 +35,7 @@ class MultipleProblemController(
@PathVariable("id") id: Long,
@RequestBody answerDto: MultipleChoiceProblemAnswerDto,
): ApiResponse<MultipleChoiceProblemGradingHistoryDto> {
val gradingRequestDto = MultipleProblemGradingRequestDto(loginUser.username, id, answerDto.answerIds)
val gradingRequestDto = MultipleProblemGradingRequestDto(loginUser, id, answerDto.answerIds)
val gradeHistory = multipleProblemService.gradingProblem(gradingRequestDto)
return ApiResponse.success(gradeHistory)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import io.csbroker.apiserver.dto.problem.ProblemPageResponseDto
import io.csbroker.apiserver.dto.problem.ProblemSearchDto
import io.csbroker.apiserver.dto.problem.ProblemsResponseDto
import io.csbroker.apiserver.dto.problem.grade.AssessmentRequestDto
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.service.problem.CommonProblemService
import org.springframework.security.core.userdetails.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand Down Expand Up @@ -60,7 +60,7 @@ class ProblemController(
return ApiResponse.success(
UpsertSuccessResponseDto(
commonProblemService.gradingAssessment(
loginUser.username,
loginUser.email,
id,
assessmentRequestDto,
),
Expand Down
Loading

0 comments on commit f61e5db

Please sign in to comment.