+60
-1
@@ -3,10 +3,13 @@
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use App\OrganizationRole;
|
||||
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;
|
||||
@@ -17,6 +20,7 @@ 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
|
||||
@@ -30,13 +34,68 @@ use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable(['name', 'email', 'password', 'is_admin', 'notes_limit'])]
|
||||
#[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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user