Skip to content

Commit

Permalink
[RID-425][#150][feat]: create fronoffice table
Browse files Browse the repository at this point in the history
  • Loading branch information
ji-seung-ryu committed Aug 16, 2023
1 parent 38ba84b commit ac47eb3
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
.authorizeRequests()
.antMatchers("h2-console").permitAll()
.antMatchers("/backend-api-docs/**", "/swagger-ui/**").permitAll()
.antMatchers("frontoffices").permitAll()
.antMatchers("*").authenticated()
.and()
.sessionManagement()
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/com/backend/curi/common/interceptor/Extractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

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

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

public static UUID extractUUIDFromUrl(HttpServletRequest request, String point) {
String requestUrl = request.getRequestURI();
String[] parts = requestUrl.split("/" +point+ "/");
if (parts.length >= 2) {
String id = parts[1].split("/")[0];
try {
return UUID.fromString(id);
} catch (NumberFormatException e) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.INVALID_URL_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.backend.curi.common.interceptor;

import com.backend.curi.exception.CuriException;
import com.backend.curi.exception.ErrorType;
import com.backend.curi.frontoffice.service.FrontofficeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
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;
import java.util.UUID;

@Component
@RequiredArgsConstructor
public class FrontofficeAuthInterceptor implements HandlerInterceptor {
private final FrontofficeService frontofficeService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = Extractor.extractLongFromUrl(request, "workspaces");
Long launchedmoduleId = Extractor.extractLongFromUrl(request, "modules");
UUID frontofficeId = Extractor.extractUUIDFromUrl(request, "frontoffices");
UUID accessToken = getAccessToken(request);

frontofficeService.checkAuth(frontofficeId, accessToken);

return true;
}

private UUID getAccessToken(HttpServletRequest request){
String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
if (authorization == null || !authorization.startsWith("Bearer ")) {
throw new CuriException(HttpStatus.BAD_REQUEST, ErrorType.FRONTOFFICE_UNAUTHORIZED);
}

String accessToken = authorization.split(" ")[1];

return UUID.fromString(accessToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class LaunchedmoduleAuthInterceptor implements HandlerInterceptor {

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

var launchedModule = launchedModuleService.getLaunchedModuleEntity(launchedmoduleId);
if(!launchedModule.getWorkspace().getId().equals(workspaceId)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand All @@ -20,8 +19,8 @@ public class LaunchedsequenceAuthInterceptor implements HandlerInterceptor {

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

var launchedSequence = launchedSequenceService.getLaunchedSequenceEntity(launchedsequenceId);
if(!launchedSequence.getWorkspace().getId().equals(workspaceId)){
Expand Down
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 = Extractor.extractFromUrl(request, "workspaces");
Long launchedworkflowId = Extractor.extractFromUrl(request, "launchedworkflows");
Long workspaceId = Extractor.extractLongFromUrl(request, "workspaces");
Long launchedworkflowId = Extractor.extractLongFromUrl(request, "launchedworkflows");


var launchedWorkflow = launchedWorkflowService.getLaunchedWorkflowEntity(launchedworkflowId);
Expand Down
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 = Extractor.extractFromUrl(request, "workspaces");
Long moduleId = Extractor.extractFromUrl(request, "modules");
Long workspaceId = Extractor.extractLongFromUrl(request, "workspaces");
Long moduleId = Extractor.extractLongFromUrl(request, "modules");

Module module = moduleService.getModuleEntity(moduleId);

Expand Down
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 = Extractor.extractFromUrl(request, "workspaces");
Long sequenceId = Extractor.extractFromUrl(request, "sequences");
Long workspaceId = Extractor.extractLongFromUrl(request, "workspaces");
Long sequenceId = Extractor.extractLongFromUrl(request, "sequences");

Sequence sequence = sequenceService.getSequenceEntity(sequenceId);

Expand Down
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 = Extractor.extractFromUrl(request, "workspaces");
Long workflowId = Extractor.extractFromUrl(request, "workflows");
Long workspaceId = Extractor.extractLongFromUrl(request, "workspaces");
Long workflowId = Extractor.extractLongFromUrl(request, "workflows");

List<WorkflowResponse> workflowResponseList = workflowService.getWorkflows(workspaceId);
boolean found = workflowResponseList.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.backend.curi.common.interceptor;

import com.backend.curi.exception.CuriException;
import com.backend.curi.exception.ErrorType;
import com.backend.curi.security.dto.CurrentUser;
import com.backend.curi.userworkspace.service.UserworkspaceService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

Expand All @@ -21,7 +15,7 @@ public class WorkspaceAuthInterceptor implements HandlerInterceptor {
private final UserworkspaceService userworkspaceService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Long workspaceId = Extractor.extractFromUrl(request, "workspaces");
Long workspaceId = Extractor.extractLongFromUrl(request, "workspaces");
userworkspaceService.belongstoWorkspace(workspaceId);

return true;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/backend/curi/exception/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public enum ErrorType {
SEQUENCE_MODULE_ALREADY_EXISTS("MODULE-004", "시퀀스에 해당 모듈이 이미 존재합니다. 모듈은 중복될 수 없습니다."),
WORKFLOW_SEQUENCE_NOT_EXISTS("SEQUENCE-001", "워크플로우에 해당 시퀀스가 존재하지 않습니다."),
SEQUENCE_NOT_EXISTS("SEQUENCE-002", "존재하지 않는 시퀀스 입니다."),
WORKFLOW_SEQUENCE_ALREADY_EXISTS("SEQUENCE-003", "워크플로우에 해당 시퀀스가 이미 존재합니다. 시퀀스는 중복될 수 없습니다.");
WORKFLOW_SEQUENCE_ALREADY_EXISTS("SEQUENCE-003", "워크플로우에 해당 시퀀스가 이미 존재합니다. 시퀀스는 중복될 수 없습니다."),

FRONTOFFICE_NOT_EXISTS("FRONTOFFICE-001", "존재하지 않는 프론트오피스입니다."),
FRONTOFFICE_UNAUTHORIZED ("FRONTOFFICE-002", "프론트오피스 접근권한이 없습니다.");

private final String errorCode;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.backend.curi.frontoffice.controller;

import com.backend.curi.frontoffice.controller.dto.FrontofficeResponse;
import com.backend.curi.frontoffice.service.FrontofficeService;
import com.backend.curi.launched.controller.dto.LaunchedSequenceResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/frontoffices")
@RequiredArgsConstructor
public class FrontofficeController {

private final FrontofficeService frontofficeService;

@GetMapping("/{frontofficeId}")
public ResponseEntity<FrontofficeResponse> getLaunchedsequence(@PathVariable UUID frontofficeId){
FrontofficeResponse frontofficeResponse = frontofficeService.getFrontoffice(frontofficeId);
return ResponseEntity.ok(frontofficeResponse);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.backend.curi.frontoffice.controller.dto;

import com.backend.curi.frontoffice.repository.entity.Frontoffice;
import com.backend.curi.launched.controller.dto.LaunchedSequenceResponse;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.UUID;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class FrontofficeResponse {
private UUID id;
private LaunchedSequenceResponse launchedSequenceResponse;

public static FrontofficeResponse of (Frontoffice frontoffice){
return new FrontofficeResponse(frontoffice.getId(), LaunchedSequenceResponse.of(frontoffice.getLaunchedSequence()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.curi.frontoffice.repository;

import com.backend.curi.frontoffice.repository.entity.Frontoffice;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface FrontofficeRepository extends JpaRepository<Frontoffice, UUID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.backend.curi.frontoffice.repository.entity;

import com.backend.curi.launched.repository.entity.LaunchedSequence;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.UUID;

@Entity
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Frontoffice {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

@OneToOne(fetch = FetchType.LAZY)
private LaunchedSequence launchedSequence;

private UUID accessToken;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.backend.curi.frontoffice.service;

import com.backend.curi.exception.CuriException;
import com.backend.curi.exception.ErrorType;
import com.backend.curi.frontoffice.controller.dto.FrontofficeResponse;
import com.backend.curi.frontoffice.repository.FrontofficeRepository;
import com.backend.curi.frontoffice.repository.entity.Frontoffice;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@RequiredArgsConstructor

public class FrontofficeService {

private final FrontofficeRepository frontofficeRepository;

public FrontofficeResponse getFrontoffice(UUID frontofficeId) {
Frontoffice frontoffice = frontofficeRepository.findById(frontofficeId).orElseThrow(() -> new CuriException(HttpStatus.NOT_FOUND, ErrorType.FRONTOFFICE_NOT_EXISTS));
return FrontofficeResponse.of(frontoffice);

}

public void checkAuth(UUID frontofficeId, UUID accessToken) {
Frontoffice frontoffice = frontofficeRepository.findById(frontofficeId).orElseThrow(() -> new CuriException(HttpStatus.NOT_FOUND, ErrorType.FRONTOFFICE_NOT_EXISTS));
if (!frontoffice.getAccessToken().equals(accessToken)) throw new CuriException(HttpStatus.UNAUTHORIZED, ErrorType.FRONTOFFICE_UNAUTHORIZED);

}
}
11 changes: 6 additions & 5 deletions src/main/java/com/backend/curi/security/filter/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,27 @@ public class JwtFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

try {


// h2-console 할 때는 패스!
if (request.getRequestURI().startsWith("/h2-console")){
filterChain.doFilter(request, response);
return;
}

// h2-console 할 때는 패스!
if (request.getRequestURI().startsWith("/swagger-ui") ){
filterChain.doFilter(request, response);
return;
}

// h2-console 할 때는 패스!
if (request.getRequestURI().startsWith("/backend-api-docs") ){
filterChain.doFilter(request, response);
return;
}

if (request.getRequestURI().startsWith("/frontoffices") ){
filterChain.doFilter(request, response);
return;
}


Cookie[] cookies = request.getCookies();

// pretendTobeAuthorized(request, response, filterChain);
Expand Down

0 comments on commit ac47eb3

Please sign in to comment.