*/ use HasFactory, Notifiable, PasskeyAuthenticatable, TwoFactorAuthenticatable; /** * Give every new account a personal organization to hold its subscription. * The entitlement check silently depends on one existing, so this happens * here rather than being left to each place that creates a user. */ protected static function booted(): void { static::created(function (User $user): void { $user->createPersonalOrganization(); }); } /** * @return BelongsTo */ public function currentOrganization(): BelongsTo { return $this->belongsTo(Organization::class, 'current_organization_id'); } /** * @return BelongsToMany */ public function organizations(): BelongsToMany { return $this->belongsToMany(Organization::class) ->withPivot('role') ->withTimestamps(); } /** * Create a personal organization owned by this user and make it current. */ public function createPersonalOrganization(): Organization { $organization = Organization::create([ 'name' => $this->name."'s Workspace", 'personal_workspace' => true, ]); $this->organizations()->attach($organization, ['role' => OrganizationRole::Owner->value]); $this->forceFill(['current_organization_id' => $organization->id])->save(); return $organization; } /** * Determine whether the current organization may use the application. A * user without one is never entitled. */ public function hasActiveSubscription(): bool { return $this->currentOrganization()->first()?->isEntitled() ?? false; } /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } /** * Get the user's initials */ public function initials(): string { $initials = Str::initials($this->name, true); return Str::length($initials) > 1 ? Str::substr($initials, 0, 1).Str::substr($initials, -1) : $initials; } /** * Determine whether the user is an administrator. */ public function isAdmin(): bool { return (bool) $this->is_admin; } /** * Get the maximum length allowed for todo notes. */ public function notesLimit(): int { return $this->notes_limit ?? 1000; } }