94 lines
2.6 KiB
PHP
94 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\UserStatus;
|
|
use App\Notifications\QueuedResetPassword;
|
|
use App\Notifications\QueuedVerifyEmail;
|
|
use App\Traits\Filterable;
|
|
use App\Traits\Sortable;
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Carbon;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $email
|
|
* @property string $username
|
|
* @property string $password
|
|
* @property Carbon|null $email_verified_at
|
|
* @property int|null $tenant_id
|
|
* @property UserStatus $status
|
|
* @property Carbon $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property Carbon|null $deleted_at
|
|
* @property-read string $full_name
|
|
* @property-read UserProfile|null $profile
|
|
* @property-read Tenant|null $tenant
|
|
*/
|
|
#[Guarded(['id'])]
|
|
#[Hidden(['password'])]
|
|
#[Appends('full_name')]
|
|
class User extends Authenticatable implements MustVerifyEmail
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use Filterable, HasApiTokens, HasFactory, HasRoles, Notifiable, SoftDeletes, Sortable;
|
|
|
|
/** @var list<string> */
|
|
protected $sortable = ['email', 'username', 'status', 'created_at'];
|
|
|
|
/** @var list<string> */
|
|
protected $filterable = ['status'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'status' => UserStatus::class,
|
|
];
|
|
}
|
|
|
|
protected function fullName(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->profile->full_name ?? $this->username,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return HasOne<UserProfile, $this>
|
|
*/
|
|
public function profile(): HasOne
|
|
{
|
|
return $this->hasOne(UserProfile::class);
|
|
}
|
|
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
public function sendEmailVerificationNotification(): void
|
|
{
|
|
$this->notify(new QueuedVerifyEmail);
|
|
}
|
|
|
|
public function sendPasswordResetNotification(#[\SensitiveParameter] $token)
|
|
{
|
|
$this->notify(new QueuedResetPassword($token));
|
|
}
|
|
}
|