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.

Use restrictedEnvironments for staging workspace client access checks

Kiran K4e7b0de8cf81
diff --git a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsx b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsx
index ded02cfdf4..86e88509d9 100644
--- a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsx
@@ -170,7 +170,10 @@ export default async function SuccessPage(props: {
src={program.logo || `${OG_AVATAR_URL}${program.name}`}
alt={`${program.name} logo`}
/>
- <Bolt variant="fill" className="shrink-0 text-[var(--brand)] opacity-30" />
+ <Bolt
+ variant="fill"
+ className="shrink-0 text-[var(--brand)] opacity-30"
+ />
<Logo className="size-10 shrink-0" />
</div>
)}
diff --git a/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx b/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
index e670321782..d4416884f0 100644
--- a/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
@@ -117,15 +117,15 @@ async function VerifyInvite({ code }: { code: string }) {
});
}
- after(async () => {
- await addMemberToStaging({
+ after(
+ addMemberToStaging({
workspace,
user: {
id: workspaceUser.userId,
role: workspaceUser.role,
},
- });
- });
+ }),
+ );
// Complete onboarding just in case
await onboardingStepCache.set({
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx
index aedae99723..9b20214830 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx
@@ -39,7 +39,7 @@ import {
UserCheck,
} from "@dub/ui/icons";
import { cn, fetcher, timeAgo } from "@dub/utils";
-import { WorkspaceRole } from "@prisma/client";
+import { WorkspaceEnvironment, WorkspaceRole } from "@prisma/client";
import { ColumnDef, Row } from "@tanstack/react-table";
import { Command } from "cmdk";
import { UserMinus } from "lucide-react";
@@ -276,7 +276,7 @@ export default function WorkspaceMembersPage() {
role,
environment,
customPermissionDescription: "invite new teammates",
- stagingBehavior: "production-only",
+ restrictedEnvironments: [WorkspaceEnvironment.staging],
});
const { error: generateInviteLinksError } = clientAccessCheck({
@@ -284,7 +284,7 @@ export default function WorkspaceMembersPage() {
role,
environment,
customPermissionDescription: "generate invite links",
- stagingBehavior: "production-only",
+ restrictedEnvironments: [WorkspaceEnvironment.staging],
});
return (
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 bc09c2ea2d..9f34fc2622 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
@@ -6,6 +6,7 @@ import DeleteWorkspace from "@/ui/workspaces/delete-workspace";
import { SlackSupportSettingsCard } from "@/ui/workspaces/slack-support-settings-card";
import UploadLogo from "@/ui/workspaces/upload-logo";
import { Form } from "@dub/ui";
+import { WorkspaceEnvironment } from "@prisma/client";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
@@ -19,7 +20,7 @@ export default function WorkspaceSettingsClient() {
action: "workspaces.write",
role,
environment,
- stagingBehavior: "production-only",
+ restrictedEnvironments: [WorkspaceEnvironment.staging],
});
const { update } = useSession();
diff --git a/apps/web/lib/client-access-check.ts b/apps/web/lib/client-access-check.ts
index cb4f48d266..f992458546 100644
--- a/apps/web/lib/client-access-check.ts
+++ b/apps/web/lib/client-access-check.ts
@@ -1,35 +1,47 @@
import { combineWords } from "@dub/utils";
import { WorkspaceEnvironment, WorkspaceRole } from "@prisma/client";
import { PermissionAction, ROLE_PERMISSIONS } from "./api/rbac/permissions";
-import { isStagingEnvironment } from "./sandbox/workspace-guards";
+
+function getDefaultRestrictedEnvironmentMessage(
+ restrictedEnvironments: WorkspaceEnvironment[],
+) {
+ if (
+ restrictedEnvironments.length === 1 &&
+ restrictedEnvironments[0] === WorkspaceEnvironment.staging
+ ) {
+ return "This setting must be managed from the production workspace.";
+ }
+
+ const labels = restrictedEnvironments.map((env) => `${env} workspaces`);
+ return `This action is not available in ${combineWords(labels)}.`;
+}
export const clientAccessCheck = ({
action,
role,
customPermissionDescription,
environment,
- stagingBehavior,
+ restrictedEnvironments,
+ restrictedEnvironmentMessage,
}: {
action: PermissionAction;
role: WorkspaceRole;
customPermissionDescription?: string;
- environment?: WorkspaceEnvironment | null;
- stagingBehavior?: "blocked" | "production-only";
+ environment?: WorkspaceEnvironment | null; // Current workspace environment
+ restrictedEnvironments?: WorkspaceEnvironment[]; // Environments where the action is restricted
+ restrictedEnvironmentMessage?: string; // Custom message to display when the action is restricted
}) => {
- if (isStagingEnvironment(environment)) {
- switch (stagingBehavior) {
- case "blocked":
- return {
- allowed: false,
- error: "This action is not available in staging workspaces.",
- };
-
- case "production-only":
- return {
- allowed: false,
- error: "This setting must be managed from the production workspace.",
- };
- }
+ if (
+ environment &&
+ restrictedEnvironments &&
+ restrictedEnvironments.includes(environment)
+ ) {
+ return {
+ allowed: false,
+ error:
+ restrictedEnvironmentMessage ??
+ getDefaultRestrictedEnvironmentMessage(restrictedEnvironments),
+ };
}
const permission = ROLE_PERMISSIONS.find((p) => p.action === action)!;
diff --git a/apps/web/ui/workspaces/delete-workspace.tsx b/apps/web/ui/workspaces/delete-workspace.tsx
index a3fdcd0fb3..e3a61f6df4 100644
--- a/apps/web/ui/workspaces/delete-workspace.tsx
+++ b/apps/web/ui/workspaces/delete-workspace.tsx
@@ -5,6 +5,7 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { useDeleteWorkspaceModal } from "@/ui/modals/delete-workspace-modal";
import { Button } from "@dub/ui";
import { cn } from "@dub/utils";
+import { WorkspaceEnvironment } from "@prisma/client";
export default function DeleteWorkspace() {
const { setShowDeleteWorkspaceModal, DeleteWorkspaceModal } =
@@ -16,7 +17,7 @@ export default function DeleteWorkspace() {
action: "workspaces.write",
role,
environment,
- stagingBehavior: "production-only",
+ restrictedEnvironments: [WorkspaceEnvironment.staging],
});
return (
diff --git a/apps/web/ui/workspaces/upload-logo.tsx b/apps/web/ui/workspaces/upload-logo.tsx
index 988bfce00c..51b4196a80 100644
--- a/apps/web/ui/workspaces/upload-logo.tsx
+++ b/apps/web/ui/workspaces/upload-logo.tsx
@@ -3,6 +3,7 @@
import { clientAccessCheck } from "@/lib/client-access-check";
import useWorkspace from "@/lib/swr/use-workspace";
import { Button, FileUpload } from "@dub/ui";
+import { WorkspaceEnvironment } from "@prisma/client";
import { useEffect, useState } from "react";
import { toast } from "sonner";
import { mutate } from "swr";
@@ -20,7 +21,7 @@ export default function UploadLogo() {
action: "workspaces.write",
role,
environment,
- stagingBehavior: "production-only",
+ restrictedEnvironments: [WorkspaceEnvironment.staging],
});
return (