Projects/HubEleven

[리팩토링] 권한 별 기능 제한 로직 구현 (2) (업데이트 중..)

annovation 2026. 1. 8. 21:56

Controller

💡Controller에서 User 정보 받아오기

@Operation(summary = "주문 전체 조회 API", description = "주문 전체 목록을 조회한다.")
    @GetMapping
    public ResponseEntity<ApiResponse<CommonPageResponse<OrderResponse>>> getOrders(
            CommonPageRequest request,
            @RequestHeader("X-User-Id") Long userId,
            @RequestHeader("X-User-Role") String userRole
    ) {
  • Header에서 받아온 userId 와 userRole 을 활용해 권한 관리 로직을 구현한다.

💡User Role 을 통해 1차 검증


AuthorizationValidator

💡AuthorizationValidator 클래스로 User Role 검증 로직 분리

package com.hubEleven.order.application.validator;

import com.commonLib.common.exception.GlobalException;
import com.hubEleven.order.domain.exception.OrderErrorCode;
import org.springframework.stereotype.Component;

@Component
public class AuthorizationValidator {

    public void validateMaster(String userRole) {
        if (!"MASTER".equalsIgnoreCase(userRole)) {
            throw new GlobalException(OrderErrorCode.MASTER_ONLY);
        }
    }

    public void validateHubManager(String userRole) {
        if (!"HUB_MANAGER".equalsIgnoreCase(userRole)) {
            throw new GlobalException(OrderErrorCode.HUB_MANAGER_ONLY);
        }
    }

    public void validateDeliveryManager(String userRole) {
        if (!"DELIVERY_MANAGER".equalsIgnoreCase(userRole)) {
            throw new GlobalException(OrderErrorCode.DELIVERY_MANAGER_ONLY);
        }
    }

    public void validateCompanyMaanger(String userRole) {
        if (!"COMPANY_MANAGER".equalsIgnoreCase(userRole)) {
            throw new GlobalException(OrderErrorCode.COMPANY_MANAGER_ONLY);
        }
    }
}
  • application/validaor 패키지에 User Role 을 통해 검증할 수 있는 로직 분리

참고 자료