The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
refactor permission check and remove requiredRole
Kiran Kc4dd584c7307
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/users/route.tsindex aa49f88d94..fecd016ee0 100644--- a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts+++ b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts@@ -1,4 +1,5 @@import { DubApiError } from "@/lib/api/errors";+import { throwIfNoAccess } from "@/lib/api/tokens/permissions";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";import { roles } from "@/lib/types";@@ -76,10 +77,19 @@ export const PUT = withWorkspace(},);-// DELETE /api/workspaces/[idOrSlug]/users – remove a user from a workspace+// DELETE /api/workspaces/[idOrSlug]/users – remove a user from a workspace or leave a workspaceexport const DELETE = withWorkspace(- async ({ searchParams, workspace, session }) => {+ async ({ searchParams, workspace, session, scopes }) => {const { userId } = removeUserSchema.parse(searchParams);++ if (userId !== session.user.id) {+ throwIfNoAccess({+ scopes,+ requiredScopes: ["workspaces.write"],+ workspaceId: workspace.id,+ });+ }+const [projectUser, totalOwners] = await Promise.all([prisma.projectUsers.findUnique({where: {@@ -104,12 +114,14 @@ export const DELETE = withWorkspace(},}),]);+if (!projectUser) {throw new DubApiError({code: "not_found",message: "User not found",});}+// If there is only one owner and the user is an owner and the user is trying to remove themselvesif (totalOwners === 1 &&@@ -122,6 +134,7 @@ export const DELETE = withWorkspace("Cannot remove owner from workspace. Please transfer ownership to another user first.",});}+const response = await prisma.projectUsers.delete({where: {userId_projectId: {@@ -142,7 +155,6 @@ export const DELETE = withWorkspace(return NextResponse.json(response);},{- requiredScopes: ["workspaces.write"],- allowSelf: true,+ skipScopeChecks: true,},);diff --git a/apps/web/lib/auth/workspace-edge.ts b/apps/web/lib/auth/workspace-edge.tsindex baa8882e0c..2c7925eee7 100644--- a/apps/web/lib/auth/workspace-edge.ts+++ b/apps/web/lib/auth/workspace-edge.ts@@ -52,7 +52,6 @@ export const withWorkspaceEdge = (needNotExceededClicks, // if the action needs the user to not have exceeded their clicks usageneedNotExceededLinks, // if the action needs the user to not have exceeded their links usageneedNotExceededAI, // if the action needs the user to not have exceeded their AI usage- allowSelf, // special case for removing yourself from a workspacebetaFeature, // if the action is a beta featurerequiredScopes = [],}: {@@ -60,7 +59,6 @@ export const withWorkspaceEdge = (needNotExceededClicks?: boolean;needNotExceededLinks?: boolean;needNotExceededAI?: boolean;- allowSelf?: boolean;betaFeature?: boolean;requiredScopes?: Scope[];} = {},@@ -287,17 +285,6 @@ export const withWorkspaceEdge = (}}- // workspace role checks- // if (- // !requiredRole.includes(workspace.users[0].role) &&- // !(allowSelf && searchParams.userId === session.user.id)- // ) {- // throw new DubApiError({- // code: "forbidden",- // message: "Unauthorized: Insufficient permissions.",- // });- // }-// clicks usage overage checksif (needNotExceededClicks && workspace.usage > workspace.usageLimit) {throw new DubApiError({diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.tsindex 4f0d02468d..358b163e6c 100644--- a/apps/web/lib/auth/workspace.ts+++ b/apps/web/lib/auth/workspace.ts@@ -63,21 +63,21 @@ export const withWorkspace = (needNotExceededClicks, // if the action needs the user to not have exceeded their clicks usageneedNotExceededLinks, // if the action needs the user to not have exceeded their links usageallowAnonymous, // special case for /api/links (POST /api/links) – allow no session- allowSelf, // special case for removing yourself from a workspaceskipLinkChecks, // special case for /api/links/exists – skip link checksdomainChecks, // if the action needs to check if the domain belongs to the workspacebetaFeature, // if the action is a beta featurerequiredScopes = [],+ skipScopeChecks, // if the action doesn't need to check for required scopes}: {requiredPlan?: Array<PlanProps>;needNotExceededClicks?: boolean;needNotExceededLinks?: boolean;allowAnonymous?: boolean;- allowSelf?: boolean;skipLinkChecks?: boolean;domainChecks?: boolean;betaFeature?: boolean;requiredScopes?: Scope[];+ skipScopeChecks?: boolean;} = {},) => {return async (@@ -298,11 +298,13 @@ export const withWorkspace = (}// Check user has permission to make the action- throwIfNoAccess({- scopes,- requiredScopes,- workspaceId: workspace.id,- });+ if (!skipScopeChecks) {+ throwIfNoAccess({+ scopes,+ requiredScopes,+ workspaceId: workspace.id,+ });+ }// beta feature checksif (betaFeature) {@@ -378,19 +380,6 @@ export const withWorkspace = (}}- console.log({ allowSelf });-- // workspace role checks- // if (- // !requiredRole.includes(workspace.users[0].role) &&- // !(allowSelf && searchParams.userId === session.user.id)- // ) {- // throw new DubApiError({- // code: "forbidden",- // message: "Unauthorized: Insufficient permissions.",- // });- // }-// clicks usage overage checksif (needNotExceededClicks && workspace.usage > workspace.usageLimit) {throw new DubApiError({