146 lines
3.7 KiB
PHP
146 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\UserOrigin;
|
|
use App\Notifications\CustomResetPasswordNotification;
|
|
use App\Notifications\CustomVerifyEmailNotification;
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
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\Str;
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable implements MustVerifyEmail
|
|
{
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
use HasApiTokens, HasFactory, HasRoles, Notifiable, SoftDeletes, TwoFactorAuthenticatable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'partner_id',
|
|
'hub_id',
|
|
'origin',
|
|
'name',
|
|
'display_name',
|
|
'email',
|
|
'password',
|
|
'email_verified_at',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'deleted_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'origin' => UserOrigin::class,
|
|
];
|
|
}
|
|
|
|
public function partner(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Partner::class);
|
|
}
|
|
|
|
/**
|
|
* Direkte Hub-Zuordnung des Users (für schnelle Queries).
|
|
*/
|
|
public function hub(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Hub::class);
|
|
}
|
|
|
|
/**
|
|
* Get the registration code used by this user
|
|
*/
|
|
public function registrationCode(): HasOne
|
|
{
|
|
return $this->hasOne(RegistrationCode::class, 'used_by_user_id');
|
|
}
|
|
|
|
/**
|
|
* Get the user's initials
|
|
*/
|
|
public function initials(): string
|
|
{
|
|
return Str::of($this->name)
|
|
->explode(' ')
|
|
->map(fn (string $name) => Str::of($name)->substr(0, 1))
|
|
->implode('');
|
|
}
|
|
|
|
/**
|
|
* Anonymize user data (for users with dependencies)
|
|
*/
|
|
public function anonymize(): void
|
|
{
|
|
$this->update([
|
|
'name' => 'Gelöschter Benutzer #'.$this->id,
|
|
'display_name' => null,
|
|
'email' => 'deleted_'.$this->id.'@anonymized.local',
|
|
'password' => bcrypt(Str::random(64)),
|
|
]);
|
|
|
|
// Entferne alle Rollen
|
|
$this->syncRoles([]);
|
|
|
|
// Soft Delete
|
|
$this->delete();
|
|
}
|
|
|
|
/**
|
|
* Check if user has dependencies that require anonymization instead of deletion
|
|
*/
|
|
public function hasDependencies(): bool
|
|
{
|
|
// TODO: Später erweitern mit weiteren Verknüpfungen
|
|
// Beispiele: Orders, Projects, Documents, etc.
|
|
|
|
// Aktuell: Prüfe ob Partner existiert
|
|
return $this->partner_id !== null;
|
|
}
|
|
|
|
/**
|
|
* Send the password reset notification.
|
|
*
|
|
* @param string $token
|
|
*/
|
|
public function sendPasswordResetNotification($token): void
|
|
{
|
|
$this->notify(new CustomResetPasswordNotification($token));
|
|
}
|
|
|
|
/**
|
|
* Send the email verification notification.
|
|
*/
|
|
public function sendEmailVerificationNotification(): void
|
|
{
|
|
$this->notify(new CustomVerifyEmailNotification);
|
|
}
|
|
}
|