withoutGlobalScope(WorkspaceScope::class) if intentionally querying across workspaces.", operation: 'scope', model: $model ); } /** * Create exception for middleware. */ public static function forMiddleware(): self { return new self( message: 'This route requires workspace context. Ensure you are accessing through a valid workspace subdomain or have a workspace session.', operation: 'middleware' ); } /** * Get the operation that failed. */ public function getOperation(): ?string { return $this->operation; } /** * Get the model class that was involved. */ public function getModel(): ?string { return $this->model; } /** * Render the exception as an HTTP response. */ public function render(Request $request): Response { if ($request->expectsJson()) { return response()->json([ 'message' => $this->getMessage(), 'error' => 'missing_workspace_context', 'operation' => $this->operation, 'model' => $this->model, ], $this->getCode()); } // For web requests, show a user-friendly error page if (view()->exists('errors.workspace-required')) { return response()->view('errors.workspace-required', [ 'message' => $this->getMessage(), ], $this->getCode()); } return response($this->getMessage(), $this->getCode()); } /** * Report the exception (for logging/monitoring). */ public function report(): bool { // Log this as a potential security issue - workspace context was missing // where it should have been present logger()->warning('Missing workspace context', [ 'operation' => $this->operation, 'model' => $this->model, 'url' => request()->url(), 'user_id' => auth()->id(), ]); // Return true to indicate we've handled reporting return true; } }