2024-05-13 18:55:34 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-08-08 16:11:50 -04:00
|
|
|
use App\Http\Filters\V1\QueryFilter;
|
2024-07-06 19:14:51 -04:00
|
|
|
use App\Notifications\ResetPassword;
|
|
|
|
use App\Notifications\VerifyEmail;
|
2024-08-07 16:23:55 -04:00
|
|
|
use App\Traits\HasCoverPhoto;
|
2024-05-14 00:09:13 -04:00
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
2024-08-08 16:11:50 -04:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-05-13 18:55:34 -04:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2024-06-18 00:41:12 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-07-04 22:10:48 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2024-05-13 18:55:34 -04:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2024-06-19 13:20:51 -04:00
|
|
|
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
|
|
|
|
2024-05-14 00:09:13 -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;
|
2024-08-07 16:23:55 -04:00
|
|
|
use HasCoverPhoto;
|
2024-05-13 18:55:34 -04:00
|
|
|
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 $hidden = [
|
|
|
|
'password',
|
|
|
|
'remember_token',
|
|
|
|
'two_factor_recovery_codes',
|
|
|
|
'two_factor_secret',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
'profile_photo_url',
|
|
|
|
];
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a user and their mods.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsToMany<Mod>
|
2024-07-20 19:52:36 -04:00
|
|
|
*/
|
2024-07-04 22:10:48 -04:00
|
|
|
public function mods(): BelongsToMany
|
2024-05-15 00:31:24 -04:00
|
|
|
{
|
2024-07-04 22:10:48 -04:00
|
|
|
return $this->belongsToMany(Mod::class);
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|
2024-06-02 22:03:59 -04:00
|
|
|
|
2024-08-28 16:59:28 -04:00
|
|
|
/**
|
2024-09-17 15:33:42 -04:00
|
|
|
* The relationship between a user and users that follow them.
|
|
|
|
*
|
|
|
|
* @return BelongsToMany<User>
|
2024-08-28 16:59:28 -04:00
|
|
|
*/
|
2024-09-17 15:33:42 -04:00
|
|
|
public function followers(): BelongsToMany
|
2024-08-28 16:59:28 -04:00
|
|
|
{
|
2024-09-17 15:33:42 -04:00
|
|
|
return $this->belongsToMany(User::class, 'user_follows', 'following_id', 'follower_id')
|
|
|
|
->withTimestamps();
|
2024-08-28 16:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-17 15:33:42 -04:00
|
|
|
* Follow another user.
|
2024-08-28 16:59:28 -04:00
|
|
|
*/
|
2024-08-29 21:50:59 -04:00
|
|
|
public function follow(User|int $user): void
|
|
|
|
{
|
|
|
|
$userId = $user instanceof User ? $user->id : $user;
|
2024-09-01 23:42:44 -04:00
|
|
|
|
|
|
|
if ($this->id === $userId) {
|
2024-09-17 15:33:42 -04:00
|
|
|
// Don't allow following yourself.
|
2024-09-01 23:42:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-17 17:19:39 -04:00
|
|
|
$this->following()->syncWithoutDetaching([$userId]);
|
2024-08-29 21:50:59 -04:00
|
|
|
}
|
|
|
|
|
2024-09-17 15:33:42 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a user and users they follow.
|
|
|
|
*
|
|
|
|
* @return BelongsToMany<User>
|
|
|
|
*/
|
|
|
|
public function following(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(User::class, 'user_follows', 'follower_id', 'following_id')
|
|
|
|
->withTimestamps();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unfollow another user.
|
|
|
|
*/
|
2024-08-29 21:50:59 -04:00
|
|
|
public function unfollow(User|int $user): void
|
|
|
|
{
|
|
|
|
$userId = $user instanceof User ? $user->id : $user;
|
2024-09-01 23:42:44 -04:00
|
|
|
|
|
|
|
if ($this->isFollowing($userId)) {
|
|
|
|
$this->following()->detach($userId);
|
|
|
|
}
|
2024-08-29 21:50:59 -04:00
|
|
|
}
|
|
|
|
|
2024-09-17 15:33:42 -04:00
|
|
|
/**
|
|
|
|
* Check if the user is following another user.
|
|
|
|
*/
|
|
|
|
public function isFollowing(User|int $user): bool
|
|
|
|
{
|
|
|
|
$userId = $user instanceof User ? $user->id : $user;
|
|
|
|
|
|
|
|
return $this->following()->where('following_id', $userId)->exists();
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* The data that is searchable by Scout.
|
|
|
|
*/
|
2024-06-02 22:03:59 -04:00
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => (int) $this->id,
|
|
|
|
'name' => $this->name,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* Determine if the model instance should be searchable.
|
|
|
|
*/
|
2024-06-02 22:03:59 -04:00
|
|
|
public function shouldBeSearchable(): bool
|
|
|
|
{
|
|
|
|
return ! is_null($this->email_verified_at);
|
|
|
|
}
|
2024-06-16 21:40:00 -04:00
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* Check if the user has the role of a moderator.
|
|
|
|
*/
|
2024-06-23 08:37:16 -04:00
|
|
|
public function isMod(): bool
|
|
|
|
{
|
2024-07-03 17:47:02 -04:00
|
|
|
return Str::lower($this->role?->name) === 'moderator';
|
2024-06-23 08:37:16 -04:00
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* Check if the user has the role of an administrator.
|
|
|
|
*/
|
2024-06-19 13:20:51 -04:00
|
|
|
public function isAdmin(): bool
|
|
|
|
{
|
2024-06-23 08:37:16 -04:00
|
|
|
return Str::lower($this->role?->name) === 'administrator';
|
2024-06-19 13:20:51 -04:00
|
|
|
}
|
|
|
|
|
2024-07-06 19:14:51 -04:00
|
|
|
/**
|
|
|
|
* Overwritten to instead use the queued version of the VerifyEmail notification.
|
|
|
|
*/
|
|
|
|
public function sendEmailVerificationNotification(): void
|
|
|
|
{
|
|
|
|
$this->notify(new VerifyEmail);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwritten to instead use the queued version of the ResetPassword notification.
|
|
|
|
*/
|
|
|
|
public function sendPasswordResetNotification(#[\SensitiveParameter] $token): void
|
|
|
|
{
|
|
|
|
$this->notify(new ResetPassword($token));
|
|
|
|
}
|
|
|
|
|
2024-07-20 13:28:38 -04:00
|
|
|
/**
|
|
|
|
* Get the relative URL to the user's profile page.
|
|
|
|
*/
|
|
|
|
public function profileUrl(): string
|
|
|
|
{
|
|
|
|
return route('user.show', [
|
|
|
|
'user' => $this->id,
|
|
|
|
'username' => $this->slug(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* Get the slug of the user's name.
|
|
|
|
*/
|
2024-07-20 13:28:38 -04:00
|
|
|
public function slug(): string
|
|
|
|
{
|
|
|
|
return Str::lower(Str::slug($this->name));
|
|
|
|
}
|
|
|
|
|
2024-07-21 01:29:07 -04:00
|
|
|
/**
|
|
|
|
* Assign a role to the user.
|
|
|
|
*/
|
|
|
|
public function assignRole(UserRole $role): bool
|
|
|
|
{
|
|
|
|
$this->role()->associate($role);
|
|
|
|
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The relationship between a user and their role.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsTo<UserRole, User>
|
2024-07-21 01:29:07 -04:00
|
|
|
*/
|
|
|
|
public function role(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(UserRole::class, 'user_role_id');
|
|
|
|
}
|
|
|
|
|
2024-08-08 16:11:50 -04:00
|
|
|
/**
|
|
|
|
* Scope a query by applying QueryFilter filters.
|
|
|
|
*/
|
|
|
|
public function scopeFilter(Builder $builder, QueryFilter $filters): Builder
|
|
|
|
{
|
|
|
|
return $filters->apply($builder);
|
|
|
|
}
|
|
|
|
|
2024-08-07 16:23:55 -04:00
|
|
|
/**
|
|
|
|
* Get the disk that profile photos should be stored on.
|
|
|
|
*/
|
|
|
|
protected function profilePhotoDisk(): string
|
|
|
|
{
|
|
|
|
return config('filesystems.asset_upload', 'public');
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast to native types.
|
|
|
|
*/
|
2024-06-16 21:40:00 -04:00
|
|
|
protected function casts(): array
|
|
|
|
{
|
|
|
|
return [
|
2024-09-17 15:33:42 -04:00
|
|
|
'id' => 'integer',
|
|
|
|
'hub_id' => 'integer',
|
2024-06-16 21:40:00 -04:00
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
'password' => 'hashed',
|
2024-09-17 15:33:42 -04:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
2024-06-16 21:40:00 -04:00
|
|
|
];
|
|
|
|
}
|
2024-05-13 18:55:34 -04:00
|
|
|
}
|