Skip to content
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
38 changes: 38 additions & 0 deletions src/main/java/com/mos/backend/common/auth/StudyMemberSecurity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mos.backend.common.auth;

import com.mos.backend.common.exception.MosException;
import com.mos.backend.common.infrastructure.EntityFacade;
import com.mos.backend.studychatrooms.entity.StudyChatRoom;
import com.mos.backend.users.entity.User;
import com.mos.backend.users.entity.exception.UserErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component("studyMemberSecurity")
@RequiredArgsConstructor
public class StudyMemberSecurity {
private final EntityFacade entityFacade;

public boolean isMemberOrAdmin(long studyChatRoomId) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
isAuthenticated(authentication);

Long currentUserId = Long.valueOf(authentication.getName());
User currentUser = entityFacade.getUser(currentUserId);

StudyChatRoom studyChatRoom = entityFacade.getStudyChatRoom(studyChatRoomId);

if (currentUser.isAdmin()) {
return true;
}
return entityFacade.getStudyMember(currentUserId, studyChatRoom.getStudy().getId()) != null;
}

private static void isAuthenticated(Authentication authentication) {
if (authentication == null || !authentication.isAuthenticated()) {
throw new MosException(UserErrorCode.USER_UNAUTHORIZED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.mos.backend.studychatmessages.application.dto.StudyChatMessageDto;
import com.mos.backend.studychatmessages.application.res.StudyChatMessageRes;
import com.mos.backend.studychatmessages.entity.StudyChatMessage;
import com.mos.backend.studychatmessages.entity.exception.StudyChatMessageErrorCode;
import com.mos.backend.studychatmessages.infrastructure.StudyChatMessageRepository;
import com.mos.backend.studychatmessages.presentation.req.StudyChatMessagePublishReq;
import com.mos.backend.studychatrooms.application.dto.StudyChatRoomInfoMessageDto;
Expand Down Expand Up @@ -64,9 +63,9 @@ private StudyChatMessage saveStudyChatMessage(StudyChatMessagePublishReq req, Us
return studyChatMessageRepository.save(studyChatMessage);
}

@PreAuthorize("@studySecurity.isMemberOrAdmin(#studyId)")
@PreAuthorize("@studyMemberSecurity.isMemberOrAdmin(#studyChatRoomId)")
@Transactional(readOnly = true)
public InfinityScrollRes<StudyChatMessageRes> getStudyChatMessages(Long studyId, Long studyChatRoomId, Long lastStudyChatMessageId, Integer size) {
public InfinityScrollRes<StudyChatMessageRes> getStudyChatMessages(Long studyChatRoomId, Long lastStudyChatMessageId, Integer size) {
StudyChatRoom studyChatRoom = entityFacade.getStudyChatRoom(studyChatRoomId);

List<StudyChatMessage> studyChatMessages = studyChatMessageRepository.findAllByChatRoomIdForInfiniteScroll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ public void publishPrivateChatMessage(Message<?> message,
studyChatMessageService.publish(userId, studyChatRoomId, studyChatMessagePublishReq);
}

@GetMapping("/studies/{studyId}/chat-rooms/{studyChatRoomId}/messages")
public InfinityScrollRes<StudyChatMessageRes> getStudyChatMessages(@PathVariable Long studyId,
@PathVariable Long studyChatRoomId,
@GetMapping("/studies/chat-rooms/{studyChatRoomId}/messages")
public InfinityScrollRes<StudyChatMessageRes> getStudyChatMessages(@PathVariable Long studyChatRoomId,
@RequestParam(required = false) Long lastStudyChatMessageId,
@RequestParam(defaultValue = "10") Integer size) {
return studyChatMessageService.getStudyChatMessages(studyId, studyChatRoomId, lastStudyChatMessageId, size);
return studyChatMessageService.getStudyChatMessages(studyChatRoomId, lastStudyChatMessageId, size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ void getStudyChatMessages_Success() {

// When
InfinityScrollRes<StudyChatMessageRes> result1 = studyChatMessageService.getStudyChatMessages(
study.getId(), studyChatRoom.getId(), lastStudyChatMessageId, size
studyChatRoom.getId(), lastStudyChatMessageId, size
);
InfinityScrollRes<StudyChatMessageRes> result2 = studyChatMessageService.getStudyChatMessages(
study.getId(), studyChatRoom.getId(), result1.getLastElementId(), size
studyChatRoom.getId(), result1.getLastElementId(), size
);

// Then
Expand All @@ -78,4 +78,4 @@ private void saveStudyChatMessages(User user, StudyChatRoom studyChatRoom, int s
}
}
}
}
}