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;
|
2026-02-14 10:48:31 +09:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
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-12-01 23:44:56 +09:00
|
|
|
'must_change_password',
|
2025-11-20 21:09:14 +09:00
|
|
|
'options',
|
|
|
|
|
'profile_photo_path',
|
|
|
|
|
'role',
|
|
|
|
|
'is_active',
|
|
|
|
|
'is_super_admin',
|
2026-01-27 20:06:51 +09:00
|
|
|
'parent_id',
|
|
|
|
|
'approval_status',
|
|
|
|
|
'approved_by',
|
|
|
|
|
'approved_at',
|
|
|
|
|
'rejection_reason',
|
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-12-01 23:44:56 +09:00
|
|
|
'must_change_password' => 'boolean',
|
2026-01-27 20:06:51 +09:00
|
|
|
'approved_at' => 'datetime',
|
2025-11-20 16:24:40 +09:00
|
|
|
];
|
|
|
|
|
}
|
2025-11-24 08:50:44 +09:00
|
|
|
|
2026-01-27 20:06:51 +09:00
|
|
|
/**
|
2026-01-29 07:57:30 +09:00
|
|
|
* 상위 관리자 (영업파트너 계층 구조)
|
2026-01-27 20:06:51 +09:00
|
|
|
*/
|
|
|
|
|
public function parent(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'parent_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 하위 관리자 목록
|
|
|
|
|
*/
|
|
|
|
|
public function children(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(User::class, 'parent_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 승인자
|
|
|
|
|
*/
|
|
|
|
|
public function approver(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'approved_by');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 10:48:31 +09:00
|
|
|
/**
|
|
|
|
|
* 영업파트너 정보
|
|
|
|
|
*/
|
|
|
|
|
public function salesPartner(): HasOne
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(\App\Models\Sales\SalesPartner::class, 'user_id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 08:00:58 +09:00
|
|
|
/**
|
|
|
|
|
* 단체(corporate) 파트너 여부 확인
|
|
|
|
|
*/
|
|
|
|
|
public function isGroupPartner(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->salesPartner && $this->salesPartner->isGroup();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 20:06:51 +09:00
|
|
|
/**
|
2026-01-29 07:57:30 +09:00
|
|
|
* 영업파트너 첨부 서류
|
2026-01-27 20:06:51 +09:00
|
|
|
*/
|
|
|
|
|
public function salesDocuments(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(\App\Models\Sales\SalesManagerDocument::class, 'user_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 승인 대기 상태인지 확인
|
|
|
|
|
*/
|
|
|
|
|
public function isPendingApproval(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->approval_status === 'pending';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 승인됨 상태인지 확인
|
|
|
|
|
*/
|
|
|
|
|
public function isApproved(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->approval_status === 'approved';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 반려됨 상태인지 확인
|
|
|
|
|
*/
|
|
|
|
|
public function isRejected(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->approval_status === 'rejected';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 승인 상태 라벨
|
|
|
|
|
*/
|
|
|
|
|
public function getApprovalStatusLabelAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return match ($this->approval_status) {
|
|
|
|
|
'pending' => '승인대기',
|
|
|
|
|
'approved' => '승인완료',
|
|
|
|
|
'rejected' => '반려',
|
|
|
|
|
default => $this->approval_status,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 승인 상태별 색상 클래스
|
|
|
|
|
*/
|
|
|
|
|
public function getApprovalStatusColorAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return match ($this->approval_status) {
|
|
|
|
|
'pending' => 'bg-yellow-100 text-yellow-800',
|
|
|
|
|
'approved' => 'bg-green-100 text-green-800',
|
|
|
|
|
'rejected' => 'bg-red-100 text-red-800',
|
|
|
|
|
default => 'bg-gray-100 text-gray-800',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2026-02-26 21:55:27 +09:00
|
|
|
/**
|
|
|
|
|
* 관계: 테넌트별 프로필 (tenant_user_profiles 테이블)
|
|
|
|
|
*/
|
|
|
|
|
public function tenantProfiles(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(\App\Models\HR\Employee::class, 'user_id');
|
|
|
|
|
}
|
|
|
|
|
|
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-30 21:04:32 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 사용자가 본사(HQ) 테넌트에 소속되어 있는지 확인
|
|
|
|
|
*/
|
|
|
|
|
public function belongsToHQ(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->tenants()
|
|
|
|
|
->where('tenant_type', 'HQ')
|
|
|
|
|
->exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 본사(HQ) 테넌트 조회
|
|
|
|
|
*/
|
|
|
|
|
public function getHQTenant(): ?\App\Models\Tenants\Tenant
|
|
|
|
|
{
|
|
|
|
|
return $this->tenants()
|
|
|
|
|
->where('tenant_type', 'HQ')
|
|
|
|
|
->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 슈퍼관리자 여부 확인
|
|
|
|
|
*/
|
|
|
|
|
public function isSuperAdmin(): bool
|
|
|
|
|
{
|
|
|
|
|
return (bool) $this->is_super_admin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MNG 관리자 패널 접근 가능 여부
|
|
|
|
|
* - 본사(HQ) 테넌트 소속이어야 함
|
|
|
|
|
*/
|
|
|
|
|
public function canAccessMng(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToHQ() && $this->is_active;
|
|
|
|
|
}
|
2025-12-02 00:53:25 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 특정 역할 보유 여부 확인
|
|
|
|
|
*
|
|
|
|
|
* @param string|array $roles 역할명 또는 역할명 배열
|
|
|
|
|
*/
|
|
|
|
|
public function hasRole(string|array $roles): bool
|
|
|
|
|
{
|
|
|
|
|
// 슈퍼관리자는 모든 역할 보유
|
|
|
|
|
if ($this->is_super_admin) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$roles = is_array($roles) ? $roles : [$roles];
|
|
|
|
|
|
|
|
|
|
// 현재 테넌트 기준 역할 확인
|
|
|
|
|
$currentTenant = $this->currentTenant();
|
|
|
|
|
if (! $currentTenant) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userRoles = $this->getRolesForTenant($currentTenant->id);
|
|
|
|
|
|
|
|
|
|
foreach ($roles as $roleName) {
|
|
|
|
|
if ($userRoles->contains('name', $roleName)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 관리자 역할 보유 여부 (admin 또는 super-admin)
|
|
|
|
|
*/
|
|
|
|
|
public function isAdmin(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->is_super_admin || $this->hasRole(['admin', 'super-admin']);
|
|
|
|
|
}
|
2025-11-24 18:48:07 +09:00
|
|
|
}
|