2025-08-20 20:23:01 +09:00
|
|
|
<?php
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-20 20:23:01 +09:00
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
|
|
|
|
|
|
abstract class Service
|
|
|
|
|
{
|
|
|
|
|
/** 활성 테넌트 ID(없으면 null) */
|
|
|
|
|
protected function tenantIdOrNull(): ?int
|
|
|
|
|
{
|
|
|
|
|
$id = app('tenant_id');
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-20 20:23:01 +09:00
|
|
|
return $id ? (int) $id : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 활성 테넌트 ID(없으면 400 Bad Request + i18n 메시지로 예외) */
|
|
|
|
|
protected function tenantId(): int
|
|
|
|
|
{
|
|
|
|
|
$id = $this->tenantIdOrNull();
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $id) {
|
2025-08-20 20:23:01 +09:00
|
|
|
// ko/error.php 의 'tenant_id' 키 사용
|
|
|
|
|
throw new BadRequestHttpException(__('error.tenant_id'));
|
|
|
|
|
}
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-20 20:23:01 +09:00
|
|
|
return $id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** (선택) API 사용자 ID 필요할 때 401로 던지고 싶다면 */
|
2025-08-21 15:43:06 +09:00
|
|
|
protected function apiUserId(): int
|
2025-08-20 20:23:01 +09:00
|
|
|
{
|
|
|
|
|
$uid = app('api_user');
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $uid) {
|
2025-08-20 20:23:01 +09:00
|
|
|
// Handler에서 AuthenticationException은 401로 처리 중
|
|
|
|
|
throw new AuthenticationException(__('auth.unauthenticated'));
|
|
|
|
|
}
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-20 20:23:01 +09:00
|
|
|
return (int) $uid;
|
|
|
|
|
}
|
2026-02-07 03:27:07 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 서비스 컨텍스트 설정 (다른 서비스에서 호출 시 사용)
|
|
|
|
|
* tenant_id, api_user를 명시적으로 설정
|
|
|
|
|
*/
|
|
|
|
|
public function setContext(int $tenantId, int $userId): self
|
|
|
|
|
{
|
|
|
|
|
app()->instance('tenant_id', $tenantId);
|
|
|
|
|
app()->instance('api_user', $userId);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2025-08-20 20:23:01 +09:00
|
|
|
}
|