Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#518 [feat] 인가체제 변경 및 인가 API 중 필드 수정 #519

Merged
merged 10 commits into from
Oct 1, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,27 @@ private String getUserTokenFromHeader(final HttpServletRequest request) {
}

private boolean authenticateUserFromMap(final UserAuthAnnotation annotation,
final Map<Long, WriterNameInfo> userRoles,
final HashMap<Long, WriterNameInfo> userRoles,
final Map<String, String> pathVariables) {
switch (annotation.value()) {
case OWNER -> {
final Long requestMoimId = secureUrlUtil.decodeUrl(pathVariables.get(MOIM_ID));
if (!userRoles.containsKey(requestMoimId) || !userRoles.get(requestMoimId).moimRole().equals(MoimRole.OWNER)) {
throw new ForbiddenException(ErrorMessage.MOIM_OWNER_AUTHENTICATION_ERROR);
}
WriterNameContextUtil.setWriterNameContext(userRoles.get(requestMoimId).writerNameId());
WriterNameContextUtil.setWriterNameIdContext(userRoles.get(requestMoimId).writerNameId());
return true;
}
case WRITER_NAME -> {
final Long requestMoimId = secureUrlUtil.decodeUrl(pathVariables.get(MOIM_ID));
if (!userRoles.containsKey(requestMoimId)) {
throw new ForbiddenException(ErrorMessage.USER_MOIM_AUTHENTICATE_ERROR);
}
WriterNameContextUtil.setWriterNameContext(userRoles.get(requestMoimId).writerNameId());
WriterNameContextUtil.setWriterNameIdContext(userRoles.get(requestMoimId).writerNameId());
return true;
}
case USER -> {
WriterNameContextUtil.setMoimWriterNameMapContext(userRoles);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package com.mile.common.utils.thread;

import com.mile.writername.service.vo.WriterNameInfo;

import java.util.HashMap;

public class WriterNameContextUtil {
private static final ThreadLocal<Long> writerNameContext = new ThreadLocal<>();
private static final ThreadLocal<HashMap<Long, WriterNameInfo>> moimWriterNameMapContext = new ThreadLocal<>();

public static void setMoimWriterNameMapContext(HashMap<Long, WriterNameInfo> moimInfoMap) {
moimWriterNameMapContext.set(moimInfoMap);
}

public static void setWriterNameContext(Long writerNameId) {
public static void setWriterNameIdContext(Long writerNameId) {
writerNameContext.set(writerNameId);
}

public static Long getWriterNameContext() {
return writerNameContext.get();
}
public static HashMap<Long, WriterNameInfo> getMoimWriterNameMapContext() {return moimWriterNameMapContext.get();}

public static void clear() {
writerNameContext.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mile.common.auth.dto.AccessTokenDto;
import com.mile.common.resolver.moim.MoimIdPathVariable;
import com.mile.common.resolver.user.UserId;
import com.mile.common.utils.thread.WriterNameContextUtil;
import com.mile.dto.SuccessResponse;
import com.mile.exception.message.SuccessMessage;
import com.mile.moim.service.MoimService;
Expand All @@ -32,7 +33,7 @@
import com.mile.moim.service.dto.response.TopicListResponse;
import com.mile.moim.service.dto.response.WriterNameConflictCheckResponse;
import com.mile.writername.domain.MoimRole;
import com.mile.writername.service.dto.response.WriterNameShortResponse;
import com.mile.writername.service.dto.response.WriterNameInformationResponse;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -284,13 +285,13 @@ public ResponseEntity<SuccessResponse<MoimWriterNameListGetResponse>> getWriterN
}

@Override
@UserAuthAnnotation(UserAuthenticationType.WRITER_NAME)
@GetMapping("/{moimId}/writername")
public ResponseEntity<SuccessResponse<WriterNameShortResponse>> getWriterNameOfUser(
public ResponseEntity<SuccessResponse<WriterNameInformationResponse>> getWriterNameOfUser(
@MoimIdPathVariable final Long moimId,
@UserId final Long userId,
@PathVariable("moimId") final String moimUrl
) {
return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.WRITER_NAME_GET_SUCCESS, moimService.getWriterNameOfUser(moimId, userId)));
return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.WRITER_NAME_GET_SUCCESS, moimService.getWriterNameOfUser(WriterNameContextUtil.getWriterNameContext())));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.mile.moim.service.dto.response.ContentListResponse;
import com.mile.moim.service.dto.response.InvitationCodeGetResponse;
import com.mile.moim.service.dto.request.MoimCreateRequest;
import com.mile.moim.service.dto.response.MoimCreateResponse;
import com.mile.moim.service.dto.response.MoimCuriousPostListResponse;
import com.mile.moim.service.dto.request.MoimInfoModifyRequest;
import com.mile.moim.service.dto.response.MoimInfoOwnerResponse;
Expand All @@ -25,7 +24,7 @@
import com.mile.moim.service.dto.response.TopicListResponse;
import com.mile.moim.service.dto.request.WriterMemberJoinRequest;
import com.mile.moim.service.dto.response.WriterNameConflictCheckResponse;
import com.mile.writername.service.dto.response.WriterNameShortResponse;
import com.mile.writername.service.dto.response.WriterNameInformationResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand Down Expand Up @@ -395,9 +394,8 @@ ResponseEntity<SuccessResponse<MoimWriterNameListGetResponse>> getWriterNameList
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
}
)
ResponseEntity<SuccessResponse<WriterNameShortResponse>> getWriterNameOfUser(
ResponseEntity<SuccessResponse<WriterNameInformationResponse>> getWriterNameOfUser(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long moimId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final @UserId Long userId,
@PathVariable("moimId") final String moimUrl
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.mile.controller.post;

import com.mile.common.auth.annotation.UserAuthAnnotation;
import com.mile.common.auth.annotation.UserAuthenticationType;
import com.mile.common.resolver.user.UserId;
import com.mile.common.utils.thread.WriterNameContextUtil;
import com.mile.curious.service.dto.CuriousInfoResponse;
import com.mile.dto.SuccessResponse;
import com.mile.exception.message.SuccessMessage;
Expand Down Expand Up @@ -39,15 +42,15 @@ public class PostController implements PostControllerSwagger {

@PostMapping("/{postId}/comment")
@Override
@UserAuthAnnotation(UserAuthenticationType.USER)
public SuccessResponse postComment(
@PostIdPathVariable final Long postId,
@Valid @RequestBody final CommentCreateRequest commentCreateRequest,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
postService.createCommentOnPost(
postId,
userId,
WriterNameContextUtil.getMoimWriterNameMapContext(),
commentCreateRequest
);
return SuccessResponse.of(SuccessMessage.COMMENT_CREATE_SUCCESS);
Expand All @@ -56,43 +59,51 @@ public SuccessResponse postComment(

@PostMapping("/{postId}/curious")
@Override
@UserAuthAnnotation(UserAuthenticationType.USER)
public SuccessResponse<PostCuriousResponse> postCurious(
@PostIdPathVariable final Long postId,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.CURIOUS_CREATE_SUCCESS, postService.createCuriousOnPost(postId, userId));
return SuccessResponse.of(SuccessMessage.CURIOUS_CREATE_SUCCESS, postService.createCuriousOnPost(postId, WriterNameContextUtil.getMoimWriterNameMapContext()));
}

@GetMapping("/{postId}/comment")
@Override
@UserAuthAnnotation(UserAuthenticationType.USER)
public ResponseEntity<SuccessResponse<CommentListResponse>> getComments(
@PostIdPathVariable final Long postId,
@UserId Long userId,
@PathVariable("postId") final String postUrl
) {
return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.COMMENT_SEARCH_SUCCESS, postService.getComments(postId, userId)));
return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.COMMENT_SEARCH_SUCCESS, postService.getComments(postId, WriterNameContextUtil.getMoimWriterNameMapContext())));
}


@GetMapping("/{postId}/info/curious")
@Override
@UserAuthAnnotation(UserAuthenticationType.USER)
public ResponseEntity<SuccessResponse<CuriousInfoResponse>> getCuriousInfo(
@PostIdPathVariable final Long postId,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
return ResponseEntity.status(HttpStatus.OK).body(SuccessResponse.of(SuccessMessage.CURIOUS_INFO_SEARCH_SUCCESS, postService.getCuriousInfoOfPost(postId, userId)));
return ResponseEntity.status(HttpStatus.OK).body(SuccessResponse.of(SuccessMessage.CURIOUS_INFO_SEARCH_SUCCESS,
postService.getCuriousInfoOfPost(
postId,
WriterNameContextUtil.getMoimWriterNameMapContext()
)));
}

@DeleteMapping("/{postId}/curious")
@Override
@UserAuthAnnotation(UserAuthenticationType.USER)
public SuccessResponse<PostCuriousResponse> deleteCurious(
@PostIdPathVariable final Long postId,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.CURIOUS_DELETE_SUCCESS, postService.deleteCuriousOnPost(postId, userId));
return SuccessResponse.of(SuccessMessage.CURIOUS_DELETE_SUCCESS,
postService.deleteCuriousOnPost(
postId,
WriterNameContextUtil.getMoimWriterNameMapContext()
));
}

@GetMapping("/{postId}/authenticate")
Expand All @@ -107,13 +118,13 @@ public ResponseEntity<SuccessResponse<PostAuthenticateResponse>> getAuthenticate

@PutMapping("/{postId}")
@Override
@UserAuthAnnotation(UserAuthenticationType.USER)
public ResponseEntity<SuccessResponse> putPost(
@PostIdPathVariable final Long postId,
@Valid @RequestBody final PostPutRequest putRequest,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
postService.updatePost(postId, userId, putRequest);
postService.updatePost(postId, WriterNameContextUtil.getMoimWriterNameMapContext(), putRequest);
return ResponseEntity.status(HttpStatus.OK).body(SuccessResponse.of(SuccessMessage.POST_PUT_SUCCESS));
}

Expand All @@ -130,13 +141,14 @@ public ResponseEntity<SuccessResponse> deletePost(

@Override
@GetMapping("/temporary/{postId}")
@UserAuthAnnotation(UserAuthenticationType.USER)
public SuccessResponse<TemporaryPostGetResponse> getTemporaryPost(
@PostIdPathVariable final Long postId,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.TEMPORARY_POST_GET_SUCCESS,
postService.getTemporaryPost(postId, userId));
postService.getTemporaryPost(postId,
WriterNameContextUtil.getMoimWriterNameMapContext()));
}

@Override
Expand Down Expand Up @@ -176,14 +188,14 @@ public SuccessResponse createTemporaryPost(

@Override
@DeleteMapping("/temporary/{postId}")
@UserAuthAnnotation(UserAuthenticationType.USER)
public ResponseEntity<SuccessResponse> deleteTemporaryPost(
@PostIdPathVariable final Long postId,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
postService.deleteTemporaryPost(
userId,
postId
postId,
WriterNameContextUtil.getMoimWriterNameMapContext()
);
return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.TEMPORARY_POST_DELETE_SUCCESS));
}
Expand All @@ -205,13 +217,13 @@ public ResponseEntity<SuccessResponse<WriterNameResponse>> putTemporaryToFixedPo

@Override
@GetMapping("/modify/{postId}")
@UserAuthAnnotation(UserAuthenticationType.USER)
public SuccessResponse<ModifyPostGetResponse> getPostForModifying(
@PostIdPathVariable final Long postId,
@UserId final Long userId,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.MODIFY_POST_GET_SUCCESS,
postService.getPostForModifying(postId, userId));
postService.getPostForModifying(postId, WriterNameContextUtil.getMoimWriterNameMapContext()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public interface PostControllerSwagger {
SuccessResponse postComment(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Valid @RequestBody final CommentCreateRequest commentCreateRequest,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand All @@ -65,7 +64,6 @@ SuccessResponse postComment(
)
SuccessResponse<PostCuriousResponse> postCurious(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand All @@ -83,7 +81,6 @@ SuccessResponse<PostCuriousResponse> postCurious(
)
ResponseEntity<SuccessResponse<CommentListResponse>> getComments(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand All @@ -100,7 +97,6 @@ ResponseEntity<SuccessResponse<CommentListResponse>> getComments(
)
ResponseEntity<SuccessResponse<CuriousInfoResponse>> getCuriousInfo(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand All @@ -117,7 +113,6 @@ ResponseEntity<SuccessResponse<CuriousInfoResponse>> getCuriousInfo(
)
SuccessResponse<PostCuriousResponse> deleteCurious(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand Down Expand Up @@ -167,7 +162,6 @@ ResponseEntity<SuccessResponse<PostAuthenticateResponse>> getAuthenticateWrite(
ResponseEntity<SuccessResponse> putPost(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@RequestBody final PostPutRequest putRequest,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand All @@ -185,7 +179,6 @@ ResponseEntity<SuccessResponse> putPost(
)
ResponseEntity<SuccessResponse> deleteTemporaryPost(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand Down Expand Up @@ -219,7 +212,6 @@ ResponseEntity<SuccessResponse> deletePost(
)
SuccessResponse<TemporaryPostGetResponse> getTemporaryPost(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);

Expand Down Expand Up @@ -312,7 +304,6 @@ ResponseEntity<SuccessResponse<WriterNameResponse>> putTemporaryToFixedPost(
)
SuccessResponse<ModifyPostGetResponse> getPostForModifying(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId,
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId,
@PathVariable("postId") final String postUrl
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ public void deleteReply(
}

public List<CommentResponse> getCommentResponse(
final Long moimId,
final Post post,
final Long userId
final Long writerNameId
) {
postRetriever.authenticateUserWithPost(post, userId);
List<Comment> commentList = commentRetriever.findByPostId(post.getId());
Long writerNameId = writerNameRetriever.getWriterNameIdByMoimIdAndUserId(moimId, userId);

return commentList.stream()
.map(comment -> CommentResponse.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.mile.writername.service.WriterNameRemover;
import com.mile.writername.service.WriterNameRetriever;
import com.mile.writername.service.WriterNameService;
import com.mile.writername.service.dto.response.WriterNameShortResponse;
import com.mile.writername.service.dto.response.WriterNameInformationResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -84,11 +84,10 @@ public ContentListResponse getContentsFromMoim(
return ContentListResponse.of(topicRetriever.getContentsFromMoim(moimId));
}

public WriterNameShortResponse getWriterNameOfUser(
final Long moimId,
final Long userId
public WriterNameInformationResponse getWriterNameOfUser(
final Long writerNameId
) {
return writerNameRetriever.findWriterNameInfo(moimId, userId);
return writerNameRetriever.findWriterNameInfo(writerNameId);
}

public WriterNameConflictCheckResponse checkConflictOfWriterName(Long moimId, String writerName) {
Expand Down
Loading
Loading