주요 변경사항: - Spatie Laravel Permission 패키지 설치 (v6.23.0) - admin 프로젝트에서 필수 Traits 및 Scopes 복사 - ModelTrait, BelongsToTenant, HasTenantFilter, UppercaseAttributes - TenantScope - Tenant 모델 관계 수정 (hasMany → belongsToMany via user_tenants) - Tenant 모델 null 처리 추가 (status_label, created_at) - Laravel 12 bootstrap/app.php에 API 라우트 등록 - API 라우트 미들웨어 수정 (auth:sanctum → web,auth) - HTMX 라이브러리 및 CSRF 토큰 헤더 추가 ViewServiceProvider 수정: - 전역 View Composer의 $tenants 변수를 $globalTenants로 변경 - 페이지별 페이지네이션된 $tenants 변수와의 충돌 방지 - tenant-selector.blade.php에서 $globalTenants 사용 버그 수정: - Collection::hasPages() 오류 해결 (ViewComposer 변수 덮어쓰기 문제) - 테넌트 목록 무한 로딩 스피너 해결 - 500 Internal Server Error 해결
55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Scopes;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Scope;
|
|
use Illuminate\Http\Request;
|
|
|
|
class TenantScope implements Scope
|
|
{
|
|
/**
|
|
* 캐시된 tenant_id (요청당 한 번만 조회)
|
|
*/
|
|
private static ?int $cachedTenantId = null;
|
|
|
|
private static bool $cacheInitialized = false;
|
|
|
|
/**
|
|
* Apply the scope to a given Eloquent query builder.
|
|
*/
|
|
public function apply(Builder $builder, Model $model)
|
|
{
|
|
|
|
// artisan migrate 등은 제외
|
|
if (app()->runningInConsole()) {
|
|
return;
|
|
}
|
|
|
|
// 캐시된 tenant_id가 없으면 조회 (요청당 1회만)
|
|
if (! self::$cacheInitialized) {
|
|
$request = app(Request::class);
|
|
|
|
self::$cachedTenantId = $request->attributes->get('tenant_id')
|
|
?? $request->header('X-TENANT-ID')
|
|
?? auth()->user()?->tenant_id;
|
|
|
|
self::$cacheInitialized = true;
|
|
}
|
|
|
|
if (self::$cachedTenantId !== null) {
|
|
$builder->where($model->getTable().'.tenant_id', self::$cachedTenantId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 캐시 초기화 (테스트 또는 장기 실행 프로세스에서 필요 시)
|
|
*/
|
|
public static function clearCache(): void
|
|
{
|
|
self::$cachedTenantId = null;
|
|
self::$cacheInitialized = false;
|
|
}
|
|
}
|