- User 모델: tenants() belongsToMany 관계 추가
- UserService: whereHas('tenants') pivot 필터링으로 변경
- 사용자 생성 시 user_tenants pivot 처리
- tenant_id 컬럼 참조 제거 (다대다 관계)
90 lines
2.1 KiB
PHP
90 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'user_id',
|
|
'name',
|
|
'email',
|
|
'phone',
|
|
'password',
|
|
'options',
|
|
'profile_photo_path',
|
|
'role',
|
|
'is_active',
|
|
'is_super_admin',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
'two_factor_secret',
|
|
'two_factor_recovery_codes',
|
|
'two_factor_confirmed_at',
|
|
'deleted_at',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'last_login_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'options' => 'array',
|
|
'is_active' => 'boolean',
|
|
'is_super_admin' => 'boolean',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 관계: 테넌트들 (Many-to-Many via user_tenants)
|
|
*/
|
|
public function tenants(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(\App\Models\Tenants\Tenant::class, 'user_tenants')
|
|
->withTimestamps()
|
|
->withPivot(['is_active', 'is_default', 'joined_at', 'left_at']);
|
|
}
|
|
|
|
/**
|
|
* 현재 선택된 테넌트 (세션 기반)
|
|
*/
|
|
public function currentTenant()
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
|
|
if (! $tenantId) {
|
|
return $this->tenants()->where('is_default', true)->first()
|
|
?? $this->tenants()->first();
|
|
}
|
|
|
|
return $this->tenants()->find($tenantId);
|
|
}
|
|
}
|