mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-30 04:12:52 +09:00
fix: Use project owner plan limits when project is shared via workspace (#5751)
This commit is contained in:
1 parent
7046cdcee9
commit
9c2116ade2
21 files changed
+725
-181
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
type AppContext,
|
||||
AuthorizationError,
|
||||
getPlanFeaturesByOwnerId,
|
||||
} from "@webstudio-is/trpc-interface/index.server";
|
||||
import {
|
||||
type NotificationType,
|
||||
@@ -590,7 +591,7 @@ const performAcceptSideEffect = async ({
|
||||
}
|
||||
|
||||
// Check receiver's plan limit before accepting the transfer
|
||||
const receiverPlan = await context.getOwnerPlanFeatures(userId);
|
||||
const receiverPlan = await getPlanFeaturesByOwnerId(userId, context);
|
||||
const projectCount = await context.postgrest.client
|
||||
.from("Project")
|
||||
.select("id", { count: "exact", head: true })
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
authorizeProject,
|
||||
type AppContext,
|
||||
AuthorizationError,
|
||||
getPlanFeaturesByOwnerId,
|
||||
} from "@webstudio-is/trpc-interface/index.server";
|
||||
import { createBuild } from "@webstudio-is/project-build/index.server";
|
||||
import { Title } from "../shared/project-schema";
|
||||
@@ -150,7 +151,7 @@ export const create = async (
|
||||
}
|
||||
|
||||
// Enforce the per-user project limit before creating anything
|
||||
const ownerPlan = await context.getOwnerPlanFeatures(projectOwnerUserId);
|
||||
const ownerPlan = await getPlanFeaturesByOwnerId(projectOwnerUserId, context);
|
||||
const projectCountResult = await context.postgrest.client
|
||||
.from("Project")
|
||||
.select("id", { count: "exact", head: true })
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Database } from "@webstudio-is/postgrest/index.server";
|
||||
import {
|
||||
type AppContext,
|
||||
AuthorizationError,
|
||||
getPlanFeaturesByOwnerId,
|
||||
} from "@webstudio-is/trpc-interface/index.server";
|
||||
import { softDeleteProject } from "./project";
|
||||
import { defaultRole, type Role } from "@webstudio-is/trpc-interface/authorize";
|
||||
@@ -247,7 +248,7 @@ export const findMany = async (userId: string, context: AppContext) => {
|
||||
// break the entire workspace list. Failed checks default to not-downgraded.
|
||||
const checks = await Promise.allSettled(
|
||||
ownerIds.map(async (ownerId) => {
|
||||
const plan = await context.getOwnerPlanFeatures(ownerId);
|
||||
const plan = await getPlanFeaturesByOwnerId(ownerId, context);
|
||||
if (plan.maxWorkspaces <= 1) {
|
||||
return { ownerId, isDowngraded: true };
|
||||
}
|
||||
@@ -281,7 +282,7 @@ export const findMany = async (userId: string, context: AppContext) => {
|
||||
return [...ownedWithRelation, ...memberWithRelation];
|
||||
};
|
||||
|
||||
const assertOwner = async (
|
||||
const assertWorkspaceOwnerByUserId = async (
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
context: AppContext
|
||||
@@ -304,6 +305,25 @@ const assertOwner = async (
|
||||
return workspace.data;
|
||||
};
|
||||
|
||||
export const assertWorkspaceOwner = async (
|
||||
workspaceId: string,
|
||||
context: AppContext
|
||||
) => {
|
||||
const userId = assertUser(context);
|
||||
return await assertWorkspaceOwnerByUserId(workspaceId, userId, context);
|
||||
};
|
||||
|
||||
export const assertWorkspaceOwnerPlan = async (
|
||||
workspaceId: string,
|
||||
context: AppContext
|
||||
) => {
|
||||
const workspace = await assertWorkspaceOwner(workspaceId, context);
|
||||
return {
|
||||
workspace,
|
||||
plan: await getPlanFeaturesByOwnerId(workspace.userId, context),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Count all current members (non-removed) plus pending invites across all
|
||||
* non-deleted workspaces owned by userId.
|
||||
@@ -368,7 +388,7 @@ export const addMember = async (
|
||||
context: AppContext
|
||||
): Promise<{ notificationId: string }> => {
|
||||
const userId = assertUser(context);
|
||||
await assertOwner(workspaceId, userId, context);
|
||||
await assertWorkspaceOwnerByUserId(workspaceId, userId, context);
|
||||
|
||||
// Look up the user by email
|
||||
const user = await context.postgrest.client
|
||||
@@ -433,7 +453,7 @@ export const updateRole = async (
|
||||
context: AppContext
|
||||
) => {
|
||||
const userId = assertUser(context);
|
||||
await assertOwner(workspaceId, userId, context);
|
||||
await assertWorkspaceOwnerByUserId(workspaceId, userId, context);
|
||||
|
||||
if (memberUserId === userId) {
|
||||
throw new Error("Cannot change the workspace owner's role");
|
||||
@@ -484,7 +504,7 @@ export const removeMember = async (
|
||||
}
|
||||
} else {
|
||||
// Only the owner can remove other members.
|
||||
await assertOwner(workspaceId, userId, context);
|
||||
await assertWorkspaceOwnerByUserId(workspaceId, userId, context);
|
||||
}
|
||||
|
||||
const result = await context.postgrest.client
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import {
|
||||
createTestServer,
|
||||
db,
|
||||
json,
|
||||
testContext,
|
||||
} from "@webstudio-is/postgrest/testing";
|
||||
import {
|
||||
createCallerFactory,
|
||||
type AppContext,
|
||||
} from "@webstudio-is/trpc-interface/index.server";
|
||||
import { defaultPlanFeatures } from "@webstudio-is/plans";
|
||||
import { projectRouter } from "./project-router";
|
||||
|
||||
const server = createTestServer();
|
||||
const createCaller = createCallerFactory(projectRouter);
|
||||
|
||||
const createContext = (
|
||||
overrides: Partial<Pick<AppContext, "authorization" | "planFeatures">> = {}
|
||||
): AppContext =>
|
||||
({
|
||||
...testContext,
|
||||
authorization: { type: "user", userId: "member-1" },
|
||||
planFeatures: defaultPlanFeatures,
|
||||
getOwnerPlanFeatures: () =>
|
||||
Promise.resolve({
|
||||
...defaultPlanFeatures,
|
||||
maxWorkspaces: 20,
|
||||
}),
|
||||
...overrides,
|
||||
}) as unknown as AppContext;
|
||||
|
||||
describe("userPublishCount", () => {
|
||||
test("counts publishes for the project owner when called by a workspace member", async () => {
|
||||
server.use(
|
||||
db.get("Project", ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
if (url.searchParams.has("userId")) {
|
||||
return json(null);
|
||||
}
|
||||
return json({ userId: "owner-1" });
|
||||
}),
|
||||
db.get("WorkspaceProjectAuthorization", () =>
|
||||
json([{ relation: "editors" }])
|
||||
),
|
||||
db.get("user_publish_count", ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
expect(url.searchParams.get("user_id")).toBe("eq.owner-1");
|
||||
return json({ count: 42 });
|
||||
})
|
||||
);
|
||||
|
||||
const caller = createCaller(createContext());
|
||||
const result = await caller.userPublishCount({ projectId: "proj-1" });
|
||||
|
||||
expect(result).toEqual({ success: true, data: 42 });
|
||||
});
|
||||
|
||||
test("counts publishes for the caller when projectId is omitted", async () => {
|
||||
server.use(
|
||||
db.get("user_publish_count", ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
expect(url.searchParams.get("user_id")).toBe("eq.member-1");
|
||||
return json({ count: 7 });
|
||||
})
|
||||
);
|
||||
|
||||
const caller = createCaller(createContext());
|
||||
const result = await caller.userPublishCount();
|
||||
|
||||
expect(result).toEqual({ success: true, data: 7 });
|
||||
});
|
||||
|
||||
test("uses token owner when projectId is omitted for token auth", async () => {
|
||||
server.use(
|
||||
db.get("user_publish_count", ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
expect(url.searchParams.get("user_id")).toBe("eq.token-owner");
|
||||
return json({ count: 9 });
|
||||
})
|
||||
);
|
||||
|
||||
const caller = createCaller(
|
||||
createContext({
|
||||
authorization: {
|
||||
type: "token",
|
||||
authToken: "token-1",
|
||||
ownerId: "token-owner",
|
||||
},
|
||||
})
|
||||
);
|
||||
const result = await caller.userPublishCount();
|
||||
|
||||
expect(result).toEqual({ success: true, data: 9 });
|
||||
});
|
||||
|
||||
test("does not count publishes when caller cannot view the project", async () => {
|
||||
let publishCountQueried = false;
|
||||
|
||||
server.use(
|
||||
db.get("Project", () => json(null)),
|
||||
db.get("WorkspaceProjectAuthorization", () => json([])),
|
||||
db.get("user_publish_count", () => {
|
||||
publishCountQueried = true;
|
||||
return json({ count: 0 });
|
||||
})
|
||||
);
|
||||
|
||||
const caller = createCaller(createContext());
|
||||
const result = await caller.userPublishCount({ projectId: "private" });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
if ("error" in result) {
|
||||
expect(result.error).toContain("Not authorized");
|
||||
}
|
||||
expect(publishCountQueried).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
AuthorizationError,
|
||||
authorizeProject,
|
||||
createErrorResponse,
|
||||
getProjectOwnerId,
|
||||
} from "@webstudio-is/trpc-interface/index.server";
|
||||
import { Title } from "../shared/project-schema";
|
||||
import { MarketplaceApprovalStatus } from "../shared/marketplace-schema";
|
||||
@@ -84,34 +85,54 @@ export const projectRouter = router({
|
||||
return projectIds.map((project) => project.id);
|
||||
}),
|
||||
|
||||
userPublishCount: procedure.query(async ({ ctx }) => {
|
||||
try {
|
||||
if (
|
||||
ctx.authorization.type !== "user" &&
|
||||
ctx.authorization.type !== "token"
|
||||
) {
|
||||
throw new Error("Not authorized");
|
||||
userPublishCount: procedure
|
||||
.input(z.object({ projectId: z.string() }).optional())
|
||||
.query(async ({ input, ctx }) => {
|
||||
try {
|
||||
if (
|
||||
ctx.authorization.type !== "user" &&
|
||||
ctx.authorization.type !== "token"
|
||||
) {
|
||||
throw new Error("Not authorized");
|
||||
}
|
||||
const userId =
|
||||
ctx.authorization.type === "user"
|
||||
? ctx.authorization.userId
|
||||
: ctx.authorization.ownerId;
|
||||
|
||||
let ownerId = userId;
|
||||
|
||||
if (input?.projectId !== undefined) {
|
||||
const canView = await authorizeProject.hasProjectPermit(
|
||||
{ projectId: input.projectId, permit: "view" },
|
||||
ctx
|
||||
);
|
||||
|
||||
if (canView === false) {
|
||||
throw new AuthorizationError(
|
||||
"Not authorized to access this project"
|
||||
);
|
||||
}
|
||||
|
||||
ownerId = await getProjectOwnerId(input.projectId, ctx);
|
||||
}
|
||||
|
||||
const result = await ctx.postgrest.client
|
||||
.from("user_publish_count")
|
||||
.select("count")
|
||||
.eq("user_id", ownerId)
|
||||
.maybeSingle();
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
data: result.data?.count ?? 0,
|
||||
};
|
||||
} catch (error) {
|
||||
return createErrorResponse(error);
|
||||
}
|
||||
const userId =
|
||||
ctx.authorization.type === "user"
|
||||
? ctx.authorization.userId
|
||||
: ctx.authorization.ownerId;
|
||||
const result = await ctx.postgrest.client
|
||||
.from("user_publish_count")
|
||||
.select("count")
|
||||
.eq("user_id", userId)
|
||||
.maybeSingle();
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
data: result.data?.count ?? 0,
|
||||
};
|
||||
} catch (error) {
|
||||
return createErrorResponse(error);
|
||||
}
|
||||
}),
|
||||
}),
|
||||
|
||||
publishedBuilds: procedure
|
||||
.input(
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
router,
|
||||
procedure,
|
||||
createErrorResponse,
|
||||
getPlanFeaturesByOwnerId,
|
||||
} from "@webstudio-is/trpc-interface/index.server";
|
||||
import { roles } from "@webstudio-is/trpc-interface/authorize";
|
||||
|
||||
@@ -77,7 +78,12 @@ export const workspaceRouter = router({
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
try {
|
||||
if (ctx.planFeatures.maxWorkspaces <= 1) {
|
||||
const { plan } = await workspaceApi.assertWorkspaceOwnerPlan(
|
||||
input.workspaceId,
|
||||
ctx
|
||||
);
|
||||
|
||||
if (plan.maxWorkspaces <= 1) {
|
||||
throw new Error("Upgrade your plan to invite members to workspaces.");
|
||||
}
|
||||
|
||||
@@ -98,7 +104,12 @@ export const workspaceRouter = router({
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
try {
|
||||
if (ctx.planFeatures.maxWorkspaces <= 1) {
|
||||
const { plan } = await workspaceApi.assertWorkspaceOwnerPlan(
|
||||
input.workspaceId,
|
||||
ctx
|
||||
);
|
||||
|
||||
if (plan.maxWorkspaces <= 1) {
|
||||
throw new Error(
|
||||
"Upgrade your plan to manage workspace member roles."
|
||||
);
|
||||
@@ -158,8 +169,9 @@ export const workspaceRouter = router({
|
||||
throw new Error("Target workspace not found");
|
||||
}
|
||||
|
||||
const ownerPlan = await ctx.getOwnerPlanFeatures(
|
||||
targetWorkspace.data.userId
|
||||
const ownerPlan = await getPlanFeaturesByOwnerId(
|
||||
targetWorkspace.data.userId,
|
||||
ctx
|
||||
);
|
||||
|
||||
if (ownerPlan.maxWorkspaces <= 1) {
|
||||
|
||||
Reference in new issue
Block a user