forge/app/Models/User.php

104 lines
2.3 KiB
PHP
Raw Normal View History

2024-05-13 18:55:34 -04:00
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
2024-05-13 18:55:34 -04:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2024-05-15 00:31:24 -04:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2024-05-13 18:55:34 -04:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
2024-05-13 18:55:34 -04:00
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
2024-06-02 22:03:59 -04:00
use Laravel\Scout\Searchable;
2024-06-16 21:40:00 -04:00
use Mchev\Banhammer\Traits\Bannable;
2024-05-13 18:55:34 -04:00
class User extends Authenticatable implements MustVerifyEmail
2024-05-13 18:55:34 -04:00
{
2024-06-16 21:40:00 -04:00
use Bannable;
2024-05-13 18:55:34 -04:00
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
2024-06-02 22:03:59 -04:00
use Searchable;
2024-06-03 02:04:49 +00:00
use TwoFactorAuthenticatable;
2024-05-13 18:55:34 -04:00
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
protected $appends = [
'profile_photo_url',
];
2024-05-15 00:31:24 -04:00
public function mods(): HasMany
{
return $this->hasMany(Mod::class);
}
2024-06-02 22:03:59 -04:00
public function toSearchableArray(): array
{
return [
'id' => (int) $this->id,
'name' => $this->name,
];
}
public function shouldBeSearchable(): bool
{
return ! is_null($this->email_verified_at);
}
2024-06-16 21:40:00 -04:00
public function assignRole(UserRole $role): bool
{
$this->role()->associate($role);
return $this->save();
}
public function role(): BelongsTo
{
return $this->belongsTo(UserRole::class, 'user_role_id');
}
public function isMod(): bool
{
return Str::lower($this->role?->name) === 'moderator' || $this->isAdmin();
}
public function isAdmin(): bool
{
return Str::lower($this->role?->name) === 'administrator';
}
2024-06-16 21:40:00 -04:00
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
/**
* Get the disk that profile photos should be stored on.
*/
protected function profilePhotoDisk(): string
{
return match (config('app.env')) {
'production' => 'r2', // Cloudflare R2 Storage
default => 'public', // Local
};
}
2024-05-13 18:55:34 -04:00
}