Files
sam-manage/app/Models/User.php

90 lines
2.1 KiB
PHP
Raw Normal View History

<?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);
}
}