Skip to content

Commit

Permalink
Merge pull request #92 from KUIT-Space/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
drbug2000 authored Aug 13, 2024
2 parents af8f545 + 9815d8f commit 8983a5b
Show file tree
Hide file tree
Showing 39 changed files with 804 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package space.space_spring.argument_resolver.jwtLogin;
package space.space_spring.argumentResolver.jwtLogin;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package space.space_spring.argument_resolver.jwtLogin;
package space.space_spring.argumentResolver.jwtLogin;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package space.space_spring.argumentResolver.userSpace;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckUserSpace {
boolean required() default true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package space.space_spring.argumentResolver.userSpace;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserSpaceAuth {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package space.space_spring.argumentResolver.userSpace;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dao.UserSpaceDao;

@Component
@RequiredArgsConstructor
public class UserSpaceAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {

private final UserSpaceDao userSpaceDao;
@Override
public boolean supportsParameter(MethodParameter parameter) {
// ์ผ๋‹จ parameter์˜ return value type ์„ ๊ฒ€์‚ฌํ•˜์ง€๋Š” X
return parameter.hasParameterAnnotation(UserSpaceAuth.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
return
userSpaceDao.findUserSpaceAuthById((Long)request.getAttribute("userSpaceId"));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package space.space_spring.argumentResolver.userSpace;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserSpaceId {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package space.space_spring.argumentResolver.userSpace;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;

@Component
public class UserSpaceIdHandlerArgumentResolver implements HandlerMethodArgumentResolver {

@Override
public boolean supportsParameter(MethodParameter parameter) {
// ์ผ๋‹จ parameter์˜ return value type ์„ ๊ฒ€์‚ฌํ•˜์ง€๋Š” X
return parameter.hasParameterAnnotation(UserSpaceId.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
return request.getAttribute("userSpaceId");
}
}
3 changes: 2 additions & 1 deletion src/main/java/space/space_spring/config/InterceptorURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public enum InterceptorURL {
SPACE("/space/**"),
TEST("/test/**"),
SPACE_LIST_FOR_USER("/user/space-choice"),
VOICE_ROOM("/voiceRoom/**");
VOICE_ROOM("/voiceRoom/**"),
USER_PROFILE_LIST("/user/profile");

private final String urlPattern;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package space.space_spring.config;

import lombok.Getter;

@Getter
public enum UserSpaceValidationInterceptorURL {
//SPACE("/space/**"),
TEST("/space/{spaceId}/test/**"),
;

private final String urlPattern;

UserSpaceValidationInterceptorURL(String urlPattern) {
this.urlPattern = urlPattern;
}
}
25 changes: 22 additions & 3 deletions src/main/java/space/space_spring/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuthHandlerArgumentResolver;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuthHandlerArgumentResolver;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuthHandlerArgumentResolver;
import space.space_spring.argumentResolver.userSpace.UserSpaceIdHandlerArgumentResolver;
import space.space_spring.interceptor.UserSpaceValidationInterceptor;
import space.space_spring.interceptor.jwtLogin.JwtLoginAuthInterceptor;

import java.util.List;
Expand All @@ -19,8 +23,10 @@ public class WebConfig implements WebMvcConfigurer {
private final JwtLoginAuthInterceptor jwtLoginAuthInterceptor;

private final JwtLoginAuthHandlerArgumentResolver jwtLoginAuthHandlerArgumentResolver;
private final UserSpaceIdHandlerArgumentResolver userSpaceIdHandlerArgumentResolver;
private final UserSpaceAuthHandlerArgumentResolver userSpaceAuthHandlerArgumentResolver;
private final UserSpaceValidationInterceptor userSpaceValidationInterceptor;

private static final String DEVELOP_FRONT_ADDRESS = "http://localhost:5173";

@Override
public void addInterceptors(InterceptorRegistry registry) {
Expand All @@ -31,17 +37,30 @@ public void addInterceptors(InterceptorRegistry registry) {
for (InterceptorURL interceptorURL : InterceptorURL.values()) {
registration.addPathPatterns(interceptorURL.getUrlPattern());
}

InterceptorRegistration userSpaceRegistration =
registry.addInterceptor(userSpaceValidationInterceptor)
.order(2);
for(UserSpaceValidationInterceptorURL url:UserSpaceValidationInterceptorURL.values()) {
userSpaceRegistration.addPathPatterns(url.getUrlPattern());
}


}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(jwtLoginAuthHandlerArgumentResolver);
argumentResolvers.add(userSpaceIdHandlerArgumentResolver);
argumentResolvers.add(userSpaceAuthHandlerArgumentResolver);
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns(String.valueOf(List.of(DEVELOP_FRONT_ADDRESS, "http://localhost:3000")))
.allowedOriginPatterns("http://localhost:3000/", "http://localhost:5173/", "https://localhost:5173/",
"http://localhost:5173/KUIT-Space-Front/", "https://localhost:5173/KUIT-Space-Front/",
"https://kuit-space.github.io/KUIT-Space-front/")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.exposedHeaders("location")
.allowedHeaders("*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.chat.request.CreateChatRoomRequest;
import space.space_spring.dto.chat.request.JoinChatRoomRequest;
import space.space_spring.dto.chat.response.ChatSuccessResponse;
import space.space_spring.dto.chat.response.CreateChatRoomResponse;
import space.space_spring.dto.chat.response.ReadChatRoomMemberResponse;
import space.space_spring.dto.chat.response.ReadChatRoomResponse;
import space.space_spring.exception.ChatException;
import space.space_spring.exception.CustomException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.ChatRoomService;
import space.space_spring.service.S3Uploader;
import space.space_spring.util.userSpace.UserSpaceUtils;

import java.io.IOException;

import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_CHATROOM_CREATE;
import static space.space_spring.response.status.BaseExceptionResponseStatus.UNAUTHORIZED_USER;
import static space.space_spring.response.status.BaseExceptionResponseStatus.*;
import static space.space_spring.util.bindingResult.BindingResultUtils.getErrorMessage;

@RestController
Expand All @@ -28,11 +30,17 @@ public class ChatRoomController {
private final S3Uploader s3Uploader;
private final UserSpaceUtils userSpaceUtils;

/**
* ๋ชจ๋“  ์ฑ„ํŒ…๋ฐฉ ์ •๋ณด ์กฐํšŒ
*/
@GetMapping("/chatroom")
public BaseResponse<ReadChatRoomResponse> readChatRooms(@JwtLoginAuth Long userId, @PathVariable Long spaceId) {
return new BaseResponse<>(chatRoomService.readChatRooms(userId, spaceId));
}

/**
* ์ฑ„ํŒ…๋ฐฉ ์ƒ์„ฑ
*/
@PostMapping("/chatroom")
public BaseResponse<CreateChatRoomResponse> createChatRoom(
@JwtLoginAuth Long userId,
Expand All @@ -41,16 +49,95 @@ public BaseResponse<CreateChatRoomResponse> createChatRoom(
BindingResult bindingResult) throws IOException {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new ChatException(UNAUTHORIZED_USER);
throw new CustomException(UNAUTHORIZED_USER);
}

if (bindingResult.hasErrors()) {
throw new ChatException(INVALID_CHATROOM_CREATE, getErrorMessage(bindingResult));
throw new CustomException(INVALID_CHATROOM_CREATE, getErrorMessage(bindingResult));
}

String chatRoomDirName = "chatRoomImg";
String chatRoomImgUrl = s3Uploader.upload(createChatRoomRequest.getImg(), chatRoomDirName);

return new BaseResponse<>(chatRoomService.createChatRoom(userId, spaceId, createChatRoomRequest, chatRoomImgUrl));
}

/**
* ํŠน์ • ์ฑ„ํŒ…๋ฐฉ์˜ ๋ชจ๋“  ์œ ์ € ์ •๋ณด ์กฐํšŒ
*/
@GetMapping("/{chatRoomId}/member")
public BaseResponse<ReadChatRoomMemberResponse> readChatRoomMembers(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @PathVariable Long chatRoomId) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}

return new BaseResponse<>(chatRoomService.readChatRoomMembers(spaceId, chatRoomId));
}

/**
* ํŠน์ • ์ฑ„ํŒ…๋ฐฉ์— ์œ ์ € ์ดˆ๋Œ€
*/
@PostMapping("/{chatRoomId}/member")
public BaseResponse<ChatSuccessResponse> joinChatRoom(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId,
@RequestBody JoinChatRoomRequest joinChatRoomRequest,
BindingResult bindingResult) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}

if(bindingResult.hasErrors()){
throw new CustomException(INVALID_CHATROOM_JOIN,getErrorMessage(bindingResult));
}

return new BaseResponse<>(chatRoomService.joinChatRoom(chatRoomId, joinChatRoomRequest));
}

/**
* ํŠน์ • ์ฑ„ํŒ…๋ฐฉ์˜ ์ด๋ฆ„ ์ˆ˜์ •
*/
@PostMapping("/{chatRoomId}/setting")
public BaseResponse<ChatSuccessResponse> modifyChatRoomName(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId,
@RequestParam String name) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}

return new BaseResponse<>(chatRoomService.modifyChatRoomName(chatRoomId, name));
}

/**
* ํŠน์ • ์ฑ„ํŒ…๋ฐฉ์—์„œ ๋‚˜๊ฐ€๊ธฐ
*/
@PostMapping("/{chatRoomId}/exit")
public BaseResponse<ChatSuccessResponse> exitChatRoom(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId) {
return new BaseResponse<>(chatRoomService.exitChatRoom(userId, chatRoomId));
}

/**
* ํŠน์ • ์ฑ„ํŒ…๋ฐฉ ์‚ญ์ œ
*/
@PostMapping("/{chatRoomId}/delete")
public BaseResponse<ChatSuccessResponse> deleteChatRoom(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}

return new BaseResponse<>(chatRoomService.deleteChatRoom(chatRoomId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.pay.dto.PayReceiveInfoDto;
import space.space_spring.dto.pay.dto.PayRequestInfoDto;
import space.space_spring.dto.pay.dto.PayTargetInfoDto;
import space.space_spring.dto.pay.dto.TotalPayInfoDto;
import space.space_spring.dto.pay.request.PostPayCompleteRequest;
import space.space_spring.dto.pay.request.PostPayCreateRequest;
Expand Down
Loading

0 comments on commit 8983a5b

Please sign in to comment.