Files
sam-api/app/Models/Members/User.php
kent 06197a1366 fix : 테넌트 관리자 사용자 API 추가
- 사용자 목록
- 사용자 생성
- 사용자 단건 조회
- 사용자 수정
- 사용자 삭제(소프트 삭제)
- 활성/비활성 전환
- 삭제 복구
- 비밀번호 초기화

- 수정필요 : 역할부여, 역할 해재
2025-08-16 03:25:42 +09:00

81 lines
2.1 KiB
PHP

<?php
namespace App\Models\Members;
use App\Models\Commons\Role;
use App\Models\Commons\File;
use App\Models\Tenants\Tenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class User extends Authenticatable
{
use HasApiTokens, Notifiable, SoftDeletes, ModelTrait;
protected $fillable = [
'user_id',
'name',
'email',
'phone',
'password',
'options',
'profile_photo_path',
];
protected $casts = [
'email_verified_at' => 'datetime',
'last_login_at' => 'datetime',
'options' => 'array',
'deleted_at' => 'datetime',
'password' => 'hashed', // ← 이걸 쓰면 자동 해싱
];
protected $hidden = [
'password', 'remember_token',
'two_factor_secret', 'two_factor_recovery_codes', 'two_factor_confirmed_at',
'deleted_at',
];
public function userTenants()
{
return $this->hasMany(UserTenant::class);
}
public function userTenant()
{
return $this->hasOne(UserTenant::class)->where('is_default', 1);
}
public function userRoles()
{
return $this->hasMany(UserRole::class);
}
public function roles()
{
return $this->belongsToMany(Role::class, 'user_roles')->withPivot('tenant_id', 'assigned_at');
}
public function userTenantById($tenantId)
{
return $this->hasOne(UserTenant::class)->where('tenant_id', $tenantId);
}
public function files()
{
return $this->morphMany(File::class, 'fileable');
}
public function tenantsMembership(): BelongsToMany
{
return $this->belongsToMany(Tenant::class, 'user_tenants', 'user_id', 'tenant_id')
->as('membership') // pivot 대신 membership으로 표기
->withPivot(['is_active', 'is_default', 'joined_at', 'left_at', 'deleted_at'])
->wherePivotNull('deleted_at'); // 소프트삭제 제외
}
}