@@ -3,12 +3,15 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\OrganizationRole;
|
||||
use App\WorkspaceType;
|
||||
use Database\Factories\OrganizationFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Number;
|
||||
use Laravel\Cashier\Billable;
|
||||
@@ -17,13 +20,14 @@ use Laravel\Cashier\Subscription;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
/**
|
||||
* An organization owns the subscription and is the Stripe customer. Every user
|
||||
* is given a personal organization when their account is created; group and
|
||||
* company organizations are created later, which is why the model is not named
|
||||
* after a person.
|
||||
* An organization is a workspace: it owns the subscription, is the Stripe
|
||||
* customer, and its members share access to the folders inside it. Every user
|
||||
* is given a personal organization when their account is created; family and
|
||||
* organization workspaces are created later.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property WorkspaceType $type
|
||||
* @property bool $personal_workspace
|
||||
* @property bool $lifetime_access
|
||||
* @property string|null $stripe_id
|
||||
@@ -33,7 +37,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable(['name', 'personal_workspace', 'lifetime_access'])]
|
||||
#[Fillable(['name', 'type', 'personal_workspace', 'lifetime_access'])]
|
||||
#[Hidden(['stripe_id'])]
|
||||
class Organization extends Model
|
||||
{
|
||||
@@ -48,6 +52,7 @@ class Organization extends Model
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => WorkspaceType::class,
|
||||
'personal_workspace' => 'boolean',
|
||||
'lifetime_access' => 'boolean',
|
||||
'trial_ends_at' => 'datetime',
|
||||
@@ -64,6 +69,67 @@ class Organization extends Model
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Folder, $this>
|
||||
*/
|
||||
public function folders(): HasMany
|
||||
{
|
||||
return $this->hasMany(Folder::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphMany<Invitation, $this>
|
||||
*/
|
||||
public function invitations(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Invitation::class, 'inviteable');
|
||||
}
|
||||
|
||||
public function isPersonal(): bool
|
||||
{
|
||||
return $this->type === WorkspaceType::Personal;
|
||||
}
|
||||
|
||||
public function isFamily(): bool
|
||||
{
|
||||
return $this->type === WorkspaceType::Family;
|
||||
}
|
||||
|
||||
public function isOrganization(): bool
|
||||
{
|
||||
return $this->type === WorkspaceType::Organization;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get how many users this workspace may hold, or null for an unlimited
|
||||
* organization.
|
||||
*/
|
||||
public function seatLimit(): ?int
|
||||
{
|
||||
return config("workspaces.seat_limits.{$this->type->value}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get how many users this workspace currently holds.
|
||||
*/
|
||||
public function memberCount(): int
|
||||
{
|
||||
return $this->users()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether another member can join this workspace.
|
||||
*
|
||||
* Organization workspaces are provisioned separately and are therefore
|
||||
* never constrained by a seat limit.
|
||||
*/
|
||||
public function canAddMember(): bool
|
||||
{
|
||||
$limit = $this->seatLimit();
|
||||
|
||||
return $limit === null || $this->memberCount() < $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subscription type that Cashier stores this organization's
|
||||
* subscriptions under.
|
||||
|
||||
Reference in New Issue
Block a user