2026-09-17 23:01:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
2026-10-04 20:34:19 -07:00
|
|
|
use App\OrganizationRole;
|
2026-09-17 23:01:26 -07:00
|
|
|
use Database\Factories\UserFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2026-10-04 20:34:19 -07:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2026-09-17 23:01:26 -07:00
|
|
|
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
|
2026-10-04 20:34:19 -07:00
|
|
|
* @property int|null $current_organization_id
|
2026-09-17 23:01:26 -07:00
|
|
|
* @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 $notes_limit
|
|
|
|
|
* @property Carbon|null $created_at
|
|
|
|
|
* @property Carbon|null $updated_at
|
|
|
|
|
*/
|
2026-10-04 20:34:19 -07:00
|
|
|
#[Fillable(['name', 'email', 'password', 'is_admin', 'notes_limit', 'current_organization_id'])]
|
2026-09-17 23:01:26 -07:00
|
|
|
#[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;
|
|
|
|
|
|
2026-10-04 20:34:19 -07:00
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-17 23:01:26 -07:00
|
|
|
/**
|
|
|
|
|
* 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 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;
|
|
|
|
|
}
|
|
|
|
|
}
|