2025-11-20 16:24:40 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2025-11-24 18:48:07 +09:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2025-11-26 20:28:07 +09:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2025-11-20 21:09:14 +09:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2025-11-20 16:24:40 +09:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2025-11-20 21:09:14 +09:00
|
|
|
use Laravel\Sanctum\HasApiTokens;
|
2025-11-20 16:24:40 +09:00
|
|
|
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
2025-11-20 21:09:14 +09:00
|
|
|
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
|
2025-11-20 16:24:40 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
2025-11-20 21:09:14 +09:00
|
|
|
'user_id',
|
2025-11-20 16:24:40 +09:00
|
|
|
'name',
|
|
|
|
|
'email',
|
2025-11-20 21:09:14 +09:00
|
|
|
'phone',
|
2025-11-20 16:24:40 +09:00
|
|
|
'password',
|
2025-11-20 21:09:14 +09:00
|
|
|
'options',
|
|
|
|
|
'profile_photo_path',
|
|
|
|
|
'role',
|
|
|
|
|
'is_active',
|
|
|
|
|
'is_super_admin',
|
2025-11-20 16:24:40 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be hidden for serialization.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
protected $hidden = [
|
|
|
|
|
'password',
|
|
|
|
|
'remember_token',
|
2025-11-20 21:09:14 +09:00
|
|
|
'two_factor_secret',
|
|
|
|
|
'two_factor_recovery_codes',
|
|
|
|
|
'two_factor_confirmed_at',
|
|
|
|
|
'deleted_at',
|
2025-11-20 16:24:40 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
2025-11-20 21:09:14 +09:00
|
|
|
'last_login_at' => 'datetime',
|
2025-11-20 16:24:40 +09:00
|
|
|
'password' => 'hashed',
|
2025-11-20 21:09:14 +09:00
|
|
|
'options' => 'array',
|
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
|
'is_super_admin' => 'boolean',
|
2025-11-20 16:24:40 +09:00
|
|
|
];
|
|
|
|
|
}
|
2025-11-24 08:50:44 +09:00
|
|
|
|
|
|
|
|
/**
|
2025-11-24 18:48:07 +09:00
|
|
|
* 관계: 테넌트들 (Many-to-Many via user_tenants)
|
2025-11-24 08:50:44 +09:00
|
|
|
*/
|
2025-11-24 18:48:07 +09:00
|
|
|
public function tenants(): BelongsToMany
|
2025-11-24 08:50:44 +09:00
|
|
|
{
|
2025-11-24 18:48:07 +09:00
|
|
|
return $this->belongsToMany(\App\Models\Tenants\Tenant::class, 'user_tenants')
|
|
|
|
|
->withTimestamps()
|
|
|
|
|
->withPivot(['is_active', 'is_default', 'joined_at', 'left_at']);
|
2025-11-24 08:50:44 +09:00
|
|
|
}
|
2025-11-24 18:48:07 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 현재 선택된 테넌트 (세션 기반)
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
2025-11-26 20:28:07 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 관계: 사용자-역할 (user_roles 테이블, 테넌트별)
|
|
|
|
|
*/
|
|
|
|
|
public function userRoles(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(UserRole::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 관계: 사용자-부서 (department_user 테이블, 테넌트별)
|
|
|
|
|
*/
|
|
|
|
|
public function departmentUsers(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(DepartmentUser::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 특정 테넌트의 역할 목록 조회
|
|
|
|
|
*/
|
|
|
|
|
public function getRolesForTenant(int $tenantId)
|
|
|
|
|
{
|
|
|
|
|
return $this->userRoles()
|
|
|
|
|
->where('tenant_id', $tenantId)
|
|
|
|
|
->with('role')
|
|
|
|
|
->get()
|
|
|
|
|
->pluck('role');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 특정 테넌트의 부서 목록 조회
|
|
|
|
|
*/
|
|
|
|
|
public function getDepartmentsForTenant(int $tenantId)
|
|
|
|
|
{
|
|
|
|
|
return $this->departmentUsers()
|
|
|
|
|
->where('tenant_id', $tenantId)
|
|
|
|
|
->with('department')
|
|
|
|
|
->get()
|
|
|
|
|
->pluck('department');
|
|
|
|
|
}
|
2025-11-27 20:05:27 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 관계: 삭제한 사용자
|
|
|
|
|
*/
|
|
|
|
|
public function deletedByUser(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'deleted_by');
|
|
|
|
|
}
|
2025-11-24 18:48:07 +09:00
|
|
|
}
|