forge/app/Models/User.php
Refringe c1a58e29dd
Multiple Mod Authors
The mod-to-user relationship has been modified to be a many-to-many relationship. Mods can now have multiple authors, and an author can have multiple mods.
2024-07-04 22:10:48 -04:00

98 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Scout\Searchable;
use Mchev\Banhammer\Traits\Bannable;
class User extends Authenticatable implements MustVerifyEmail
{
use Bannable;
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use Searchable;
use TwoFactorAuthenticatable;
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
protected $appends = [
'profile_photo_url',
];
public function mods(): BelongsToMany
{
return $this->belongsToMany(Mod::class);
}
public function toSearchableArray(): array
{
return [
'id' => (int) $this->id,
'name' => $this->name,
];
}
public function shouldBeSearchable(): bool
{
return ! is_null($this->email_verified_at);
}
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';
}
public function isAdmin(): bool
{
return Str::lower($this->role?->name) === 'administrator';
}
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
};
}
}