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.

admin changes

Steven Tey30416cc30630
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/layout-nav-client.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/layout-nav-client.tsx
index 50416c3f16..2d843f6118 100644
--- a/apps/web/app/(ee)/admin.dub.co/(dashboard)/layout-nav-client.tsx
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/layout-nav-client.tsx
@@ -1,5 +1,6 @@
"use client";
+import { canAccessAdminPath } from "@/lib/auth/admin-access-guard";
import {
ClientOnly,
MaxWidthWrapper,
@@ -7,9 +8,10 @@ import {
Popover,
useMediaQuery,
} from "@dub/ui";
+import { useSession } from "next-auth/react";
import Link from "next/link";
import { usePathname } from "next/navigation";
-import { useState } from "react";
+import { useMemo, useState } from "react";
const tabs = [
{
@@ -50,10 +52,24 @@ export function AdminNav() {
const [openPopover, setOpenPopover] = useState(false);
const { isMobile } = useMediaQuery();
const pathname = usePathname();
+ const { data: session, status } = useSession();
+
+ const visibleTabs = useMemo(
+ () =>
+ status === "loading"
+ ? []
+ : tabs.filter((tab) =>
+ canAccessAdminPath({
+ userId: session?.user?.id,
+ pathname: tab.href,
+ }),
+ ),
+ [status, session?.user?.id],
+ );
const NavContent = () => (
<div className="flex w-full flex-col gap-1 p-2">
- {tabs.map((tab) => {
+ {visibleTabs.map((tab) => {
const isActive =
pathname === tab.href || pathname?.startsWith(`${tab.href}/`);
return (
@@ -112,7 +128,7 @@ export function AdminNav() {
</div>
) : (
<div className="flex items-center gap-4">
- {tabs.map((tab) => {
+ {visibleTabs.map((tab) => {
const isActive =
pathname === tab.href ||
pathname?.startsWith(`${tab.href}/`);
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/components/ban-link.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/components/ban-link.tsx
similarity index 100%
rename from apps/web/app/(ee)/admin.dub.co/(dashboard)/components/ban-link.tsx
rename to apps/web/app/(ee)/admin.dub.co/(dashboard)/links/components/ban-link.tsx
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/components/disable-restore-workspace.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/components/disable-restore-workspace.tsx
similarity index 100%
rename from apps/web/app/(ee)/admin.dub.co/(dashboard)/components/disable-restore-workspace.tsx
rename to apps/web/app/(ee)/admin.dub.co/(dashboard)/links/components/disable-restore-workspace.tsx
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/layout.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/layout.tsx
new file mode 100644
index 0000000000..5635cb0e38
--- /dev/null
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/layout.tsx
@@ -0,0 +1,22 @@
+import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
+import { Suspense } from "react";
+import { LinksNavTabs } from "./links-nav-tabs";
+
+export default function AdminLinksLayout({
+ children,
+}: {
+ children: React.ReactNode;
+}) {
+ return (
+ <PageWidthWrapper className="pb-12 pt-4">
+ <div className="border-border-subtle overflow-hidden rounded-xl border bg-neutral-100">
+ <Suspense>
+ <LinksNavTabs />
+ <div className="border-border-subtle -mx-px -mb-px space-y-4 rounded-xl border bg-white p-4">
+ {children}
+ </div>
+ </Suspense>
+ </div>
+ </PageWidthWrapper>
+ );
+}
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/links-nav-tabs.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/links-nav-tabs.tsx
new file mode 100644
index 0000000000..5c07458ac4
--- /dev/null
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/links-nav-tabs.tsx
@@ -0,0 +1,26 @@
+"use client";
+
+import { PageNavTabs } from "@/ui/layout/page-nav-tabs";
+import { GridLayoutRows, ShieldAlert } from "@dub/ui";
+import { useMemo } from "react";
+
+export function LinksNavTabs() {
+ const tabs = useMemo(
+ () => [
+ {
+ id: "moderation",
+ label: "Moderation",
+ icon: ShieldAlert,
+ href: "/links",
+ },
+ {
+ id: "list",
+ label: "Links List",
+ icon: GridLayoutRows,
+ },
+ ],
+ [],
+ );
+
+ return <PageNavTabs basePath="/links" tabs={tabs} />;
+}
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/list/page.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/list/page.tsx
new file mode 100644
index 0000000000..655308eb39
--- /dev/null
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/list/page.tsx
@@ -0,0 +1,10 @@
+import { WorkspaceLinksList } from "app/app.dub.co/(dashboard)/[slug]/links/page-client";
+import { Suspense } from "react";
+
+export default function AdminLinksList() {
+ return (
+ <Suspense>
+ <WorkspaceLinksList />
+ </Suspense>
+ );
+}
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/page.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/page.tsx
index 8827a0a285..20ed6ddefc 100644
--- a/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/page.tsx
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/links/page.tsx
@@ -1,12 +1,23 @@
-import { WorkspaceLinksList } from "app/app.dub.co/(dashboard)/[slug]/links/page-client";
-import { Suspense } from "react";
+import { BanLink } from "./components/ban-link";
+import { DisableRestoreWorkspace } from "./components/disable-restore-workspace";
-export default function AdminLinks() {
+export default function AdminLinksPage() {
return (
- <Suspense>
- <div className="pb-12 pt-4">
- <WorkspaceLinksList />
+ <div className="mx-auto flex w-full max-w-screen-sm flex-col divide-y divide-neutral-200">
+ <div className="flex flex-col space-y-4 px-1 py-6">
+ <h2 className="text-xl font-semibold">Ban Link</h2>
+ <p className="text-sm text-neutral-500">Ban a dub.sh link</p>
+ <BanLink />
</div>
- </Suspense>
+ <div className="flex flex-col space-y-4 px-1 py-6">
+ <h2 className="text-xl font-semibold">Disable / Restore Workspace</h2>
+ <p className="text-sm text-neutral-500">
+ Disable or restore all links for a workspace. Disabling also
+ downgrades owners to billing, members to viewer, and emails workspace
+ owners. Restoring reverts those role changes.
+ </p>
+ <DisableRestoreWorkspace />
+ </div>
+ </div>
);
}
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx
index 5cb4f96b7d..cd977a5012 100644
--- a/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx
@@ -1,6 +1,4 @@
-import { BanLink } from "./components/ban-link";
import { DeletePartnerAccount } from "./components/delete-partner-account";
-import { DisableRestoreWorkspace } from "./components/disable-restore-workspace";
import { ImpersonateUser } from "./components/impersonate-user";
import { ResetLoginAttempts } from "./components/reset-login-attempts";
import { SlackSupportInvite } from "./components/slack-support-invite";
@@ -17,11 +15,6 @@ export default function AdminPage() {
</p>
<ImpersonateUser />
</div>
- <div className="flex flex-col space-y-4 px-5 py-10">
- <h2 className="text-xl font-semibold">Ban Link</h2>
- <p className="text-sm text-neutral-500">Ban a dub.sh link</p>
- <BanLink />
- </div>
<div className="flex flex-col space-y-4 px-5 py-10">
<h2 className="text-xl font-semibold">Delete Stripe Express Account</h2>
<p className="text-sm text-neutral-500">
@@ -51,15 +44,6 @@ export default function AdminPage() {
</p>
<SlackSupportInvite />
</div>
- <div className="flex flex-col space-y-4 px-5 py-10">
- <h2 className="text-xl font-semibold">Disable / Restore Workspace</h2>
- <p className="text-sm text-neutral-500">
- Disable or restore all links for a workspace. Disabling also
- downgrades owners to billing, members to viewer, and emails workspace
- owners. Restoring reverts those role changes.
- </p>
- <DisableRestoreWorkspace />
- </div>
</div>
);
}
diff --git a/apps/web/lib/auth/admin-access-guard.ts b/apps/web/lib/auth/admin-access-guard.ts
new file mode 100644
index 0000000000..a2dd4643da
--- /dev/null
+++ b/apps/web/lib/auth/admin-access-guard.ts
@@ -0,0 +1,40 @@
+// Hardcoded restriction of admin dashboard tabs per Dub admin user.
+// User IDs listed here will NOT have access to ADMIN_RESTRICTED_PATHS.
+export const ADMIN_ACCESS_BLOCKLIST: string[] = [
+ "user_1M3YFDDQ97AGTKWQ9NCSRQTYB",
+];
+
+export const ADMIN_RESTRICTED_PATHS = ["/links", "/revenue", "/domains"];
+
+const ADMIN_RESTRICTED_API_PATHS = [
+ "/api/admin/links",
+ "/api/admin/workspaces",
+ "/api/admin/revenue",
+ "/api/admin/domains",
+];
+
+const matchesPath = (pathname: string, paths: string[]) =>
+ paths.some((p) => pathname === p || pathname.startsWith(`${p}/`));
+
+export const isAdminAccessRestricted = (userId?: string | null): boolean =>
+ !!userId && ADMIN_ACCESS_BLOCKLIST.includes(userId);
+
+export const canAccessAdminPath = ({
+ userId,
+ pathname,
+}: {
+ userId?: string | null;
+ pathname: string;
+}): boolean =>
+ !isAdminAccessRestricted(userId) ||
+ !matchesPath(pathname, ADMIN_RESTRICTED_PATHS);
+
+export const canAccessAdminApiPath = ({
+ userId,
+ pathname,
+}: {
+ userId?: string | null;
+ pathname: string;
+}): boolean =>
+ !isAdminAccessRestricted(userId) ||
+ !matchesPath(pathname, ADMIN_RESTRICTED_API_PATHS);
diff --git a/apps/web/lib/auth/admin.ts b/apps/web/lib/auth/admin.ts
index 5127faafd0..d7b28ce3a4 100644
--- a/apps/web/lib/auth/admin.ts
+++ b/apps/web/lib/auth/admin.ts
@@ -1,6 +1,7 @@
import { prisma } from "@/lib/prisma";
import { DUB_WORKSPACE_ID, getSearchParams } from "@dub/utils";
import { WorkspaceRole } from "@prisma/client";
+import { canAccessAdminApiPath } from "./admin-access-guard";
import { getSession, type Session } from "./utils";
// Internal use only (for admin portal)
@@ -64,6 +65,17 @@ export const withAdmin =
);
}
+ if (
+ !canAccessAdminApiPath({
+ userId: session.user.id,
+ pathname: new URL(req.url).pathname,
+ })
+ ) {
+ return new Response("Unauthorized: You don't have access to this page.", {
+ status: 403,
+ });
+ }
+
const searchParams = getSearchParams(req.url);
return handler({
diff --git a/apps/web/lib/middleware/admin.ts b/apps/web/lib/middleware/admin.ts
index a06278046d..d8ea225719 100644
--- a/apps/web/lib/middleware/admin.ts
+++ b/apps/web/lib/middleware/admin.ts
@@ -1,3 +1,4 @@
+import { canAccessAdminPath } from "@/lib/auth/admin-access-guard";
import { prismaEdge } from "@/lib/prisma/edge";
import { DUB_WORKSPACE_ID } from "@dub/utils";
import { NextRequest, NextResponse } from "next/server";
@@ -23,7 +24,10 @@ export async function AdminMiddleware(req: NextRequest) {
if (!isAdminUser) {
return NextResponse.next(); // throw 404 page
- } else if (path === "/login") {
+ } else if (
+ path === "/login" ||
+ !canAccessAdminPath({ userId: user.id, pathname: path })
+ ) {
return NextResponse.redirect(new URL("/", req.url));
}
}
diff --git a/apps/web/prisma/schema/commission.prisma b/apps/web/prisma/schema/commission.prisma
index 8e7f50a552..60f93be6ca 100644
--- a/apps/web/prisma/schema/commission.prisma
+++ b/apps/web/prisma/schema/commission.prisma
@@ -82,5 +82,4 @@ model Commission {
@@index(status)
@@index(rewardId)
@@index(userId)
- @@index([status, payoutId, source]) // temp index for migration script
}
diff --git a/apps/web/ui/layout/page-nav-tabs.tsx b/apps/web/ui/layout/page-nav-tabs.tsx
index 156dfb3c16..cc79f5d6e1 100644
--- a/apps/web/ui/layout/page-nav-tabs.tsx
+++ b/apps/web/ui/layout/page-nav-tabs.tsx
@@ -17,6 +17,7 @@ export type PageNavTabsTab = {
label: string;
icon: Icon;
badge?: string | number;
+ href?: string;
};
export type PageNavTabsQuicklink = {
@@ -81,7 +82,7 @@ export function PageNavTabs({
layout
className={cn("relative z-0 inline-flex items-center gap-1")}
>
- {tabs.map(({ id, label, icon: Icon, badge }, idx) => {
+ {tabs.map(({ id, label, icon: Icon, badge, href }, idx) => {
// if it's the base path, match the first tab
const isSelected =
pathname.endsWith(basePath) && idx === 0
@@ -90,7 +91,7 @@ export function PageNavTabs({
return (
<Link
key={id}
- href={`${basePath}/${id}${
+ href={`${href ?? `${basePath}/${id}`}${
preservedQueryParams
? getQueryString(
{},