Add profile avatar uploads
This commit is contained in:
+39
-2
@@ -4,29 +4,48 @@ namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use App\Actions\Permissions\PermissionService;
|
||||
use App\Actions\Users\UpdateUserAvatar;
|
||||
use App\Enums\Permission;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
|
||||
#[Fillable(['name', 'email', 'password', 'must_change_password_on_next_login'])]
|
||||
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
|
||||
#[Fillable(['name', 'email', 'password', 'must_change_password_on_next_login', 'avatar_path', 'avatar_color', 'avatar_is_generated'])]
|
||||
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token', 'avatar_path'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable, TwoFactorAuthenticatable;
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $appends = [
|
||||
'avatar',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected $attributes = [
|
||||
'avatar_is_generated' => false,
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::deleting(function (self $user): void {
|
||||
app(UpdateUserAvatar::class)->delete($user);
|
||||
|
||||
$user->permissionAssignments()->delete();
|
||||
});
|
||||
}
|
||||
@@ -39,6 +58,7 @@ class User extends Authenticatable
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'avatar_is_generated' => 'bool',
|
||||
'email_verified_at' => 'datetime',
|
||||
'must_change_password_on_next_login' => 'bool',
|
||||
'password' => 'hashed',
|
||||
@@ -46,6 +66,23 @@ class User extends Authenticatable
|
||||
];
|
||||
}
|
||||
|
||||
public function hasUploadedAvatar(): bool
|
||||
{
|
||||
return filled($this->avatar_path) && ! $this->avatar_is_generated;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Attribute<?string, never>
|
||||
*/
|
||||
protected function avatar(): Attribute
|
||||
{
|
||||
return Attribute::get(
|
||||
fn (): ?string => filled($this->avatar_path)
|
||||
? Storage::disk('public')->url($this->avatar_path)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
public function permissionAssignments(): MorphMany
|
||||
{
|
||||
return $this->morphMany(PermissionAssignment::class, 'assignee');
|
||||
|
||||
Reference in New Issue
Block a user