Files
2026-11-04 11:10:05 -08:00

169 lines
5.0 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\OrganizationRole;
use App\WorkspaceType;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravel\Fortify\Contracts\PasskeyUser;
use Laravel\Fortify\PasskeyAuthenticatable;
use Laravel\Fortify\TwoFactorAuthenticatable;
/**
* @property int $id
* @property int|null $current_organization_id
* @property string $name
* @property string $email
* @property Carbon|null $email_verified_at
* @property string $password
* @property string|null $two_factor_secret
* @property string|null $two_factor_recovery_codes
* @property Carbon|null $two_factor_confirmed_at
* @property string|null $remember_token
* @property bool $is_admin
* @property int|null $notes_limit
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
#[Fillable(['name', 'email', 'password', 'is_admin', 'notes_limit', 'current_organization_id'])]
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
class User extends Authenticatable implements PasskeyUser
{
/** @use HasFactory<UserFactory> */
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<Organization, $this>
*/
public function currentOrganization(): BelongsTo
{
return $this->belongsTo(Organization::class, 'current_organization_id');
}
/**
* @return BelongsToMany<Organization, $this>
*/
public function organizations(): BelongsToMany
{
return $this->belongsToMany(Organization::class)
->withPivot('role')
->withTimestamps();
}
/**
* @return BelongsToMany<Folder, $this>
*/
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<string, string>
*/
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;
}
}