Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: remove company-owner gate from backup export

Sidwebworksdc5bcf7bf69a
diff --git a/.claude/rules/company-backup-restore.md b/.claude/rules/company-backup-restore.md
index a1db1966b0..04ef81b6ea 100644
--- a/.claude/rules/company-backup-restore.md
+++ b/.claude/rules/company-backup-restore.md
@@ -25,13 +25,14 @@ from a committed demo template. **Inngest tasks, NOT edge functions** — the ol
is the replacement. Reader-facing docs: `docs/content/docs/platform/backups.mdx`
(kept deliberately impl-free — keep internals here, not there).
-User-facing rules of the feature: backups are **company-owner only**
-(`group.ownerId === userId` check in the route), exclude secrets, and a restore is
+User-facing rules of the feature: backups require `settings` update permission
+(no owner gate — the old `group.ownerId === userId` check was removed from both the
+route and the `export-company` edge function), exclude secrets, and a restore is
reversible via an auto-snapshot.
**Currently internal-only** (`isInternalEmail`, `@carbon/utils`) while the
multi-tenant caveats below are unhardened: the nav entry is in `internalOnlyRoutes`
-(`useSettingsSubmodules.tsx`), and `requireOwner` (route loader/action) plus both
+(`useSettingsSubmodules.tsx`), and `requireInternal` (route loader/action) plus both
`api+/settings.backup-*` loaders 404/redirect non-internal users. Internal =
`@carbon.ms` / `@carbon.us.org`. Drop the gates to ship publicly.
@@ -141,9 +142,9 @@ snapshotPath, foreign, includeGroup }`. `revert` reads the marker and reloads
- `backups.server.ts` — server-only trigger wrappers (`startCompanyRestore`,
`finalizeCompanyRestore`, `revertCompanyRestore`) — kept off the client to avoid
`Buffer`-in-client.
-- `routes/x+/settings+/backups.tsx` — **owner-gated** loader/action (export /
- restore / keep / dismiss / revert / delete intents). `filePath` is forced under
- `exports/`.
+- `routes/x+/settings+/backups.tsx` — **internal-gated** (`requireInternal`)
+ loader/action (export / restore / keep / dismiss / revert / delete intents).
+ `filePath` is forced under `exports/`.
- `routes/api+/settings.backup-summary.ts` — lazy "what's in a backup" counts,
grouped, per-entity `scope: company|group`.
- `routes/api+/settings.backup-restore-status.$restoreRunId.ts` — poll; `companyId`
diff --git a/docs/content/docs/platform/backups.mdx b/docs/content/docs/platform/backups.mdx
index 46c34bf028..ba57509e50 100644
--- a/docs/content/docs/platform/backups.mdx
+++ b/docs/content/docs/platform/backups.mdx
@@ -5,7 +5,7 @@ description: Snapshot a company's data and files, restore it in place with one-c
A backup is a single snapshot of one company: its records, and optionally the files it has uploaded. You make one from **Settings → Backups**, and you do two things with it. You **restore** it over the same company to undo a bad change, or you use it as the starting data for a brand-new company at sign-up. Same file, two jobs.
-Backups belong to the company owner. Opening **Settings → Backups** as anyone else shows "Only the company owner can access backups".
+Backups are available to anyone with permission to manage **Settings**.
<Callout type="warning" title="A backup is not disaster recovery">
This is a per-company snapshot for cloning environments and undoing a bad change, not a full database dump, and there is no scheduling or point-in-time recovery. Treat it as a working tool, not your only copy.
diff --git a/packages/database/supabase/functions/export-company/index.ts b/packages/database/supabase/functions/export-company/index.ts
index 95d9115af1..ca56737ea2 100644
--- a/packages/database/supabase/functions/export-company/index.ts
+++ b/packages/database/supabase/functions/export-company/index.ts
@@ -1,16 +1,12 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";
-import {
- errorResponse,
- jsonResponse,
- requireCompanyOwner
-} from "../lib/company-backup.ts";
+import { errorResponse, jsonResponse } from "../lib/company-backup.ts";
import { corsHeaders } from "../lib/headers.ts";
import { sendInngestEvent } from "../lib/inngest.ts";
import { requirePermissions } from "../lib/supabase.ts";
/**
- * Thin auth boundary for company backup exports. Validates the caller is
- * the company owner, then hands the heavy lifting to the
+ * Thin auth boundary for company backup exports. Validates the caller has
+ * settings update permission, then hands the heavy lifting to the
* `carbon/company-export` inngest job. The backup lands in the company's
* bucket under `exports/`.
*/
@@ -28,10 +24,9 @@ serve(async (req: Request) => {
throw new Error("includeStorage must be 'none' or 'all'");
}
- const client = await requirePermissions(req, companyId, userId, {
+ await requirePermissions(req, companyId, userId, {
update: "settings"
});
- await requireCompanyOwner(client, companyId, userId);
await sendInngestEvent("carbon/company-export", {
companyId,
diff --git a/packages/database/supabase/functions/lib/company-backup.ts b/packages/database/supabase/functions/lib/company-backup.ts
index 2c6a49467b..20c9ab3b6f 100644
--- a/packages/database/supabase/functions/lib/company-backup.ts
+++ b/packages/database/supabase/functions/lib/company-backup.ts
@@ -1,11 +1,6 @@
-import type { createClient } from "@supabase/supabase-js";
-import type { Database } from "./types.ts";
-
export const BACKUP_INTEGRATION = "company-backup";
export const EXPORTS_PREFIX = "exports";
-type Client = ReturnType<typeof createClient<Database>>;
-
/** Extract the authenticated user id (JWT `sub`) from the request. */
export function getUserIdFromRequest(req: Request): string | null {
const token =
@@ -20,36 +15,6 @@ export function getUserIdFromRequest(req: Request): string | null {
}
}
-/**
- * Company data export/import is owner-only — the same gate the settings UI
- * applies via `requiresOwnership`.
- */
-export async function requireCompanyOwner(
- client: Client,
- companyId: string,
- userId: string
-): Promise<void> {
- const company = await client
- .from("company")
- .select("companyGroupId")
- .eq("id", companyId)
- .single();
- if (company.error) throw new Error(company.error.message);
- if (!company.data?.companyGroupId) {
- throw new Error("Company has no company group");
- }
-
- const group = await client
- .from("companyGroup")
- .select("ownerId")
- .eq("id", company.data.companyGroupId)
- .single();
- if (group.error) throw new Error(group.error.message);
- if (group.data?.ownerId !== userId) {
- throw new Error("Only the company owner can manage company data");
- }
-}
-
export function jsonResponse(
body: Record<string, unknown>,
status: number,