구현 패턴 예제
📝 초안 (Draft)
이 문서는 검토 중입니다. 내용이 변경될 수 있습니다.
Multi-SaaS Kit에서 자주 사용되는 구현 패턴입니다.
개요
Multi-SaaS Kit은 Laravel과 Filament를 기반으로 재사용 가능한 구현 패턴을 제공합니다. 이 문서에서는 Core 모듈의 주요 Trait와 패턴을 활용하는 방법을 설명합니다.
핵심 패턴 분류
| 패턴 | 모듈 | 용도 |
|---|---|---|
| 테넌트 격리 | Core/Tenant | 데이터 자동 격리 |
| Level 0~6 계층 권한 | Core/Permission | 계층별 접근 제어 |
| 감사 로그 | Core/Audit | 변경 이력 추적 |
| Guardian 관계 | Extensions/Guardian | 보호자-피보호자 관계 |
| 사용자 전환 | Extensions/Impersonate | 관리자 대리 접속 |
1. 테넌트 격리 패턴
BelongsToTenant Trait
모델에 BelongsToTenant Trait를 적용하면 자동으로 테넌트 격리가 적용됩니다.
namespace App\Models;
use App\Core\Base\Tenant\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use BelongsToTenant;
protected $fillable = ['name', 'price', 'description'];
}
자동 동작
// 자동으로 현재 테넌트의 데이터만 조회
$products = Product::all(); // WHERE tenant_id = {current_tenant_id}
// 생성 시 자동으로 tenant_id 설정
$product = Product::create([
'name' => '상품 A',
'price' => 10000,
]);
// tenant_id가 자동으로 설정됨
Migration 패턴
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 10, 2);
// 테넌트 격리용 컬럼
$table->foreignId('tenant_id')->constrained()->onDelete('cascade');
$table->index('tenant_id'); // 성능을 위한 인덱스
$table->timestamps();
});
테넌트 스코프 우회 (Platform Admin)
// Platform Admin만 전체 데이터 조회 가능
if (auth()->user()->isPlatformAdmin()) {
$allProducts = Product::withoutGlobalScope(TenantScope::class)->get();
}