fix: conflict 수정 후 merge

This commit is contained in:
kgc9007 2024-07-31 11:27:00 +09:00
commit ca821ca3e3
8 changed files with 164 additions and 38 deletions

View File

@ -29,5 +29,7 @@ public interface LectureService {
void changeState(Long lectureId);
boolean getState(Long lectureId);
boolean checkTeacher(Long userId, Long lectureId);
}

View File

@ -238,6 +238,14 @@ public class LectureServiceImpl implements LectureService {
lectureRepository.save(l);
}
@Override
public boolean getState(Long lectureId) {
Lecture lecture= lectureRepository.findById(lectureId).orElse(null);
return lecture.isOnline();
}
@Override
public boolean checkTeacher(Long userId, Long lectureId) {
Optional<Lecture> lecture = lectureRepository.findById(lectureId);

View File

@ -3,6 +3,8 @@ package com.edufocus.edufocus.user.controller;
import com.edufocus.edufocus.user.model.entity.InfoDto;
import com.edufocus.edufocus.user.model.entity.PasswordDto;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.exception.ExpriedTokenException;
import com.edufocus.edufocus.user.model.exception.UnAuthorizedException;
import com.edufocus.edufocus.user.model.service.UserService;
import com.edufocus.edufocus.user.util.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
@ -258,7 +260,15 @@ public class UserController {
}
@ExceptionHandler(ExpriedTokenException.class)
public ResponseEntity<?> handleExpiredTokenException(){
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(UnAuthorizedException.class)
public ResponseEntity<?> handleUnauthorizedException(){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

View File

@ -0,0 +1,11 @@
package com.edufocus.edufocus.user.model.exception;
public class ExpriedTokenException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ExpriedTokenException() {
super("계정 권한이 유효하지 않습니다.\n다시 로그인을 하세요.");
}
}

View File

@ -0,0 +1,25 @@
package com.edufocus.edufocus.user.model.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(InvalidTokenException.class)
public ResponseEntity<String> handleUnAuthorizedException(InvalidTokenException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(ExpriedTokenException.class)
public ResponseEntity<String> handleInvalidTokenException(ExpriedTokenException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED);
}
}

View File

@ -0,0 +1,7 @@
package com.edufocus.edufocus.user.model.exception;
public class InvalidTokenException extends RuntimeException {
public InvalidTokenException() {
super("Token is invalid");
}
}

View File

@ -4,6 +4,8 @@ import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;
import com.edufocus.edufocus.user.model.exception.ExpriedTokenException;
import com.edufocus.edufocus.user.model.exception.InvalidTokenException;
import com.edufocus.edufocus.user.model.exception.UnAuthorizedException;
import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Value;
@ -52,19 +54,27 @@ public class JWTUtil {
public boolean checkToken(String token) {
try {
Jws<Claims> claims = Jwts.parserBuilder()
.setSigningKey(generateKey())
.build()
.parseClaimsJws(token);
log.debug("claims: {}", claims);
return true;
} catch (MalformedJwtException | UnsupportedJwtException | IllegalArgumentException | SignatureException | ExpiredJwtException e) {
log.error("Token validation error: {}", e.getMessage());
return false;
} catch (Exception e) {
System.out.println(token);
log.error("Unexpected error while validating token: {}", e.getMessage());
return false;
Jws<Claims> claims = Jwts.parserBuilder()
.setSigningKey(generateKey())
.build()
.parseClaimsJws(token);
log.debug("claims: {}", claims);
return true;
} catch (MalformedJwtException | UnsupportedJwtException | IllegalArgumentException | SignatureException e) {
log.error("Token validation error: {}", e.getMessage());
return false;
}
catch ( ExpiredJwtException e)
{
throw new ExpriedTokenException();
}
catch (Exception e) {
System.out.println(token);
System.out.println(e.getMessage());
log.error("Unexpected error while validating token: {}", e.getMessage());
return false;
}
}
@ -77,9 +87,15 @@ public class JWTUtil {
Map<String, Object> value = claims.getBody();
log.info("value : {}", value);
return (String) value.get("id");
} catch (Exception e) {
}catch ( ExpiredJwtException e)
{
System.out.println("expired token");
throw new ExpriedTokenException();
}
catch (Exception e) {
log.error("Failed to get user ID from token: {}", e.getMessage());
throw new UnAuthorizedException();
throw new InvalidTokenException();
}
}
}

View File

@ -4,9 +4,13 @@ import java.util.Map;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.lecture.service.LectureService;
import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import com.edufocus.edufocus.registration.service.RegistrationService;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.entity.UserRole;
import com.edufocus.edufocus.user.model.repository.UserRepository;
import com.edufocus.edufocus.user.model.service.UserService;
import com.edufocus.edufocus.user.util.JWTUtil;
import com.edufocus.edufocus.video.service.VideoSertvice;
@ -33,6 +37,8 @@ public class Controller {
private final LectureService lectureService;
private final VideoSertvice videoSertvice;
private final RegistrationService registrationService;
private final UserRepository userRepository;
private final LectureRepository lectureRepository;
@Value("${livekit.api.key}")
private String LIVEKIT_API_KEY;
@ -63,46 +69,58 @@ public class Controller {
// }
@PostMapping(value = "/makeroom/{lecture_id}")
public ResponseEntity<Map<String, String>> startLecture(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
public ResponseEntity<String> startLecture(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
String userToken = request.getHeader("Authorization");
log.info("userToekn : ", userToken);
Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
LectureDetailResponse lecture= lectureService.findLectureById(userId,id);
String roomName = lecture.getTitle();
String participantName = userService.getUserName(userId);
AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
token.setName(participantName);
token.setIdentity(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName),new RoomCreate(true));
videoSertvice.startOnline(userId, id);
videoSertvice.startOnline(userId,id);
return new ResponseEntity<>("방만들기 성공",HttpStatus.OK);
return ResponseEntity.ok(Map.of("token", token.toJwt()));
}
@PostMapping(value = "/joinroom/{lecture_id}")
public ResponseEntity<Map<String, String>> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception {
public ResponseEntity<Map<String, String>> joinRoom(@PathVariable("lecture_id") Long id, HttpServletRequest request) throws Exception
{
String userToken = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
LectureDetailResponse lecture= lectureService.findLectureById(userId,id);
User findUser= userRepository.findById(userId).orElse(null);
Lecture lecture= lectureRepository.findById(id).orElse(null);
//RegistrationStatus registrationStatus = registrationService.isOnline(userId,id);
// if(registrationStatus==RegistrationStatus.ACCEPTED)
// {
if(findUser.getRole()==UserRole.ADMIN && lecture.isOnline() ) {
String roomName = lecture.getTitle();
String participantName = userService.getUserName(userId);
AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
token.setName(participantName);
token.setIdentity(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName), new RoomCreate(true));
videoSertvice.startOnline(userId, id);
return ResponseEntity.ok(Map.of("token", token.toJwt()));
}
else if(findUser.getRole()==UserRole.STUDENT && lecture.isOnline() )
{
String roomName = lecture.getTitle();
String participantName = userService.getUserName(userId);
@ -112,11 +130,40 @@ public class Controller {
token.setIdentity(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName));
//videoSertvice.startOnline(userId,id);
videoSertvice.startOnline(userId, id);
return ResponseEntity.ok(Map.of("token", token.toJwt()));
}
return ResponseEntity.ok(Map.of("token", null));
// String userToken = request.getHeader("Authorization");
//
// Long userId = Long.parseLong(jwtUtil.getUserId(userToken));
// LectureDetailResponse lecture= lectureService.findLectureById(userId,id);
//
//
// //RegistrationStatus registrationStatus = registrationService.isOnline(userId,id);
//
// if(registrationStatus==RegistrationStatus.ACCEPTED)
// {
// String roomName = lecture.getTitle();
// String participantName = userService.getUserName(userId);
//
//
// AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
// token.setName(participantName);
// token.setIdentity(participantName);
// token.addGrants(new RoomJoin(true), new RoomName(roomName));
//
// //videoSertvice.startOnline(userId,id);
//
//
//
// return ResponseEntity.ok(Map.of("token", token.toJwt()));
// }
// else{
// return ResponseEntity.status(HttpStatus.FORBIDDEN).body(Map.of("errorMessage", "Not accepted"));