trypost/app/Policies/WorkspacePolicy.php
Paulo Castellano cbf8fb283c feat: per-workspace pricing, onboarding, and billing overhaul
Pricing
- Bill per workspace ($12/mo or $120/yr each); Stripe quantity tracks the
  workspace count and syncs on workspace create/delete.
- 2,500 AI credits per workspace, pooled at the account level; monthly reset
  on the billing anniversary, annual granted upfront (no rollover).
- One social account per network per workspace; remove all count-based limits
  (workspace/social/member) and the legacy plan tiers (single Workspace plan).

Onboarding (cloud only: SELF_HOSTED=false + PostHog)
- Replace the /subscribe plan picker with /onboarding persona selection
  (Creator/Freelancer/Startup/Agency/Small business/Other), saved on the user
  (users.persona) and mirrored to PostHog, then Stripe Checkout on the monthly
  price. 8-day trial so Stripe displays 7.

Billing screen
- Remove the Change Plan dialog (dead with a single plan); add an annual-upgrade
  banner for monthly subscribers (swapToYearly).
- Current-plan card shows the workspace count instead of the plan name.

System AI
- Brand analyzer / workspace autofill is always allowed and never debits credits
  (system feature, not the user's usage).

Self-hosted (SELF_HOSTED=true) bypasses all billing, credit, limit, network,
and onboarding logic.
2026-06-21 20:40:03 -03:00

118 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Policies;
use App\Enums\UserWorkspace\Role;
use App\Models\User;
use App\Models\Workspace;
class WorkspacePolicy
{
public function viewAny(User $user): bool
{
return true;
}
public function view(User $user, Workspace $workspace): bool
{
return $this->canAccess($user, $workspace);
}
public function create(User $user): bool
{
return $user->isAccountOwner();
}
public function update(User $user, Workspace $workspace): bool
{
return $this->isOwnerOrWorkspaceAdmin($user, $workspace);
}
public function delete(User $user, Workspace $workspace): bool
{
return $this->isOwner($user, $workspace);
}
public function restore(User $user, Workspace $workspace): bool
{
return $this->isOwner($user, $workspace);
}
public function forceDelete(User $user, Workspace $workspace): bool
{
return $this->isOwner($user, $workspace);
}
public function manageTeam(User $user, Workspace $workspace): bool
{
return $this->isOwnerOrWorkspaceAdmin($user, $workspace);
}
public function manageAccounts(User $user, Workspace $workspace): bool
{
return $this->isOwnerOrWorkspaceAdmin($user, $workspace);
}
public function createPost(User $user, Workspace $workspace): bool
{
if ($this->isOwner($user, $workspace)) {
return true;
}
return $this->hasRole($user, $workspace, [Role::Admin, Role::Member]);
}
public function inviteMember(User $user, Workspace $workspace): bool
{
return $this->isOwnerOrWorkspaceAdmin($user, $workspace);
}
public function manageBilling(User $user, Workspace $workspace): bool
{
return $this->isOwner($user, $workspace);
}
private function isOwner(User $user, Workspace $workspace): bool
{
return $workspace->account_id === $user->account_id && $user->isAccountOwner();
}
private function isOwnerOrWorkspaceAdmin(User $user, Workspace $workspace): bool
{
if ($this->isOwner($user, $workspace)) {
return true;
}
return $this->hasRole($user, $workspace, [Role::Admin]);
}
private function canAccess(User $user, Workspace $workspace): bool
{
if ($workspace->account_id !== $user->account_id) {
return false;
}
if ($user->isAccountOwner()) {
return true;
}
return $workspace->members()->where('user_id', $user->id)->exists();
}
private function hasRole(User $user, Workspace $workspace, array $roles): bool
{
if ($workspace->account_id !== $user->account_id) {
return false;
}
$member = $workspace->members()->where('user_id', $user->id)->first();
if (! $member) {
return false;
}
return in_array(Role::tryFrom($member->pivot->role), $roles);
}
}