From 9935bba84eb710f801f6b3924303865fb4922a8d Mon Sep 17 00:00:00 2001 From: hskwon Date: Thu, 14 Aug 2025 20:19:51 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=ED=85=8C=EB=84=8C=ED=8A=B8=20?= =?UTF-8?q?=EC=97=86=EB=8A=94=20=ED=9A=8C=EC=9B=90=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=8F=20=ED=85=8C=EB=84=8C=ED=8A=B8=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=EC=8B=9C=20=ED=95=B4=EB=8B=B9=20=ED=85=8C=EB=84=8C=ED=8A=B8=20?= =?UTF-8?q?=EB=94=94=ED=8F=B4=ED=8A=B8=EA=B0=92=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Middleware/ApiKeyMiddleware.php | 23 ++++++++++++++--------- app/Models/Members/User.php | 2 +- app/Services/MemberService.php | 23 ++++++++++++----------- app/Services/TenantService.php | 10 ++++++++++ 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/app/Http/Middleware/ApiKeyMiddleware.php b/app/Http/Middleware/ApiKeyMiddleware.php index ef031c1..c2b079a 100644 --- a/app/Http/Middleware/ApiKeyMiddleware.php +++ b/app/Http/Middleware/ApiKeyMiddleware.php @@ -49,16 +49,21 @@ public function handle(Request $request, Closure $next) $user = $accessToken->tokenable; if ($user) { - // 기본 테넌트(여러개 소속시 우선순위) - $tenantId = $user->tenant?->tenant_id ?? $user->userTenants->first()?->tenant_id; + // 기본 테넌트(여러개 소속시 활성화 테넌트) + $tenantId = $user->userTenants + ->where('is_default', 1) + ->first()?->tenant_id + ?? $user->userTenants->first()?->tenant_id; + $tenantId = $tenantId ?? 0; + + // tenant_id 설정 $request->attributes->set('tenant_id', $tenantId); - $request->attributes->set('api_user', $user->id); - - - // ApiKeyMiddleware 등에서 - app()->instance('api_user', $user->id); app()->instance('tenant_id', $tenantId); + + // user_id 설정 + $request->attributes->set('api_user', $user->id); + app()->instance('api_user', $user->id); } } } @@ -75,8 +80,8 @@ public function handle(Request $request, Closure $next) if (!in_array($currentRoute, $allowWithoutAuth)) { // 인증정보(api_user, tenant_id) 없으면 튕김 - if (!app()->bound('api_user') || !app()->bound('tenant_id')) { - throw new AuthenticationException('회원정보 또는 테넌트 정보 없음'); + if (!app()->bound('api_user')) { + throw new AuthenticationException('회원정보 정보 없음'); } } diff --git a/app/Models/Members/User.php b/app/Models/Members/User.php index f44efde..3d58700 100644 --- a/app/Models/Members/User.php +++ b/app/Models/Members/User.php @@ -60,7 +60,7 @@ public function userTenants() public function userTenant() { - return $this->hasOne(UserTenant::class)->where('is_active', 1); + return $this->hasOne(UserTenant::class)->where('is_default', 1); } public function userRoles() diff --git a/app/Services/MemberService.php b/app/Services/MemberService.php index dbbac45..913bf47 100644 --- a/app/Services/MemberService.php +++ b/app/Services/MemberService.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Hash; use App\Models\Members\User; +use App\Models\Tenants\Tenant; use App\Models\Members\UserTenant; class MemberService @@ -49,16 +50,14 @@ public static function getMember(int $userNo) public static function getMyInfo() { $apiUser = app('api_user'); + $user = User::find($apiUser); + $data['user'] = $user; - $user = User::with([ - 'userTenant.tenant' => function($q) { - $q->select('id', 'company_name', 'code', 'email', 'phone', 'address', 'business_num', 'corp_reg_no', 'ceo_name', 'homepage', 'fax', 'logo', 'admin_memo', 'options',); // 원하는 컬럼만 - } - ])->find($apiUser); - $data=[ - 'user' => $user->userTenant->user, - 'tenant' => $user->userTenant->tenant, - ]; + $tenantId = app('tenant_id'); + if($tenantId){ + $tenant = Tenant::find($tenantId); + $data['tenant'] = $tenant; + } return ApiResponse::response('result', $data); } @@ -168,12 +167,14 @@ public static function switchMyTenant(int $tenantId) $apiUser = app('api_user'); // 1) 현재 유저의 기본 테넌트를 모두 해제 - UserTenant::where('user_id', $apiUser) + UserTenant::withoutGlobalScopes() + ->where('user_id', $apiUser) ->where('is_default', 1) ->update(['is_default' => 0]); // 2) 지정한 tenant_id를 기본 테넌트로 설정 - $updated = UserTenant::where('user_id', $apiUser) + $updated = UserTenant::withoutGlobalScopes() + ->where('user_id', $apiUser) ->where('tenant_id', $tenantId) ->update(['is_default' => 1]); diff --git a/app/Services/TenantService.php b/app/Services/TenantService.php index 3892fdf..262b4fe 100644 --- a/app/Services/TenantService.php +++ b/app/Services/TenantService.php @@ -257,6 +257,16 @@ public static function storeTenants(array $params = []) $tenant = Tenant::create($payload); + // 성성된 테넌트를 나의 테넌트로 셋팅 + $apiUser = app('api_user'); + UserTenant::create([ + 'user_id' => $apiUser, + 'tenant_id' => $tenant->id, + 'is_active' => 0, + 'is_default' => 1, + 'joined_at' => now(), + ]); + // 생성된 리소스를 그대로 반환 (목록 카드용 요약 원하면 컬럼 제한) return ApiResponse::response('result', $tenant); }