mailwolt/app/Models/User.php

74 lines
1.8 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Enums\Role;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
protected $fillable = [
'name',
'username',
'email',
'admin_email',
'password',
'is_active',
'required_change_password',
'role',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'is_active' => 'boolean',
'required_change_password' => 'boolean',
'role' => Role::class,
];
/** Quick helper: check if user is admin */
public function isAdmin(): bool
{
return $this->role === Role::Admin;
}
// Fallback: admin_email || email
public function getSystemNotifyEmailAttribute(): string
{
$admin = trim((string)$this->admin_email);
return filter_var($admin, FILTER_VALIDATE_EMAIL) ? $admin : $this->email;
}
// Wenn du Laravel Notifications nutzt:
public function routeNotificationForMail($notification): string
{
return $this->system_notify_email;
}
public function twoFactorMethods()
{
return $this->hasMany(\App\Models\TwoFactorMethod::class);
}
public function twoFactorEnabled(): bool
{
return $this->twoFactorMethods()->where('enabled', true)->exists();
}
public function twoFactorMethod(string $method): ?\App\Models\TwoFactorMethod
{
return $this->twoFactorMethods()->where('method', $method)->first();
}
}