Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withProgram #1994

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions apps/web/lib/auth/partner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ interface WithPartnerProfileHandler {
}): Promise<Response>;
}

export const withPartnerProfile = (
handler: WithPartnerProfileHandler,
{}: {} = {},
) => {
export const withPartnerProfile = (handler: WithPartnerProfileHandler) => {
return withAxiom(
async (
req: AxiomRequest,
Expand Down
98 changes: 98 additions & 0 deletions apps/web/lib/auth/program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { DubApiError } from "@/lib/api/errors";
import { ProgramProps, WorkspaceWithUsers } from "@/lib/types";
import { getProgramOrThrow } from "../api/programs/get-program-or-throw";
import { PermissionAction } from "../api/rbac/permissions";
import { parseRequestBody } from "../api/utils";
import { Session } from "./utils";
import { withWorkspace } from "./workspace";

export interface WithProgramHandler {
(args: {
req: Request;
body: any;
params: Record<string, string>;
searchParams: Record<string, string>;
headers?: Record<string, string>;
session: Session;
permissions: PermissionAction[];
workspace: WorkspaceWithUsers;
program: ProgramProps;
}): Promise<Response>;
}

interface WithProgramOptions {
requiredPermissions?: PermissionAction[];
includeDiscounts?: boolean;
}

/**
* withProgram wraps a handler with both workspace-level and program-level
* access control.
*
* It first uses withWorkspace to perform all the workspace and API key checks.
* Then it retrieves the program (using getProgramOrThrow) based on the program
* identifier (which should be supplied in `params.programId` or `searchParams.programId`).
* If the program does not exist or the user does not have access, an error will be thrown.
*/
export const withProgram = (
handler: WithProgramHandler,
options?: WithProgramOptions,
) => {
return withWorkspace(
async ({
req,
params,
searchParams,
headers,
session,
workspace,
permissions,
}) => {
const body = req.body ? await parseRequestBody(req) : undefined;

const programId =
params.programId || searchParams.programId || body?.programId;

if (!programId) {
throw new DubApiError({
code: "bad_request",
message:
"Program ID not found. Did you forget to include a `programId` query parameter?",
});
}

const program = await getProgramOrThrow(
{
programId,
workspaceId: workspace.id,
},
{
...(options?.includeDiscounts && { includeDiscounts: true }),
},
);

return await handler({
req,
body,
params,
searchParams,
headers,
session,
permissions,
workspace,
program,
});
},

{
...options,
requiredPlan: [
"business",
"business extra",
"business max",
"business plus",
"enterprise",
],
},
);
};
Loading