Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ensureCustomerExists, getItemQuantityForCustomer } from "@/lib/payments";
import { ensureClientCanAccessCustomer, ensureCustomerExists, getItemQuantityForCustomer } from "@/lib/payments";
import { getPrismaClientForTenancy } from "@/prisma-client";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { KnownErrors } from "@stackframe/stack-shared";
Expand Down Expand Up @@ -64,7 +64,16 @@ export const GET = createSmartRouteHandler({
}),
}).defined(),
}),
handler: async (req) => {
handler: async (req, fullReq) => {
if (req.auth.type === "client") {
await ensureClientCanAccessCustomer({
customerType: req.params.customer_type,
customerId: req.params.customer_id,
user: fullReq.auth?.user,
tenancy: req.auth.tenancy,
forbiddenMessage: "Clients can only access their own user or team items.",
});
}
const { tenancy } = req.auth;
const paymentsConfig = tenancy.config.payments;

Expand Down Expand Up @@ -100,5 +109,3 @@ export const GET = createSmartRouteHandler({
};
},
});


Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ensureProductIdOrInlineProduct, getOwnedProductsForCustomer, grantProductToCustomer, productToInlineProduct } from "@/lib/payments";
import { ensureClientCanAccessCustomer, ensureProductIdOrInlineProduct, getOwnedProductsForCustomer, grantProductToCustomer, productToInlineProduct } from "@/lib/payments";
import { getPrismaClientForTenancy } from "@/prisma-client";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { adaptSchema, clientOrHigherAuthTypeSchema, inlineProductSchema, serverOrHigherAuthTypeSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
Expand Down Expand Up @@ -32,7 +32,16 @@ export const GET = createSmartRouteHandler({
bodyType: yupString().oneOf(["json"]).defined(),
body: customerProductsListResponseSchema,
}),
handler: async ({ auth, params, query }) => {
handler: async ({ auth, params, query }, fullReq) => {
if (auth.type === "client") {
await ensureClientCanAccessCustomer({
customerType: params.customer_type,
customerId: params.customer_id,
user: fullReq.auth?.user,
tenancy: auth.tenancy,
forbiddenMessage: "Clients can only access their own user or team products.",
});
}
const prisma = await getPrismaClientForTenancy(auth.tenancy);
const ownedProducts = await getOwnedProductsForCustomer({
prisma,
Expand Down Expand Up @@ -191,4 +200,3 @@ export const POST = createSmartRouteHandler({
};
},
});

5 changes: 4 additions & 1 deletion apps/backend/src/lib/payments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ProductWithMetadata = yup.InferType<typeof productSchemaWithMetadata>;
type SelectedPrice = Exclude<Product["prices"], "include-by-default">[string];

export async function ensureClientCanAccessCustomer(options: {
customerType: "user" | "team",
customerType: "user" | "team" | "custom",
customerId: string,
user: UsersCrud["Admin"]["Read"] | undefined,
tenancy: Tenancy,
Expand All @@ -36,6 +36,9 @@ export async function ensureClientCanAccessCustomer(options: {
if (!currentUser) {
throw new KnownErrors.UserAuthenticationRequired();
}
if (options.customerType === "custom") {
throw new StatusError(StatusError.Forbidden, options.forbiddenMessage);
}
if (options.customerType === "user") {
if (options.customerId !== currentUser.id) {
throw new StatusError(StatusError.Forbidden, options.forbiddenMessage);
Expand Down
Loading