사용자 관리: - 사용자 등록/수정 시 테넌트별 역할/부서 선택 기능 추가 - Department, UserRole, DepartmentUser 모델 추가 - User 모델에 역할/부서 관계 및 헬퍼 메서드 추가 - syncRoles/syncDepartments 메서드 (forceDelete로 유니크 키 충돌 방지) - 체크박스 UI로 다중 선택 지원 부서 관리: - Soft Delete 필터 (정상만/전체/삭제된 항목만) - 복구(restore) 및 영구삭제(forceDelete) 기능 추가 - Department 모델에 SoftDeletes 트레이트 추가 - 삭제된 항목 빨간 배경 + "삭제됨" 배지 표시
76 lines
2.1 KiB
PHP
76 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Tenants;
|
|
|
|
use App\Models\Members\User;
|
|
use App\Models\Permissions\PermissionOverride;
|
|
use App\Models\Tenants\Pivots\DepartmentUser;
|
|
use App\Traits\ModelTrait;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Permission\Traits\HasPermissions;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class Department extends Model
|
|
{
|
|
use HasPermissions, HasRoles, ModelTrait, SoftDeletes; // 부서도 권한/역할을 가짐
|
|
|
|
protected $table = 'departments';
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $casts = [
|
|
'tenant_id' => 'int',
|
|
'parent_id' => 'int',
|
|
'is_active' => 'bool',
|
|
'sort_order' => 'int',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'deleted_by', 'deleted_at',
|
|
];
|
|
|
|
// 스파티 가드명(admin 패널과 일치시켜야 함)
|
|
protected string $guard_name = 'api';
|
|
|
|
/** 테넌트 관계 */
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class, 'tenant_id');
|
|
}
|
|
|
|
/** 상위/하위 부서 */
|
|
public function parent(): BelongsTo
|
|
{
|
|
return $this->belongsTo(self::class, 'parent_id');
|
|
}
|
|
|
|
public function children()
|
|
{
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
}
|
|
|
|
/** 부서-사용자 N:N (추가 컬럼 포함 Pivot) */
|
|
public function users()
|
|
{
|
|
return $this->belongsToMany(User::class, 'department_user')
|
|
->using(DepartmentUser::class)
|
|
->withTimestamps()
|
|
->withPivot(['tenant_id', 'is_primary', 'joined_at', 'left_at']);
|
|
}
|
|
|
|
/** 부서의 권한 오버라이드(DENY/임시허용) */
|
|
public function permissionOverrides(): MorphMany
|
|
{
|
|
return $this->morphMany(PermissionOverride::class, 'model');
|
|
}
|
|
|
|
/** 부서-사용자 매핑 로우들(피벗 테이블의 레코드들) */
|
|
public function departmentUsers()
|
|
{
|
|
return $this->hasMany(DepartmentUser::class, 'department_id');
|
|
}
|
|
}
|