Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
Open Tokens settings for Data Rooms plans (remove feature flag gate)
Marc Seitz7fc0fed46549
diff --git a/components/settings/settings-header.tsx b/components/settings/settings-header.tsxindex b578681f..d87a1359 100644--- a/components/settings/settings-header.tsx+++ b/components/settings/settings-header.tsx@@ -1,11 +1,14 @@import { useFeatureFlags } from "@/lib/hooks/use-feature-flags";import { useIsAdmin } from "@/lib/hooks/use-is-admin";+import { usePlan } from "@/lib/swr/use-billing";import { NavMenu } from "../navigation-menu";export function SettingsHeader() {const { features } = useFeatureFlags();const { isAdmin } = useIsAdmin();+ const { isDatarooms, isTrial } = usePlan();+ const hasTokensAccess = isDatarooms || isTrial;return (<header>@@ -77,7 +80,7 @@ export function SettingsHeader() {label: "Tokens",href: `/settings/tokens`,segment: "tokens",- disabled: !features?.tokens,+ disabled: !hasTokensAccess,},{label: "API",diff --git a/pages/api/teams/[teamId]/tokens/index.ts b/pages/api/teams/[teamId]/tokens/index.tsindex df41f800..6d9059cb 100644--- a/pages/api/teams/[teamId]/tokens/index.ts+++ b/pages/api/teams/[teamId]/tokens/index.ts@@ -8,7 +8,6 @@ import {parseRestrictedTokenSubjectType,} from "@/lib/api/auth/restricted-tokens";import { hashToken } from "@/lib/api/auth/token";-import { getFeatureFlags } from "@/lib/featureFlags";import { newId } from "@/lib/id-helper";import { GRANULAR_SCOPES, PRESET_SCOPES } from "@/lib/oauth/scopes";import prisma from "@/lib/prisma";@@ -20,37 +19,38 @@ export default async function handle() {const { teamId } = req.query as { teamId: string };- const features = await getFeatureFlags({ teamId });- if (!features.tokens) {+ // Authenticate and verify team membership before anything else, so we never+ // leak team details (including plan) to unauthenticated callers or non-members.+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).json({ error: "Unauthorized" });+ }+ const userId = (session.user as CustomUser).id;++ const userTeam = await prisma.userTeam.findUnique({+ where: { userId_teamId: { userId, teamId } },+ select: { role: true },+ });+ if (!userTeam) {+ return res.status(403).json({ error: "Unauthorized" });+ }++ // Tokens require a Data Rooms plan or higher (or a Data Rooms trial).+ const team = await prisma.team.findUnique({+ where: { id: teamId },+ select: { plan: true },+ });+ const plan = team?.plan ?? "";+ const hasTokensAccess =+ plan.includes("datarooms") || plan.includes("drtrial");+ if (!hasTokensAccess) {return res.status(403)- .json({ error: "This feature is not available for your team" });+ .json({ error: "This feature requires a Data Rooms plan or higher." });}if (req.method === "GET") {try {- const session = await getServerSession(req, res, authOptions);- if (!session) {- return res.status(401).json({ error: "Unauthorized" });- }-- const { teamId } = req.query as { teamId: string };- const userId = (session.user as CustomUser).id;-- // Check if user is in team- const userTeam = await prisma.userTeam.findUnique({- where: {- userId_teamId: {- userId,- teamId,- },- },- });-- if (!userTeam) {- return res.status(403).json({ error: "Unauthorized" });- }-// Fetch tokensconst tokens = await prisma.restrictedToken.findMany({where: {@@ -89,13 +89,8 @@ export default async function handle(return res.status(500).json({ error: "Error fetching tokens" });}} else if (req.method === "POST") {- const session = await getServerSession(req, res, authOptions);- if (!session) {- return res.status(401).json({ error: "Unauthorized" });- }-- const { teamId } = req.query as { teamId: string };- const userId = (session.user as CustomUser).id;+ // Any team member can create a token. The resulting key is still scoped to+ // the team and limited by the scopes selected on creation.const {name,scopes,@@ -107,24 +102,6 @@ export default async function handle(};try {- // Membership check — any role in the team can create a token. The- // resulting key is still scoped to the team and limited by the scopes- // selected on creation.- const userTeam = await prisma.userTeam.findUnique({- where: {- userId_teamId: {- userId,- teamId,- },- },- select: {- role: true,- },- });- if (!userTeam) {- return res.status(403).json({ error: "Unauthorized" });- }-// Validate name up front so a missing / non-string / whitespace-only// value produces a 400 instead of bubbling up as a Prisma 500.if (typeof name !== "string" || name.trim().length === 0) {@@ -218,12 +195,6 @@ export default async function handle(}} else if (req.method === "PATCH") {try {- const session = await getServerSession(req, res, authOptions);- if (!session) {- return res.status(401).json({ error: "Unauthorized" });- }-- const { teamId } = req.query as { teamId: string };const {tokenId,name,@@ -233,20 +204,11 @@ export default async function handle(name?: string;scopes?: string[] | string;};- const userId = (session.user as CustomUser).id;if (!tokenId || typeof tokenId !== "string") {return res.status(400).json({ error: "tokenId is required" });}- const userTeam = await prisma.userTeam.findUnique({- where: { userId_teamId: { userId, teamId } },- select: { role: true },- });- if (!userTeam) {- return res.status(403).json({ error: "Unauthorized" });- }-const existing = await prisma.restrictedToken.findFirst({where: { id: tokenId, teamId, source: "dashboard" },select: { id: true, userId: true },@@ -321,30 +283,10 @@ export default async function handle(}} else if (req.method === "DELETE") {try {- const session = await getServerSession(req, res, authOptions);- if (!session) {- return res.status(401).json({ error: "Unauthorized" });- }-- const { teamId } = req.query as { teamId: string };const { tokenId } = req.body;- const userId = (session.user as CustomUser).id;-- // Check if user is in team and has admin role- const { role } = await prisma.userTeam.findUniqueOrThrow({- where: {- userId_teamId: {- userId,- teamId,- },- },- select: {- role: true,- },- });// Only admins can delete tokens- if (role !== "ADMIN") {+ if (userTeam.role !== "ADMIN") {return res.status(403).json({error:"You don't have the permissions to delete a token. Please contact your administrator.",diff --git a/pages/settings/tokens.tsx b/pages/settings/tokens.tsxindex 3a40e573..3c1b1fff 100644--- a/pages/settings/tokens.tsx+++ b/pages/settings/tokens.tsx@@ -14,6 +14,7 @@ import {import { toast } from "sonner";import useSWR from "swr";+import { usePlan } from "@/lib/swr/use-billing";import { cn, fetcher, timeAgo } from "@/lib/utils";import AppLayout from "@/components/layouts/app";@@ -62,17 +63,17 @@ export default function TokenSettings() {const teamId = teamInfo?.currentTeam?.id;const router = useRouter();- const { data: features } = useSWR<{ tokens: boolean }>(- teamId ? `/api/feature-flags?teamId=${teamId}` : null,- fetcher,- );+ const { isDatarooms, isTrial, loading: planLoading } = usePlan();+ const hasTokensAccess = isDatarooms || isTrial;useEffect(() => {- if (features && !features.tokens) {+ // `planLoading` is only meaningful once we have a teamId.+ if (!teamId || planLoading) return;+ if (!hasTokensAccess) {router.push("/settings/general");- toast.error("This feature is not available for your team");+ toast.error("This feature requires a Data Rooms plan or higher");}- }, [features, router]);+ }, [teamId, planLoading, hasTokensAccess, router]);const {data: tokens,