Ctrl K
ComunidadGuías…

app/Models/User.php

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Fortify\TwoFactorAuthenticatable;

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable, TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'password',
        'two_factor_secret',
        'two_factor_recovery_codes',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    /**
     * Determine whether new account registration is currently allowed.
     *
     * Registration is allowed when:
     * - the auth.registration_enabled config flag is true, OR
     * - there are zero users in the database (the first user — usually the
     *   operator setting up the app — can always register)
     */
    public static function registrationAllowed(): bool
    {
        if (config('auth.registration_enabled', false)) {
            return true;
        }

        return self::query()->count() === 0;
    }

    /**
     * Determine whether this user is the instance owner (the first user,
     * usually the operator who installed the app)
     */
    public function isInstanceOwner(): bool
    {
        return $this->id === self::query()->min('id');
    }

    /**
     * Get the user's initials
     */
    public function initials(): string
    {
        return Str::of($this->name)
            ->explode(' ')
            ->take(2)
            ->map(fn ($word) => Str::substr($word, 0, 1))
            ->implode('');
    }

    /**
     * Get teams owned by the user
     */
    public function ownedTeams(): HasMany
    {
        return $this->hasMany(Team::class, 'owner_id');
    }

    /**
     * Get teams the user belongs to
     */
    public function teams(): BelongsToMany
    {
        return $this->belongsToMany(Team::class)
            ->withPivot('role')
            ->withTimestamps();
    }

    /**
     * Get monitors owned by the user
     */
    public function monitors(): HasMany
    {
        return $this->hasMany(Monitor::class);
    }

    /**
     * Get projects owned by the user
     */
    public function projects(): HasMany
    {
        return $this->hasMany(Project::class);
    }

    /**
     * Get status pages owned by the user
     */
    public function statusPages(): HasMany
    {
        return $this->hasMany(StatusPage::class);
    }

    /**
     * Get the user's notification channels
     */
    public function notificationChannels(): HasMany
    {
        return $this->hasMany(NotificationChannel::class);
    }

    /**
     * Get the user's default notification channel
     */
    public function defaultNotificationChannel(): ?NotificationChannel
    {
        return $this->notificationChannels()
            ->where('is_active', true)
            ->where('is_default', true)
            ->first();
    }

    protected static function booted(): void
    {
        static::created(function (self $user): void {
            $user->notificationChannels()->create([
                'type' => \App\Enums\NotificationChannelType::Email,
                'config' => [],
                'is_active' => true,
                'is_default' => true,
            ]);
        });
    }
}