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

[Bugfix/#282] 게시글 관련 권한문제 해결 #307

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public enum MenuType {
// 장학회
SCHOLARSHIP(ANONYMOUS, SECRETARY, ANONYMOUS, ADMIN, ADMIN),
// 후원 내용
SPONSOR(ANONYMOUS, EXECUTIVES, ANONYMOUS, BASIC, ANONYMOUS),
SPONSOR(ANONYMOUS, SECRETARY, ANONYMOUS, BASIC, ANONYMOUS),
// 사용 내역
USAGE(ANONYMOUS, EXECUTIVES, ANONYMOUS, BASIC, ANONYMOUS),
USAGE(ANONYMOUS, SECRETARY, ANONYMOUS, BASIC, ANONYMOUS),

// 관리자가 추가, 삭제 가능한 메뉴.(NormalBoard 연관)
LIST(ANONYMOUS, ANONYMOUS, ANONYMOUS, ANONYMOUS, ANONYMOUS), // 리스트형 게시판 메뉴
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public interface NormalBoardRepositoryCustom {

List<NormalBoardDto> findAllByTypeAndIsPinned(NormalBoardType boardType);

List<NormalBoardDto> findAllByMemberIdAndTypeAndSearch(
Long memberId, NormalBoardType boardType, String search);

List<NormalBoardDto> findAllByTypeAndSearch(NormalBoardType boardType, String search);

Optional<NormalBoard> findByMemberIdAndTypeAndId(
Long memberId, NormalBoardType boardType, Long boardId);

Optional<NormalBoard> findByTypeAndId(NormalBoardType boardType, Long boardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,6 @@ public List<NormalBoardDto> findAllByTypeAndIsPinned(NormalBoardType boardType)
.fetch();
}

@Override
public List<NormalBoardDto> findAllByMemberIdAndTypeAndSearch(
Long memberId, NormalBoardType boardType, String search) {
return queryFactory
.select(
Projections.constructor(
NormalBoardDto.class,
normalBoard.id,
normalBoard.title.value,
normalBoard.writer.id,
normalBoard.writer.name.value,
normalBoard.datePinExpiration,
normalBoard.dateCreated,
normalBoard.dateUpdated,
normalBoard.isPinned))
.from(normalBoard)
.where(
eqMemberId(memberId)
.and(eqNormalBoardType(boardType))
.and(likeTitle(search).or(likeContent(search))))
.orderBy(normalBoard.dateCreated.desc())
.fetch();
}

@Override
public List<NormalBoardDto> findAllByTypeAndSearch(NormalBoardType boardType, String search) {
return queryFactory
Expand All @@ -87,20 +63,6 @@ public List<NormalBoardDto> findAllByTypeAndSearch(NormalBoardType boardType, St
.fetch();
}

@Override
public Optional<NormalBoard> findByMemberIdAndTypeAndId(
Long memberId, NormalBoardType boardType, Long boardId) {
return Optional.ofNullable(
queryFactory
.selectFrom(normalBoard)
.where(
eqMemberId(memberId)
.and(eqNormalBoardType(boardType))
.and(normalBoard.id.eq(boardId)))
.orderBy(normalBoard.dateCreated.desc())
.fetchOne());
}

@Override
public Optional<NormalBoard> findByTypeAndId(NormalBoardType boardType, Long boardId) {
return Optional.ofNullable(
Expand All @@ -111,10 +73,6 @@ public Optional<NormalBoard> findByTypeAndId(NormalBoardType boardType, Long boa
.fetchOne());
}

private BooleanExpression eqMemberId(Long memberId) {
return normalBoard.writer.id.eq(memberId);
}

private BooleanExpression eqNormalBoardType(NormalBoardType normalBoardType) {
return normalBoard.menu.id.eq(normalBoardType.getMenuId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface NormalBoardService {

List<NormalBoardDto> getPosts(NormalBoardType boardType, String search);

NormalBoardDetailDto getPost(Long memberId, NormalBoardType boardType, Long boardId);
NormalBoardDetailDto getPost(NormalBoardType boardType, Long boardId);

Long write(Long memberId, NormalBoardType normalBoardType, SaveNormalBoardDto saveNormalBoardDto);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.inhabas.api.domain.normalBoard.usecase;

import static com.inhabas.api.domain.board.domain.PinOption.*;
import static com.inhabas.api.domain.normalBoard.domain.NormalBoardType.*;
import static com.inhabas.api.domain.normalBoard.domain.NormalBoardType.EXECUTIVE;
import static com.inhabas.api.domain.normalBoard.domain.NormalBoardType.NOTICE;

Expand All @@ -11,11 +10,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inhabas.api.auth.domain.error.authException.InvalidAuthorityException;
import com.inhabas.api.auth.domain.error.businessException.InvalidInputException;
import com.inhabas.api.auth.domain.error.businessException.NotFoundException;
import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
Expand Down Expand Up @@ -62,35 +59,16 @@ public List<NormalBoardDto> getPinned(NormalBoardType boardType) {
@Override
@Transactional(readOnly = true)
public List<NormalBoardDto> getPosts(NormalBoardType boardType, String search) {
List<NormalBoardDto> normalBoardList = new ArrayList<>();
if (boardType.equals(SUGGEST)) {
if (SecurityContextHolder.getContext() == null) {
throw new InvalidAuthorityException();
}
Long memberId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
normalBoardList.addAll(
normalBoardRepository.findAllByMemberIdAndTypeAndSearch(memberId, boardType, search));
} else {
normalBoardList.addAll(normalBoardRepository.findAllByTypeAndSearch(boardType, search));
}
return normalBoardList;
return normalBoardRepository.findAllByTypeAndSearch(boardType, search);
}

@Override
@Transactional(readOnly = true)
public NormalBoardDetailDto getPost(Long memberId, NormalBoardType boardType, Long boardId) {
NormalBoard normalBoard;
if (boardType.equals(SUGGEST)) {
normalBoard =
normalBoardRepository
.findByMemberIdAndTypeAndId(memberId, boardType, boardId)
.orElseThrow(NotFoundException::new);
} else {
normalBoard =
normalBoardRepository
.findByTypeAndId(boardType, boardId)
.orElseThrow(NotFoundException::new);
}
public NormalBoardDetailDto getPost(NormalBoardType boardType, Long boardId) {
NormalBoard normalBoard =
normalBoardRepository
.findByTypeAndId(boardType, boardId)
.orElseThrow(NotFoundException::new);

ClassifiedFiles classifiedFiles = ClassifyFiles.classifyFiles(normalBoard.getFiles());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ public ResponseEntity<PagedPinnedResponseDto<NormalBoardDto>> getBoardList(
"{\"status\": 404, \"code\": \"G004\", \"message\": \"데이터가 존재하지 않습니다.\"}")))
})
public ResponseEntity<NormalBoardDetailDto> getBoard(
@PathVariable Long boardId,
@PathVariable NormalBoardType boardType,
@Authenticated Long memberId) {
@PathVariable Long boardId, @PathVariable NormalBoardType boardType) {

return ResponseEntity.ok(normalBoardService.getPost(memberId, boardType, boardId));
return ResponseEntity.ok(normalBoardService.getPost(boardType, boardId));
}

@Operation(summary = "게시글 추가")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,6 @@ public void save() {
assertThat(saveBoard.getWriter()).isEqualTo(writer);
}

@DisplayName("memberId, type, search 로 게시글 목록 조회한다.")
@Test
public void findAllByMemberIdAndTypeAndSearch() {
// given
NormalBoard saveBoard = normalBoardRepository.save(NOTICE_BOARD);
NormalBoard saveBoard2 = normalBoardRepository.save(NOTICE_BOARD_2);
Long writerId = writer.getId();

// when
List<NormalBoardDto> dtoList =
normalBoardRepository.findAllByMemberIdAndTypeAndSearch(writerId, NOTICE, "");

// then
assertThat(dtoList).hasSize(2);
assertThat(dtoList.get(0).getTitle()).isEqualTo(saveBoard.getTitle());
}

@DisplayName("type, search 로 게시글 목록 조회한다.")
@Test
public void findAllByTypeAndSearch() {
Expand All @@ -106,24 +89,6 @@ public void findAllByTypeAndSearch() {
assertThat(dtoList.get(0).getTitle()).isEqualTo(saveBoard.getTitle());
}

@DisplayName("memberId, type, id 로 게시글 상세 조회한다.")
@Test
public void findByMemberIdAndTypeAndId() {
// given
NormalBoard saveBoard = normalBoardRepository.save(NOTICE_BOARD);
Long writerId = writer.getId();

// when
NormalBoard normalBoard =
normalBoardRepository
.findByMemberIdAndTypeAndId(writerId, NOTICE, saveBoard.getId())
.orElse(null);

// then
assertThat(normalBoard).isNotNull();
assertThat(normalBoard.getTitle()).isEqualTo(saveBoard.getTitle());
}

@DisplayName("type, id 로 게시글 상세 조회한다.")
@Test
public void findByTypeAndId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void getPost() {
given(normalBoardRepository.findByTypeAndId(any(), any())).willReturn(Optional.of(normalBoard));

// when
NormalBoardDetailDto dto = normalBoardService.getPost(1L, NOTICE, 1L);
NormalBoardDetailDto dto = normalBoardService.getPost(NOTICE, 1L);

// then
assertThat(dto.getTitle()).isEqualTo(normalBoard.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void getBoard() throws Exception {
.dateUpdated(LocalDateTime.now())
.isPinned(false)
.build();
given(normalBoardService.getPost(any(), any(), any())).willReturn(normalBoardDetailDto);
given(normalBoardService.getPost(any(), any())).willReturn(normalBoardDetailDto);

// when
String response =
Expand Down Expand Up @@ -165,7 +165,7 @@ void getBoard_Invalid_Input() throws Exception {
@Test
void getBoard_Not_Found() throws Exception {
// given
doThrow(NotFoundException.class).when(normalBoardService).getPost(any(), any(), any());
doThrow(NotFoundException.class).when(normalBoardService).getPost(any(), any());

// when
String response =
Expand Down
Loading