Skip to content

Commit

Permalink
Merge pull request #93 from KUIT-Space/refactoring/#88/voiceroom-custโ€ฆ
Browse files Browse the repository at this point in the history
โ€ฆom-exception-resolver-logging
  • Loading branch information
drbug2000 authored Aug 13, 2024
2 parents 9815d8f + b50df26 commit f2d3d74
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public enum UserSpaceValidationInterceptorURL {
//SPACE("/space/**"),
TEST("/space/{spaceId}/test/**"),
VOICEROOM("/space/{spaceId}/voiceRoom/**")
;

private final String urlPattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuth;
import space.space_spring.dao.UserSpaceDao;
import space.space_spring.dao.VoiceRoomRepository;
import space.space_spring.dto.VoiceRoom.*;
import space.space_spring.entity.Space;
import space.space_spring.entity.User;
import space.space_spring.entity.UserSpace;
import space.space_spring.exception.VoiceRoomException;

import space.space_spring.exception.CustomException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.LiveKitService;
import space.space_spring.service.VoiceRoomService;
Expand Down Expand Up @@ -47,15 +46,15 @@ public BaseResponse<PostVoiceRoomDto.Response> createRoom(
@PathVariable("spaceId") @NotNull long spaceId,
@JwtLoginAuth Long userId,
@Validated @RequestBody PostVoiceRoomDto.Request voiceRoomRequest,
@UserSpaceAuth String userSpaceAuth,
BindingResult bindingResult){
if(bindingResult.hasErrors()){
throw new VoiceRoomException(INVALID_VOICEROOM_REQUEST,getErrorMessage(bindingResult));
throw new CustomException(INVALID_VOICEROOM_REQUEST,getErrorMessage(bindingResult));
}

//ํ•ด๋‹น ์œ ์ €๊ฐ€ voice์ด ์žˆ๋Š” space์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€(๊ถŒํ•œ์ด ์žˆ๋Š”์ง€) ํ™•์ธ
validateIsUserInSpace(spaceId,userId);
//ํ•ด๋‹น ์œ ์ €๊ฐ€ ํ˜„์žฌ space์— ๋Œ€ํ•ด ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
validateManagerPermission(spaceId,userId);
validateManagerPermission(userSpaceAuth);

//Todo response ๋‚ด์šฉ์„ ๋ฌด์—‡์„ ์ฃผ๋ฉด ์ข‹์„์ง€ ( POST response ์ „์ฒด ๊ธฐ๋Šฅ ํ†ต์ผ ํ•˜๋Š” ๊ฒƒ์ผ ์ข‹์•„๋ณด์ž„ )
PostVoiceRoomDto.Response res = new PostVoiceRoomDto.Response(voiceRoomService.createVoiceRoom(spaceId,voiceRoomRequest));
return new BaseResponse<>(res);
Expand All @@ -72,8 +71,6 @@ public BaseResponse<GetVoiceRoomList.Response> getRoomList(

boolean showParticipantValue = (showParticipant != null) ? showParticipant : false;

//ํ•ด๋‹น ์œ ์ €๊ฐ€, voiceRoom์ด ์žˆ๋Š” space์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€(๊ถŒํ•œ์ด ์žˆ๋Š”์ง€) ํ™•์ธ
validateIsUserInSpace(spaceId,userId);

GetVoiceRoomList.Request voiceRoomList=new GetVoiceRoomList.Request(limit, showParticipant);

Expand All @@ -89,8 +86,7 @@ public BaseResponse<String> getToken(
@PathVariable("voiceRoomId") @NotNull Long roomId,
HttpServletResponse response
){
//ํ•ด๋‹น ์œ ์ €๊ฐ€, voiceRoom์ด ์žˆ๋Š” space์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€(๊ถŒํ•œ์ด ์žˆ๋Š”์ง€) ํ™•์ธ
validateIsUserInSpace(spaceId,userId);

//ํ•ด๋‹น voiceRoomId๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
validateVoiceRoom(roomId);
//ํ•ด๋‹น voiceRoom์ด ํ•ด๋‹น space์— ์†ํ•œ๊ฒƒ์ด ๋งž๋Š”์ง€ ํ™•์ธ
Expand All @@ -109,8 +105,7 @@ public BaseResponse<GetParticipantList.Response> getParticipants(
@JwtLoginAuth Long userId,
@PathVariable("voiceRoomId") @NotNull Long roomId
){
//ํ•ด๋‹น ์œ ์ €๊ฐ€ voice์ด ์žˆ๋Š” space์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€(๊ถŒํ•œ์ด ์žˆ๋Š”์ง€) ํ™•์ธ
validateIsUserInSpace(spaceId,userId);

//ํ•ด๋‹น voiceRoomId๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
validateVoiceRoom(roomId);
//ํ•ด๋‹น voiceRoom์ด ํ•ด๋‹น space์— ์†ํ•œ๊ฒƒ์ด ๋งž๋Š”์ง€ ํ™•์ธ
Expand All @@ -125,16 +120,15 @@ public BaseResponse<String> updateVoiceRoom(
@PathVariable("spaceId") @NotNull long spaceId,
@JwtLoginAuth Long userId,
@Validated @RequestBody PatchVoiceRoom patchVoiceRoom,
@UserSpaceAuth String userSpaceAuth,
BindingResult bindingResult
){

if(bindingResult.hasErrors()){
throw new VoiceRoomException(INVALID_VOICEROOM_REQUEST,getErrorMessage(bindingResult));
throw new CustomException(INVALID_VOICEROOM_REQUEST,getErrorMessage(bindingResult));
}
//ํ•ด๋‹น ์œ ์ €๊ฐ€ voice์ด ์žˆ๋Š” space์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€(๊ถŒํ•œ์ด ์žˆ๋Š”์ง€) ํ™•์ธ
validateIsUserInSpace(spaceId,userId);
//ํ•ด๋‹น ์œ ์ €๊ฐ€ ํ˜„์žฌ space์— ๋Œ€ํ•ด ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
validateManagerPermission(spaceId,userId);
validateManagerPermission(userSpaceAuth);
//ํ•ด๋‹น voiceRoom์ด ํ•ด๋‹น space์— ์†ํ•œ๊ฒƒ์ด ๋งž๋Š”์ง€ ํ™•์ธ
for(PatchVoiceRoom.UpdateRoom updateRoom : patchVoiceRoom.getUpdateRoomList()) {
validateVoiceRoomInSpace(spaceId, updateRoom.getRoomId());
Expand All @@ -149,12 +143,12 @@ public BaseResponse<String> updateVoiceRoom(
public BaseResponse<String> deleteVoiceRoom(
@PathVariable("spaceId") @NotNull long spaceId,
@JwtLoginAuth Long userId,
@PathVariable("voiceRoomId") @NotNull Long voiceRoomId
@PathVariable("voiceRoomId") @NotNull Long voiceRoomId,
@UserSpaceAuth String userSpaceAuth
){
//ํ•ด๋‹น ์œ ์ €๊ฐ€ voice์ด ์žˆ๋Š” space์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€(๊ถŒํ•œ์ด ์žˆ๋Š”์ง€) ํ™•์ธ
validateIsUserInSpace(spaceId,userId);

//ํ•ด๋‹น ์œ ์ €๊ฐ€ ํ˜„์žฌ space์— ๋Œ€ํ•ด ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
validateManagerPermission(spaceId,userId);
validateManagerPermission(userSpaceAuth);
//ํ•ด๋‹น voiceRoom์ด ํ•ด๋‹น space์— ์†ํ•œ๊ฒƒ์ด ๋งž๋Š”์ง€ ํ™•์ธ
validateVoiceRoomInSpace(spaceId, voiceRoomId);

Expand All @@ -169,43 +163,29 @@ public BaseResponse<String> postRoomStatus(){
return new BaseResponse<String>(null);
}

private void validateIsUserInSpace( Long spaceId,Long userId) {
// ์œ ์ €๊ฐ€ ์ŠคํŽ˜์ด์Šค์— ์†ํ•  ๊ฒฝ์šฐ exception์ด ํ„ฐ์ง€์ง€ ์•Š์„ ๊ฒƒ์ž„
// ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ, USER_IS_NOT_IN_SPACE ์˜ˆ์™ธ๊ฐ€ ํ„ฐ์งˆ ๊ฒƒ์ž„ -> ์ถ”ํ›„ exception handling ๊ณผ์ • ํ•„์š”

//ํ˜„์žฌ๋Š” ์ŠคํŽ˜์ด์Šค ์ ‘๊ทผ ๊ถŒํ•œ์„ ์ผ๊ด„์ ์œผ๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
//๋ถ„๋ฆฌ ๊ฐ€๋Šฅ์„ฑ ๋ฐ ํšจ์šฉ์„ฑ ๊ฒ€ํ†  ํ•„์š”
userSpaceUtils.isUserInSpace(userId, spaceId);
}
private boolean validateVoiceRoom(long voiceRoomId){
//Todo ํ•ด๋‹น ๋ณด์ด์Šค๋ฃธ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
if(!voiceRoomRepository.existsByVoiceRoomId(voiceRoomId)){
throw new VoiceRoomException(VOICEROOM_NOT_EXIST);
throw new CustomException(VOICEROOM_NOT_EXIST);
}
return true;
}
private boolean validateVoiceRoomNameExist(String voiceRoomName){
if(!voiceRoomRepository.existsByName(voiceRoomName)){
throw new VoiceRoomException(VOICEROOM_NAME_ALREADY_EXIST);
throw new CustomException(VOICEROOM_NAME_ALREADY_EXIST);
}
return true;
}
private boolean validateVoiceRoomInSpace(long spaceId,long voiceRoomId){
if(! (voiceRoomRepository.findById(voiceRoomId).getSpace().getSpaceId().equals(spaceId))){
throw new VoiceRoomException(VOICEROOM_NOT_IN_SPACE);
throw new CustomException(VOICEROOM_NOT_IN_SPACE);
}
return true;
}
private boolean validateManagerPermission(long spaceId,long userId){
private boolean validateManagerPermission(String userSpaceAuth){
//ํ•ด๋‹น ์œ ์ €๊ฐ€ ํ˜„์žฌ space์— ๋Œ€ํ•ด ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
//TODO ๊ถŒํ•œ ํ™•์ธ ๊ณผ์ •์„ ์ผ๊ด„์ ์œผ๋กœ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•ด ๋ณด์ž„
User user = userUtils.findUserByUserId(userId);
Space space = spaceUtils.findSpaceBySpaceId(spaceId);
//์ด๋ฏธ userSpace ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•ด์„œ null ๊ฒ€์‚ฌ๋Š” ์ƒ๋žตํ•จ

if(!userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceAuth().toString().equals(MANAGER.getAuth())){
System.out.print("Author :" +userSpaceDao.findUserSpaceByUserAndSpace(user,space).get().getUserSpaceAuth().toString());
throw new VoiceRoomException(VOICEROOM_DO_NOT_HAVE_PERMISSION);
if(!userSpaceAuth.equals(MANAGER.getAuth())){
throw new CustomException(VOICEROOM_DO_NOT_HAVE_PERMISSION);
}
return true;
}
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/space/space_spring/exception/VoiceRoomException.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private Long getUserSpace(long spaceId,long userId){
}
Optional<UserSpace> userSpace = userSpaceDao.findUserSpaceByUserAndSpace(userByUserId, spaceBySpaceId);
Optional.ofNullable(userSpace
.orElseThrow(() -> new UserSpaceException(USER_IS_NOT_IN_SPACE)));
.orElseThrow(() -> new CustomException(USER_IS_NOT_IN_SPACE)));
return userSpace.get().getUserSpaceId();
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/space/space_spring/jwt/JwtLoginProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import space.space_spring.entity.User;
import space.space_spring.exception.CustomException;
import space.space_spring.exception.jwt.bad_request.JwtUnsupportedTokenException;
import space.space_spring.exception.jwt.unauthorized.JwtInvalidTokenException;
import space.space_spring.exception.jwt.unauthorized.JwtMalformedTokenException;
Expand Down Expand Up @@ -56,10 +57,13 @@ public boolean isExpiredToken(String accessToken) {
throw new JwtMalformedTokenException(MALFORMED_TOKEN);
} catch (IllegalArgumentException e) {
throw new JwtInvalidTokenException(INVALID_TOKEN);
} catch (JwtException e) {
} catch (SignatureException e){
throw new CustomException(WRONG_SIGNATURE_JWT);
}catch (JwtException e) {
log.error("[JwtTokenProvider.validateAccessToken]", e);
throw e;
}

}

public Long getUserIdFromToken(String accessToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
EXPIRED_TOKEN(4005, HttpStatus.UNAUTHORIZED, "๋งŒ๋ฃŒ๋œ ํ† ํฐ์ž…๋‹ˆ๋‹ค."),
TOKEN_MISMATCH(4006, HttpStatus.UNAUTHORIZED, "๋กœ๊ทธ์ธ ์ •๋ณด๊ฐ€ ํ† ํฐ ์ •๋ณด์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
CANNOT_FIND_USER_ID(4007, HttpStatus.UNAUTHORIZED,"ํ† ํฐ์˜ userId์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
WRONG_SIGNATURE_JWT(4008,HttpStatus.UNAUTHORIZED,"JWT ์„œ๋ช…์ด ์ž˜๋ชป ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."),

/**
* 5000: User ์˜ค๋ฅ˜
Expand Down

0 comments on commit f2d3d74

Please sign in to comment.