trypost/app/Http/Middleware/EnsureSubscribed.php
Paulo Castellano 4b015241ac fix: allow invited members to access workspace when owner has subscription
Members invited to a workspace no longer need their own subscription
if the workspace owner has an active subscription or trial.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 13:22:27 -03:00

48 lines
1.3 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureSubscribed
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// Skip subscription check for self-hosted mode
if (config('trypost.self_hosted')) {
return $next($request);
}
$user = $request->user();
if (! $user) {
return redirect()->route('login');
}
// Allow access if user has active subscription or is on trial
if ($user->subscribed('default') || $user->onTrial('default')) {
return $next($request);
}
// Allow access if user belongs to a workspace owned by a subscribed user
$currentWorkspace = $user->currentWorkspace;
if ($currentWorkspace && $currentWorkspace->owner && $currentWorkspace->owner->id !== $user->id) {
$owner = $currentWorkspace->owner;
if ($owner->subscribed('default') || $owner->onTrial('default')) {
return $next($request);
}
}
// Redirect to subscription page
return redirect()->route('subscribe');
}
}