feat: 글로벌 메뉴 분리 및 테넌트 메뉴 동기화 시스템 구현
- global_menus 테이블 분리를 위한 menus 컬럼 추가 (global_menu_id, is_customized) - GlobalMenuController: 글로벌 메뉴 CRUD API - GlobalMenuService: 글로벌 메뉴 비즈니스 로직 - MenuSyncService: 테넌트 메뉴 동기화 서비스 - MenuBootstrapService: 테넌트 초기 메뉴 생성 로직 개선 - MenuController: 메뉴 재동기화 엔드포인트 추가
This commit is contained in:
@@ -13,36 +13,48 @@ class MenuBootstrapService
|
||||
/**
|
||||
* 글로벌 메뉴 템플릿을 테넌트에 복제
|
||||
*
|
||||
* - 활성화된 글로벌 메뉴만 복제 (is_active=1)
|
||||
* - global_menu_id로 원본 추적 가능
|
||||
* - is_customized=0 (원본 상태)
|
||||
*
|
||||
* @param int $tenantId 테넌트 ID
|
||||
* @return array 생성된 메뉴 ID 목록
|
||||
*/
|
||||
public static function cloneGlobalMenusForTenant(int $tenantId): array
|
||||
{
|
||||
return DB::transaction(function () use ($tenantId) {
|
||||
// 1. 글로벌 템플릿 메뉴 조회 (parent_id 순서대로 정렬)
|
||||
// 1. 활성화된 글로벌 템플릿 메뉴 조회 (parent_id 순서대로 정렬)
|
||||
$templateMenus = Menu::withoutGlobalScopes()
|
||||
->whereNull('tenant_id')
|
||||
->where('is_active', true) // 활성화된 메뉴만 복사
|
||||
->orderByRaw('COALESCE(parent_id, 0)')
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
|
||||
// 2. parent_id 매핑 테이블 생성
|
||||
// 2. parent_id 매핑 테이블 생성 (글로벌 ID → 테넌트 ID)
|
||||
$idMapping = [];
|
||||
$menuIds = [];
|
||||
|
||||
// 3. 계층 순서대로 복제 (parent → child)
|
||||
foreach ($templateMenus as $template) {
|
||||
// parent가 복제되지 않은 경우 (비활성 부모) 건너뛰기
|
||||
if ($template->parent_id && ! isset($idMapping[$template->parent_id])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$newMenu = Menu::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'parent_id' => $template->parent_id
|
||||
? ($idMapping[$template->parent_id] ?? null)
|
||||
: null,
|
||||
'global_menu_id' => $template->id, // 원본 글로벌 메뉴 ID 저장
|
||||
'name' => $template->name,
|
||||
'url' => $template->url,
|
||||
'icon' => $template->icon,
|
||||
'sort_order' => $template->sort_order,
|
||||
'is_active' => $template->is_active,
|
||||
'hidden' => $template->hidden,
|
||||
'is_customized' => false, // 원본 상태
|
||||
'is_external' => $template->is_external,
|
||||
'external_url' => $template->external_url,
|
||||
]);
|
||||
@@ -56,6 +68,89 @@ public static function cloneGlobalMenusForTenant(int $tenantId): array
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 글로벌 메뉴를 테넌트에 복제 (단일)
|
||||
*
|
||||
* @param int $globalMenuId 글로벌 메뉴 ID
|
||||
* @param int $tenantId 테넌트 ID
|
||||
* @param int|null $newParentId 복제 후 부모 메뉴 ID (테넌트 메뉴)
|
||||
* @return Menu|null 생성된 테넌트 메뉴 (이미 복제된 경우 null)
|
||||
*/
|
||||
public static function cloneSingleMenu(int $globalMenuId, int $tenantId, ?int $newParentId = null): ?Menu
|
||||
{
|
||||
// 이미 복제된 메뉴인지 확인
|
||||
$exists = Menu::withoutGlobalScopes()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('global_menu_id', $globalMenuId)
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 글로벌 메뉴 조회
|
||||
$globalMenu = Menu::withoutGlobalScopes()
|
||||
->whereNull('tenant_id')
|
||||
->find($globalMenuId);
|
||||
|
||||
if (! $globalMenu) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 테넌트 메뉴로 복제
|
||||
return Menu::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'parent_id' => $newParentId,
|
||||
'global_menu_id' => $globalMenu->id,
|
||||
'name' => $globalMenu->name,
|
||||
'url' => $globalMenu->url,
|
||||
'icon' => $globalMenu->icon,
|
||||
'sort_order' => $globalMenu->sort_order,
|
||||
'is_active' => $globalMenu->is_active,
|
||||
'hidden' => $globalMenu->hidden,
|
||||
'is_customized' => false,
|
||||
'is_external' => $globalMenu->is_external,
|
||||
'external_url' => $globalMenu->external_url,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 글로벌 메뉴와 그 하위 메뉴를 모두 테넌트에 복제 (재귀)
|
||||
*
|
||||
* @param int $globalMenuId 글로벌 메뉴 ID
|
||||
* @param int $tenantId 테넌트 ID
|
||||
* @param int|null $newParentId 복제 후 부모 메뉴 ID
|
||||
* @return array 생성된 메뉴 ID 목록
|
||||
*/
|
||||
public static function cloneMenuWithChildren(int $globalMenuId, int $tenantId, ?int $newParentId = null): array
|
||||
{
|
||||
return DB::transaction(function () use ($globalMenuId, $tenantId, $newParentId) {
|
||||
$menuIds = [];
|
||||
|
||||
// 1. 루트 메뉴 복제
|
||||
$rootMenu = self::cloneSingleMenu($globalMenuId, $tenantId, $newParentId);
|
||||
if (! $rootMenu) {
|
||||
return $menuIds;
|
||||
}
|
||||
$menuIds[] = $rootMenu->id;
|
||||
|
||||
// 2. 하위 메뉴 재귀 복제
|
||||
$children = Menu::withoutGlobalScopes()
|
||||
->whereNull('tenant_id')
|
||||
->where('parent_id', $globalMenuId)
|
||||
->where('is_active', true)
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
|
||||
foreach ($children as $child) {
|
||||
$childIds = self::cloneMenuWithChildren($child->id, $tenantId, $rootMenu->id);
|
||||
$menuIds = array_merge($menuIds, $childIds);
|
||||
}
|
||||
|
||||
return $menuIds;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 테넌트를 위한 기본 메뉴 구조 생성 (구버전 - 하위 호환성 유지)
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user