11package org .dfbf .soundlink .domain .chat .service ;
22
3- import jakarta .servlet .http .HttpServletRequest ;
43import lombok .RequiredArgsConstructor ;
54import org .dfbf .soundlink .domain .chat .dto .ChatReqDto ;
65import org .dfbf .soundlink .domain .chat .entity .ChatRoom ;
76import org .dfbf .soundlink .domain .chat .exception .ChatRoomNotFoundException ;
7+ import org .dfbf .soundlink .domain .chat .exception .UnauthorizedAccessException ;
88import org .dfbf .soundlink .domain .chat .repository .ChatRoomRepository ;
99import org .dfbf .soundlink .domain .emotionRecord .entity .EmotionRecord ;
1010import org .dfbf .soundlink .domain .emotionRecord .exception .EmotionRecordNotFoundException ;
1818import org .dfbf .soundlink .global .exception .ResponseResult ;
1919import org .springframework .dao .DataIntegrityViolationException ;
2020import org .springframework .data .redis .core .RedisTemplate ;
21+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
2122import org .springframework .stereotype .Service ;
2223import org .springframework .transaction .annotation .Transactional ;
2324
@@ -31,14 +32,11 @@ public class ChatRoomService {
3132 private final UserRepository userRepository ;
3233 private final EmotionRecordRepository emotionRecordRepository ;
3334 private final RedisTemplate <String , String > redisTemplate ;
34- private final JwtProvider jwtProvider ;
35+
3536
3637 @ Transactional
37- public ResponseResult createChatRoom (HttpServletRequest request , Long recordId ){
38+ public ResponseResult createChatRoom (@ AuthenticationPrincipal Long userId , Long recordId ){
3839 try {
39- String accessToken = jwtProvider .resolveAccessToken (request ); //AT 추출
40- Long userId = jwtProvider .getUserId (accessToken );
41-
4240 //요청 보내는사람
4341 User requestUserId = userRepository .findById (userId )
4442 .orElseThrow (UserNotFoundException ::new );
@@ -65,7 +63,8 @@ public ResponseResult createChatRoom(HttpServletRequest request, Long recordId){
6563 redisTemplate .opsForValue ().set ("Room::" +chatRoom .getChatRoomId (), String .valueOf (chatReqDto ));
6664
6765 return new ResponseResult (ErrorCode .SUCCESS , chatRoom );
68- }catch (DataIntegrityViolationException e ) {
66+ }
67+ catch (DataIntegrityViolationException e ) {
6968 return new ResponseResult (ErrorCode .CHAT_FAILED , "채팅방 생성 실패: 이미 존재하는 데이터입니다." ); // recordId 값 중복 시
7069 } catch (Exception e ) {
7170 return new ResponseResult (ErrorCode .INTERNAL_SERVER_ERROR , e .getMessage ());
@@ -75,10 +74,17 @@ public ResponseResult createChatRoom(HttpServletRequest request, Long recordId){
7574
7675 //채팅방 닫기
7776 @ Transactional
78- public ResponseResult closeChatRoom (Long chatRoomId ) {
77+ public ResponseResult closeChatRoom (@ AuthenticationPrincipal Long userId , Long chatRoomId ) {
7978 try {
8079 ChatRoom chatRoom = chatRoomRepository .findById (chatRoomId )
8180 .orElseThrow (ChatRoomNotFoundException ::new );
81+
82+ //요청자 또는 응답자가 아니면 예외 처리
83+ if (!chatRoom .getRequestUserId ().getUserId ().equals (userId ) &&
84+ !chatRoom .getRecordId ().getUser ().getUserId ().equals (userId )) {
85+ throw new UnauthorizedAccessException ();//권한이 없을 경우 예외 발생
86+ }
87+
8288 chatRoom .updateChatRoomStatus (RoomStatus .CLOSED ); //삳태 '닫기'로 변경
8389 chatRoomRepository .save (chatRoom );//DB에 저장
8490
0 commit comments