2026-03-19 19:25:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
|
use Database\Factories\UserFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2026-03-19 19:47:06 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2026-03-21 15:25:05 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
2026-03-19 19:25:51 +02:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2026-03-21 15:25:05 +02:00
|
|
|
use Illuminate\Support\Str;
|
2026-03-19 19:25:51 +02:00
|
|
|
|
|
|
|
|
#[Fillable(['name', 'email', 'password'])]
|
|
|
|
|
#[Hidden(['password', 'remember_token'])]
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<UserFactory> */
|
|
|
|
|
use HasFactory, Notifiable;
|
|
|
|
|
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
|
'password' => 'hashed',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function projects(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Project::class)
|
|
|
|
|
->withPivot('role')
|
|
|
|
|
->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function ownedProjects(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->projects()->wherePivot('role', 'owner');
|
|
|
|
|
}
|
2026-03-19 19:47:06 +02:00
|
|
|
|
|
|
|
|
public function notificationPreferences(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(NotificationPreference::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function notificationPreferenceFor(Project $project): NotificationPreference
|
|
|
|
|
{
|
|
|
|
|
return $this->notificationPreferences()
|
|
|
|
|
->firstOrCreate(
|
|
|
|
|
['project_id' => $project->id],
|
|
|
|
|
['email_enabled' => true, 'database_enabled' => true, 'muted' => false],
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-21 15:25:05 +02:00
|
|
|
|
|
|
|
|
public function tokens(): MorphMany
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(PersonalAccessToken::class, 'tokenable');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createToken(string $name, array $abilities = ['*'], ?\DateTimeInterface $expiresAt = null): PersonalAccessToken
|
|
|
|
|
{
|
|
|
|
|
$token = Str::random(64);
|
|
|
|
|
|
|
|
|
|
return $this->tokens()->create([
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'token' => hash('sha256', $token),
|
|
|
|
|
'abilities' => $abilities,
|
|
|
|
|
'expires_at' => $expiresAt,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-03-19 19:25:51 +02:00
|
|
|
}
|