*/ 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(); } /** * @return BelongsToMany */ public function sharedFolders(): BelongsToMany { return $this->belongsToMany(Folder::class) ->withPivot('role', 'invited_by') ->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", 'type' => WorkspaceType::Personal, '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 a site-wide administrator. This is * unrelated to any workspace role: site admins may reach the admin area, * whereas a workspace owner or admin may not. */ public function isSiteAdmin(): bool { return (bool) $this->is_admin; } /** * Determine whether the user is an owner or admin of the given workspace. * This grants rights inside that workspace only, never site-wide access. */ public function isOrganizationAdmin(Organization $organization): bool { return $organization->users() ->where('users.id', $this->id) ->wherePivotIn('role', [ OrganizationRole::Owner->value, OrganizationRole::Admin->value, ]) ->exists(); } /** * Get the maximum length allowed for todo notes. */ public function notesLimit(): int { return $this->notes_limit ?? 1000; } }