105 lines
2.9 KiB
PHP
105 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
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;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Fortify\Contracts\PasskeyUser;
|
|
use Laravel\Fortify\PasskeyAuthenticatable;
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
|
use Modules\UserManagement\Models\Role;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $name
|
|
* @property string $email
|
|
* @property Carbon|null $email_verified_at
|
|
* @property bool $helpdesk_agent
|
|
* @property bool $helpdesk_admin
|
|
*/
|
|
#[Fillable(['name', 'email', 'password', 'email_verified_at', 'helpdesk_agent', 'helpdesk_admin'])]
|
|
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
|
|
class User extends Authenticatable implements PasskeyUser
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasFactory, Notifiable, PasskeyAuthenticatable, TwoFactorAuthenticatable;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'helpdesk_agent' => 'boolean',
|
|
'helpdesk_admin' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function initials(): string
|
|
{
|
|
$initials = Str::initials($this->name, true);
|
|
|
|
return Str::length($initials) > 1
|
|
? Str::substr($initials, 0, 1).Str::substr($initials, -1)
|
|
: $initials;
|
|
}
|
|
|
|
public function gravatarUrl(int $size = 160): string
|
|
{
|
|
$email = strtolower(trim((string) $this->email));
|
|
$hash = hash('sha256', $email);
|
|
|
|
return 'https://www.gravatar.com/avatar/'.$hash
|
|
.'?s='.max(1, min($size, 2048))
|
|
.'&d=404&r=g';
|
|
}
|
|
|
|
public function roles(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
Role::class,
|
|
'user_management_role_user',
|
|
'user_id',
|
|
'role_id'
|
|
);
|
|
}
|
|
|
|
public function hasRole(string $role): bool
|
|
{
|
|
return $this->roles()->where('slug', $role)->exists();
|
|
}
|
|
|
|
public function hasPermission(string $permission): bool
|
|
{
|
|
return $this->roles()
|
|
->whereHas('permissions', static fn ($query) => $query->where('slug', $permission))
|
|
->exists();
|
|
}
|
|
|
|
public function isHelpdeskAdmin(): bool
|
|
{
|
|
return (bool) $this->helpdesk_admin;
|
|
}
|
|
|
|
public function isHelpdeskAgent(): bool
|
|
{
|
|
return $this->isHelpdeskAdmin() || (bool) $this->helpdesk_agent;
|
|
}
|
|
|
|
public function isAdmin(): bool
|
|
{
|
|
return $this->isHelpdeskAdmin();
|
|
}
|
|
|
|
public function isAgent(): bool
|
|
{
|
|
return $this->isHelpdeskAgent();
|
|
}
|
|
}
|