- User 모델에 must_change_password 필드 추가 - UserService: createUser(), resetPassword()에서 플래그 설정 - ProfileService: changePassword()에서 플래그 해제 - EnsurePasswordChanged 미들웨어 추가 - 인증 라우트에 password.changed 미들웨어 적용 - 프로필 페이지에 비밀번호 변경 필요 알림 추가 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
178 lines
4.3 KiB
PHP
178 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
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',
|
|
'must_change_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',
|
|
'must_change_password' => '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);
|
|
}
|
|
|
|
/**
|
|
* 관계: 사용자-역할 (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');
|
|
}
|
|
|
|
/**
|
|
* 관계: 삭제한 사용자
|
|
*/
|
|
public function deletedByUser(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'deleted_by');
|
|
}
|
|
|
|
/**
|
|
* 사용자가 본사(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;
|
|
}
|
|
}
|