Skip to content

Commit

Permalink
[RID-421][#148][feat] : create launchedsequence & launchedmodule Auth… (
Browse files Browse the repository at this point in the history
  • Loading branch information
ji-seung-ryu authored Aug 15, 2023
2 parents f24388a + 79918e4 commit 38ba84b
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class WebConfiguration implements WebMvcConfigurer {
private final SequenceAuthInterceptor sequenceAuthInterceptor;
private final ModuleAuthInterceptor moduleAuthInterceptor;
private final LaunchedworkflowAuthInterceptor launchedworkflowAuthInterceptor;
private final LaunchedsequenceAuthInterceptor launchedsequenceAuthInterceptor;
private final LaunchedmoduleAuthInterceptor launchedmoduleAuthInterceptor;


@Override
public void addInterceptors(InterceptorRegistry registry){
Expand All @@ -37,5 +40,11 @@ public void addInterceptors(InterceptorRegistry registry){

registry.addInterceptor(launchedworkflowAuthInterceptor)
.addPathPatterns("/workspaces/{workspaceId}/launchedworkflows/{launchedworkflowId}/**");

registry.addInterceptor(launchedsequenceAuthInterceptor)
.addPathPatterns("/workspaces/{workspaceId}/launchedworkflows/{launchedworkflowId}/sequences/{sequenceId}/**");

registry.addInterceptor(launchedmoduleAuthInterceptor)
.addPathPatterns("/workspaces/{workspaceId}/launchedworkflows/{launchedworkflowId}/sequences/{sequenceId}/modules/{moduleId}/**");
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/backend/curi/common/interceptor/Extractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.backend.curi.common.interceptor;

import com.backend.curi.exception.CuriException;
import com.backend.curi.exception.ErrorType;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;

public class Extractor {
public static Long extractFromUrl(HttpServletRequest request, String point) {
String requestUrl = request.getRequestURI();
String[] parts = requestUrl.split("/" +point+ "/");
if (parts.length >= 2) {
String workspaceIdStr = parts[1].split("/")[0];
try {
return Long.parseLong(workspaceIdStr);
} catch (NumberFormatException e) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}

throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package com.backend.curi.common.interceptor;

public class LaunchedmoduleAuthInterceptor {

import com.backend.curi.exception.CuriException;
import com.backend.curi.exception.ErrorType;
import com.backend.curi.launched.service.LaunchedModuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@RequiredArgsConstructor
public class LaunchedmoduleAuthInterceptor implements HandlerInterceptor {
private final LaunchedModuleService launchedModuleService;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
Long launchedmoduleId = Extractor.extractFromUrl(request, "modules");

var launchedModule = launchedModuleService.getLaunchedModuleEntity(launchedmoduleId);
if(!launchedModule.getWorkspace().getId().equals(workspaceId)){
throw new CuriException(HttpStatus.NOT_FOUND, ErrorType.MODULE_NOT_EXISTS);
}

return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package com.backend.curi.common.interceptor;

public class LaunchedsequenceAuthInterceptor {
import com.backend.curi.exception.CuriException;
import com.backend.curi.exception.ErrorType;
import com.backend.curi.launched.repository.entity.LaunchedSequence;
import com.backend.curi.launched.service.LaunchedSequenceService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@RequiredArgsConstructor
public class LaunchedsequenceAuthInterceptor implements HandlerInterceptor {

private final LaunchedSequenceService launchedSequenceService;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
Long launchedsequenceId = Extractor.extractFromUrl(request, "sequences");

var launchedSequence = launchedSequenceService.getLaunchedSequenceEntity(launchedsequenceId);
if(!launchedSequence.getWorkspace().getId().equals(workspaceId)){
throw new CuriException(HttpStatus.NOT_FOUND, ErrorType.SEQUENCE_NOT_EXISTS);
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class LaunchedworkflowAuthInterceptor implements HandlerInterceptor {
private final LaunchedWorkflowService launchedWorkflowService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = extractFromUrl(request, "workspaces");
Long launchedworkflowId = extractFromUrl(request, "launchedworkflows");
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
Long launchedworkflowId = Extractor.extractFromUrl(request, "launchedworkflows");


var launchedWorkflow = launchedWorkflowService.getLaunchedWorkflowEntity(launchedworkflowId);
Expand All @@ -31,19 +31,5 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}

public static Long extractFromUrl(HttpServletRequest request, String point) {
String requestUrl = request.getRequestURI();
String[] parts = requestUrl.split("/" +point+ "/");
if (parts.length >= 2) {
String workspaceIdStr = parts[1].split("/")[0];
try {
return Long.parseLong(workspaceIdStr);
} catch (NumberFormatException e) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}

throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class ModuleAuthInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = extractFromUrl(request, "workspaces");
Long moduleId = extractFromUrl(request, "modules");
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
Long moduleId = Extractor.extractFromUrl(request, "modules");

Module module = moduleService.getModuleEntity(moduleId);

Expand All @@ -32,18 +32,5 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}

public static Long extractFromUrl(HttpServletRequest request, String point) {
String requestUrl = request.getRequestURI();
String[] parts = requestUrl.split("/" +point+ "/");
if (parts.length >= 2) {
String workspaceIdStr = parts[1].split("/")[0];
try {
return Long.parseLong(workspaceIdStr);
} catch (NumberFormatException e) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}

throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class SequenceAuthInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = extractFromUrl(request, "workspaces");
Long sequenceId = extractFromUrl(request, "sequences");
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
Long sequenceId = Extractor.extractFromUrl(request, "sequences");

Sequence sequence = sequenceService.getSequenceEntity(sequenceId);

Expand All @@ -32,18 +32,5 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}

public static Long extractFromUrl(HttpServletRequest request, String point) {
String requestUrl = request.getRequestURI();
String[] parts = requestUrl.split("/" +point+ "/");
if (parts.length >= 2) {
String workspaceIdStr = parts[1].split("/")[0];
try {
return Long.parseLong(workspaceIdStr);
} catch (NumberFormatException e) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}

throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class WorkflowAuthInterceptor implements HandlerInterceptor {
private final WorkflowService workflowService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = extractFromUrl(request, "workspaces");
Long workflowId = extractFromUrl(request, "workflows");
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
Long workflowId = Extractor.extractFromUrl(request, "workflows");

List<WorkflowResponse> workflowResponseList = workflowService.getWorkflows(workspaceId);
boolean found = workflowResponseList.stream()
Expand All @@ -32,18 +32,5 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}

public static Long extractFromUrl(HttpServletRequest request, String point) {
String requestUrl = request.getRequestURI();
String[] parts = requestUrl.split("/" +point+ "/");
if (parts.length >= 2) {
String workspaceIdStr = parts[1].split("/")[0];
try {
return Long.parseLong(workspaceIdStr);
} catch (NumberFormatException e) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}

throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,10 @@ public class WorkspaceAuthInterceptor implements HandlerInterceptor {
private final UserworkspaceService userworkspaceService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = extractFromUrl(request);
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
userworkspaceService.belongstoWorkspace(workspaceId);

return true;
}

public static Long extractFromUrl(HttpServletRequest request) {
String requestUrl = request.getRequestURI();
String[] parts = requestUrl.split("/workspaces/");
if (parts.length >= 2) {
String workspaceIdStr = parts[1].split("/")[0];
try {
return Long.parseLong(workspaceIdStr);
} catch (NumberFormatException e) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
}
// Return a default value or throw an exception if the workspaceId cannot be extracted.
// For simplicity, let's return null if the workspaceId cannot be extracted.
return null;
}
}

0 comments on commit 38ba84b

Please sign in to comment.