127 lines
3.3 KiB
PHP
127 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Mails\Auth\LinkResetPasswordMail;
|
|
use App\Models\GitlabUser;
|
|
use App\Models\UserPassword;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Uwil\LaravelBackend\Models\User as BackendUser;
|
|
|
|
/**
|
|
* Extends the package User model with some additionnal data.
|
|
*
|
|
* @property-read \App\Models\UserPassword|null $userPassword User's associated password.
|
|
* @property-read \App\Models\GitlabUser|null $gitlabUser User's associated gitlab id.
|
|
* @property-read \App\Models\Site|null $sites User's associated sites.
|
|
* @see \Uwil\LaravelBackend\Models\User For all other properties and methods.
|
|
*/
|
|
class User extends BackendUser
|
|
{
|
|
/**
|
|
* Relations should be included in the cache.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
#[\Override]
|
|
public static $cacheRelations = [
|
|
'gitlabUser',
|
|
'sites',
|
|
];
|
|
|
|
/**
|
|
* Except some fields to the user model fill.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
#[\Override]
|
|
public static $exceptFromFill = [
|
|
'gitlab_id',
|
|
'sites',
|
|
];
|
|
|
|
/**
|
|
* Relations should be share to views.
|
|
*
|
|
* @return array
|
|
*/
|
|
#[\Override]
|
|
public static function propsRelations(): array
|
|
{
|
|
return [
|
|
'gitlabUser',
|
|
'sites' => fn($q) => $q->withoutGlobalScope(SoftDeletingScope::class),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the password for authentication.
|
|
* Retrieves the hashed password from the related user_passwords table.
|
|
*
|
|
* @return string
|
|
*/
|
|
#[\Override]
|
|
public function getAuthPassword(): string
|
|
{
|
|
return $this->userPassword()->value('password') ?? '';
|
|
}
|
|
|
|
/**
|
|
* Send the password reset notification to the user.
|
|
*
|
|
* @param string $token
|
|
* @return void
|
|
*/
|
|
// phpstan-ignore-next-line
|
|
// phpcs:ignore
|
|
#[\Override]
|
|
public function sendPasswordResetNotification($token): void
|
|
{
|
|
$url = sprintf(
|
|
"%s?email=%s",
|
|
url(route('back.reset-password', ['token' => $token], false)),
|
|
urlencode($this->getEmailForPasswordReset())
|
|
);
|
|
|
|
Mail::send(new LinkResetPasswordMail($url, $this));
|
|
}
|
|
|
|
// * RELATIONSHIPS.
|
|
|
|
/**
|
|
* Get the user's associated password.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\App\Models\UserPassword,\App\Models\User>
|
|
*/
|
|
public function userPassword(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
{
|
|
return $this->hasOne(UserPassword::class);
|
|
}
|
|
|
|
/**
|
|
* Get the user's associated gitlab id.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\App\Models\GitlabUser,\App\Models\User>
|
|
*/
|
|
public function gitlabUser(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
{
|
|
return $this->hasOne(GitlabUser::class);
|
|
}
|
|
|
|
/**
|
|
* Get sites associated to the user.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Site>
|
|
*/
|
|
public function sites(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphToMany(
|
|
Site::class,
|
|
'sitable',
|
|
'sitables',
|
|
'sitable_id',
|
|
'site_id',
|
|
)->withPivot('created_at');
|
|
}
|
|
}
|