Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Block staging workspace actions with throwIfStagingWorkspace helper

Kiran K46837da68aaa
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/invites/reset/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/invites/reset/route.ts
index ef1de6679c..3e965448ac 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/invites/reset/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/invites/reset/route.ts
@@ -1,10 +1,13 @@
import { withWorkspace } from "@/lib/auth";
+import { throwIfStagingWorkspace } from "@/lib/sandbox/throw-if-staging-workspace";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { NextResponse } from "next/server";
export const POST = withWorkspace(
async ({ workspace }) => {
+ throwIfStagingWorkspace(workspace);
+
const response = await prisma.project.update({
where: {
id: workspace.id,
@@ -18,6 +21,5 @@ export const POST = withWorkspace(
},
{
requiredPermissions: ["workspaces.write"],
- rejectStagingWorkspace: true,
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/invites/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/invites/route.ts
index fb981d71fd..6980e6fad7 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/invites/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/invites/route.ts
@@ -3,6 +3,7 @@ import { inviteUser } from "@/lib/api/users";
import { assertRoleAllowedForPlan } from "@/lib/api/workspaces/assert-role-plan";
import { withWorkspace } from "@/lib/auth";
import { exceededLimitError } from "@/lib/exceeded-limit-error";
+import { throwIfStagingWorkspace } from "@/lib/sandbox/throw-if-staging-workspace";
import { ratelimit, redis } from "@/lib/upstash";
import { inviteTeammatesSchema } from "@/lib/zod/schemas/invites";
import {
@@ -48,6 +49,8 @@ export const GET = withWorkspace(
// POST /api/workspaces/[idOrSlug]/invites – invite a teammate
export const POST = withWorkspace(
async ({ req, workspace, session }) => {
+ throwIfStagingWorkspace(workspace);
+
const { teammates } = inviteTeammatesSchema.parse(await req.json());
for (const teammate of teammates) {
@@ -168,7 +171,6 @@ export const POST = withWorkspace(
},
{
requiredPermissions: ["workspaces.write"],
- rejectStagingWorkspace: true,
},
);
@@ -180,6 +182,8 @@ const updateInviteRoleSchema = z.object({
// PATCH /api/workspaces/[idOrSlug]/invites - update an invite's role
export const PATCH = withWorkspace(
async ({ req, workspace }) => {
+ throwIfStagingWorkspace(workspace);
+
const { email, role } = updateInviteRoleSchema.parse(await req.json());
assertRoleAllowedForPlan({
@@ -219,13 +223,14 @@ export const PATCH = withWorkspace(
},
{
requiredPermissions: ["workspaces.write"],
- rejectStagingWorkspace: true,
},
);
// DELETE /api/workspaces/[idOrSlug]/invites – delete a pending invite
export const DELETE = withWorkspace(
async ({ searchParams, workspace }) => {
+ throwIfStagingWorkspace(workspace);
+
const { email } = z
.object({
email: z.email(),
@@ -245,6 +250,5 @@ export const DELETE = withWorkspace(
},
{
requiredPermissions: ["workspaces.write"],
- rejectStagingWorkspace: true,
},
);
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index 828e9787ea..f39b360e57 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -8,6 +8,7 @@ import { withWorkspace } from "@/lib/auth";
import { getFeatureFlags } from "@/lib/edge-config";
import { jackson } from "@/lib/jackson";
import { syncWorkspaceSettings } from "@/lib/sandbox/sync-workspace";
+import { throwIfStagingWorkspace } from "@/lib/sandbox/throw-if-staging-workspace";
import { mergeSiteVisitTrackingSettings } from "@/lib/sitemaps/site-visit-tracking";
import { storage } from "@/lib/storage";
import { redis } from "@/lib/upstash";
@@ -97,16 +98,9 @@ export const PATCH = withWorkspace(
siteVisitTrackingSettings,
} = await updateWorkspaceSchema.parseAsync(await parseRequestBody(req));
- if (
- workspace.environment === WorkspaceEnvironment.staging &&
- (name || slug || logo)
- ) {
- throw new DubApiError({
- code: "forbidden",
- message:
- "This action is not available in a staging workspace. Use the production workspace instead.",
- });
- }
+ throwIfStagingWorkspace(workspace, {
+ when: !!(name || slug || logo),
+ });
if (["free", "pro"].includes(workspace.plan) && conversionEnabled) {
throw new DubApiError({
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
index 290b8c3382..f5da6e2b07 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
@@ -7,12 +7,13 @@ import {
removeMemberFromStaging,
updateMemberRoleInStaging,
} from "@/lib/sandbox/sync-workspace";
+import { throwIfStagingWorkspace } from "@/lib/sandbox/throw-if-staging-workspace";
import {
getWorkspaceUsersQuerySchema,
workspaceUserSchema,
} from "@/lib/zod/schemas/workspaces";
import { prisma } from "@dub/prisma";
-import { WorkspaceEnvironment, WorkspaceRole } from "@dub/prisma/client";
+import { WorkspaceRole } from "@dub/prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
import * as z from "zod/v4";
@@ -64,6 +65,8 @@ const updateRoleSchema = z.object({
// PATCH /api/workspaces/[idOrSlug]/users – update a user's role for a specific workspace
export const PATCH = withWorkspace(
async ({ req, workspace }) => {
+ throwIfStagingWorkspace(workspace);
+
const { userId, role } = updateRoleSchema.parse(await req.json());
assertRoleAllowedForPlan({
@@ -100,7 +103,6 @@ export const PATCH = withWorkspace(
},
{
requiredPermissions: ["workspaces.write"],
- rejectStagingWorkspace: true,
},
);
@@ -156,16 +158,9 @@ export const DELETE = withWorkspace(
});
}
- if (
- workspace.environment === WorkspaceEnvironment.staging &&
- !projectUser.user.isMachine
- ) {
- throw new DubApiError({
- code: "forbidden",
- message:
- "This action is not available in a staging workspace. Use the production workspace instead.",
- });
- }
+ throwIfStagingWorkspace(workspace, {
+ when: !projectUser.user.isMachine,
+ });
// If there is only one owner and the user is an owner and the user is trying to remove themselves
if (
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/page-client.tsx
index 55eb871fde..a0f659d0cc 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/page-client.tsx
@@ -19,7 +19,7 @@ export default function WorkspaceSettingsClient() {
action: "workspaces.write",
role,
environment,
- stagingBehavior: "blocked",
+ stagingBehavior: "production-only",
});
const { update } = useSession();
diff --git a/apps/web/lib/api/workspaces/delete-workspace.ts b/apps/web/lib/api/workspaces/delete-workspace.ts
index 351d785dfc..ab68f4bd09 100644
--- a/apps/web/lib/api/workspaces/delete-workspace.ts
+++ b/apps/web/lib/api/workspaces/delete-workspace.ts
@@ -28,9 +28,6 @@ export async function deleteWorkspace(
select: {
id: true,
slug: true,
- logo: true,
- stripeId: true,
- stagingWorkspaceId: true,
},
})
: null;
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index ae0b3dc857..674dfd0494 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -2,7 +2,7 @@ import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { BetaFeatures, PlanProps, WorkspaceWithUsers } from "@/lib/types";
import { ratelimit } from "@/lib/upstash";
import { prisma } from "@dub/prisma";
-import { WorkspaceEnvironment, WorkspaceRole } from "@dub/prisma/client";
+import { WorkspaceRole } from "@dub/prisma/client";
import { API_DOMAIN, getSearchParams } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { headers } from "next/headers";
@@ -71,13 +71,11 @@ export const withWorkspace = (
requiredPermissions = [],
requiredRoles = [],
featureFlag, // if the action needs a specific feature flag
- rejectStagingWorkspace = false,
}: {
requiredPlan?: Array<PlanProps>;
requiredPermissions?: PermissionAction[];
requiredRoles?: WorkspaceRole[];
featureFlag?: BetaFeatures;
- rejectStagingWorkspace?: boolean;
} = {},
) => {
return withAxiomBodyLog(
@@ -462,17 +460,6 @@ export const withWorkspace = (
});
}
- if (
- rejectStagingWorkspace &&
- workspace.environment === WorkspaceEnvironment.staging
- ) {
- throw new DubApiError({
- code: "forbidden",
- message:
- "This action is not available in a staging workspace. Use the production workspace instead.",
- });
- }
-
// analytics API checks
if (
workspace.plan === "free" &&
diff --git a/apps/web/lib/sandbox/throw-if-staging-workspace.ts b/apps/web/lib/sandbox/throw-if-staging-workspace.ts
new file mode 100644
index 0000000000..9a698cadde
--- /dev/null
+++ b/apps/web/lib/sandbox/throw-if-staging-workspace.ts
@@ -0,0 +1,21 @@
+import { DubApiError } from "@/lib/api/errors";
+import { Project, WorkspaceEnvironment } from "@dub/prisma/client";
+
+export function throwIfStagingWorkspace(
+ workspace: Pick<Project, "environment">,
+ options?: {
+ when?: boolean;
+ message?: string;
+ },
+) {
+ const shouldCheck = options?.when ?? true;
+
+ if (shouldCheck && workspace.environment === WorkspaceEnvironment.staging) {
+ throw new DubApiError({
+ code: "forbidden",
+ message:
+ options?.message ??
+ "This action is not available in a staging workspace. Use the production workspace instead.",
+ });
+ }
+}
diff --git a/apps/web/ui/workspaces/delete-workspace.tsx b/apps/web/ui/workspaces/delete-workspace.tsx
index 678994f44e..619e484492 100644
--- a/apps/web/ui/workspaces/delete-workspace.tsx
+++ b/apps/web/ui/workspaces/delete-workspace.tsx
@@ -16,7 +16,7 @@ export default function DeleteWorkspace() {
action: "workspaces.write",
role,
environment,
- stagingBehavior: "blocked",
+ stagingBehavior: "production-only",
});
return (