131 lines
3.4 KiB
PHP
131 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Mails\Auth\LinkResetPasswordMail;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
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 \Illuminate\Database\Eloquent\Collection<int,\App\Models\Site> $sites
|
|
* User's associated sites.
|
|
*
|
|
* @see \Uwil\LaravelBackend\Models\User For all other properties and methods.
|
|
*/
|
|
class User extends BackendUser
|
|
{
|
|
/**
|
|
* Except some fields to the user model fill.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
#[\Override]
|
|
public static $exceptFromFill = [
|
|
'gitlab_id',
|
|
'sites',
|
|
];
|
|
|
|
/**
|
|
* Get relations to include in the cache.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
#[\Override]
|
|
public static function getCacheRelations(): array
|
|
{
|
|
return ['gitlabUser', 'sites'];
|
|
}
|
|
|
|
/**
|
|
* Relations should be share to views.
|
|
*
|
|
* @return array<int|string,mixed>
|
|
*/
|
|
#[\Override]
|
|
public static function getPropsRelations(): 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.
|
|
*/
|
|
#[\Override]
|
|
public function getAuthPassword(): string
|
|
{
|
|
return $this->userPassword()->value('password') ?? '';
|
|
}
|
|
|
|
/**
|
|
* Send the password reset notification to the user.
|
|
*
|
|
* @param string $token
|
|
*
|
|
* @phpcs:disable
|
|
*/
|
|
#[\Override]
|
|
public function sendPasswordResetNotification($token): void
|
|
{
|
|
// @phpcs:enable
|
|
$url = sprintf(
|
|
'%s?email=%s',
|
|
url(route(config('uwil-backend.routes.name') . '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,$this>
|
|
*/
|
|
public function userPassword(): HasOne
|
|
{
|
|
return $this->hasOne(UserPassword::class);
|
|
}
|
|
|
|
/**
|
|
* Get the user's associated gitlab id.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne<\App\Models\GitlabUser,$this>
|
|
*/
|
|
public function gitlabUser(): HasOne
|
|
{
|
|
return $this->hasOne(GitlabUser::class);
|
|
}
|
|
|
|
/**
|
|
* Get sites associated to the user.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Site,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'>
|
|
*/
|
|
public function sites(): MorphToMany
|
|
{
|
|
return $this->morphToMany(
|
|
Site::class,
|
|
'sitable',
|
|
'sitables',
|
|
'sitable_id',
|
|
'site_id',
|
|
)->withPivot('created_at');
|
|
}
|
|
}
|