Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Show preview initially for view only users (#940)

Ivan Starkov50e17d44eee5
diff --git a/.gitignore b/.gitignore
index 7730c73eb7..6c2c9cda16 100644
--- a/.gitignore
+++ b/.gitignore
@@ -50,3 +50,6 @@ packages/**/lib
generated
storybook-static
tsconfig.tsbuildinfo
+
+# to save thunder https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client files
+.thunder
\ No newline at end of file
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index 6dadecdc3d..e887e7111b 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -29,7 +29,11 @@ import {
Workspace,
} from "./features/workspace";
import { usePublishShortcuts } from "./shared/shortcuts";
-import { useDragAndDropState, useIsPreviewMode } from "~/shared/nano-states";
+import {
+ useDragAndDropState,
+ useIsPreviewMode,
+ useSetIsPreviewMode,
+} from "~/shared/nano-states";
import { useClientSettings } from "./shared/client-settings";
import { Navigator } from "./features/sidebar-left";
import { getBuildUrl } from "~/shared/router-utils";
@@ -241,6 +245,7 @@ export type DesignerProps = {
buildOrigin: string;
authReadToken: string;
authToken?: string;
+ authPermit: "view" | "build" | "own";
};
export const Designer = ({
@@ -252,6 +257,7 @@ export const Designer = ({
buildOrigin,
authReadToken,
authToken,
+ authPermit,
}: DesignerProps) => {
useSubscribeBreakpoints();
useSetProject(project);
@@ -260,6 +266,7 @@ export const Designer = ({
const [publish, publishRef] = usePublish();
useDesignerStore(publish);
useSyncServer({ buildId, treeId, projectId: project.id, authToken });
+ useSetIsPreviewMode(authPermit === "view");
const [isPreviewMode] = useIsPreviewMode();
usePublishShortcuts(publish);
const onRefReadCanvasWidth = useUpdateCanvasWidth();
diff --git a/apps/designer/app/routes/designer/$projectId.tsx b/apps/designer/app/routes/designer/$projectId.tsx
index 8d47be39ea..88ceb7e735 100644
--- a/apps/designer/app/routes/designer/$projectId.tsx
+++ b/apps/designer/app/routes/designer/$projectId.tsx
@@ -7,6 +7,7 @@ import { sentryException } from "~/shared/sentry";
import { getBuildOrigin } from "~/shared/router-utils";
import { createContext, createAuthReadToken } from "~/shared/context.server";
import type { ShouldRevalidateFunction } from "@remix-run/react";
+import { authorizeProject } from "@webstudio-is/trpc-interface/server";
export { links };
@@ -25,6 +26,17 @@ export const loader = async ({
const project = await db.project.loadById(params.projectId, context);
+ // At this point we already knew that if project loaded we have at least "view" permit
+ // having that getProjectPermit is heavy operation we can skip check "view" permit
+ const authPermit =
+ (await authorizeProject.getProjectPermit(
+ {
+ projectId: project.id,
+ permits: ["own", "build"],
+ },
+ context
+ )) ?? "view";
+
if (project === null) {
throw new Error(`Project "${params.projectId}" not found`);
}
@@ -47,6 +59,7 @@ export const loader = async ({
buildOrigin: getBuildOrigin(request),
authReadToken,
authToken,
+ authPermit,
};
};
diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.ts
index 73424b6df3..c978b2a95a 100644
--- a/apps/designer/app/shared/nano-states/nano-states.ts
+++ b/apps/designer/app/shared/nano-states/nano-states.ts
@@ -259,6 +259,11 @@ export const hoveredInstanceOutlineStore = atom<
export const isPreviewModeStore = atom<boolean>(false);
export const useIsPreviewMode = () => useValue(isPreviewModeStore);
+export const useSetIsPreviewMode = (isPreviewMode: boolean) => {
+ useSyncInitializeOnce(() => {
+ isPreviewModeStore.set(isPreviewMode);
+ });
+};
const selectedInstanceOutlineContainer = atom<{
visible: boolean;
diff --git a/packages/trpc-interface/src/authorize/project.server.ts b/packages/trpc-interface/src/authorize/project.server.ts
index e3340e4994..bcdf6c237f 100644
--- a/packages/trpc-interface/src/authorize/project.server.ts
+++ b/packages/trpc-interface/src/authorize/project.server.ts
@@ -1,3 +1,4 @@
+import type { Project } from "@webstudio-is/prisma-client";
import type { AppContext } from "../context/context.server";
/**
@@ -28,10 +29,12 @@ export const registerProjectOwner = async (
});
};
+type Permit = "view" | "edit" | "build" | "own";
+
export const hasProjectPermit = async (
props: {
- projectId: string;
- permit: "view" | "edit" | "own";
+ projectId: Project["id"];
+ permit: Permit;
},
context: AppContext
) => {
@@ -105,3 +108,34 @@ export const hasProjectPermit = async (
return allowed;
};
+
+/**
+ * Returns the first allowed permit from the list or undefined if none is allowed
+ * @todo think about caching to authorizeTrpc.check.query
+ * batching check queries would help too https://github.com/ory/keto/issues/812
+ */
+export const getProjectPermit = async <T extends Permit>(
+ props: {
+ projectId: string;
+ permits: T[];
+ },
+ context: AppContext
+): Promise<T | undefined> => {
+ const permitToCheck = props.permits;
+
+ const permits = await Promise.allSettled(
+ permitToCheck.map((permit) =>
+ hasProjectPermit({ projectId: props.projectId, permit }, context)
+ )
+ );
+
+ for (const permit of permits) {
+ if (permit.status === "rejected") {
+ throw new Error(`Authorization call failed ${permit.reason}`);
+ }
+
+ if (permit.value === true) {
+ return permitToCheck[permits.indexOf(permit)];
+ }
+ }
+};
diff --git a/packages/trpc-interface/src/shared/authorization-router.ts b/packages/trpc-interface/src/shared/authorization-router.ts
index 57af416f05..8646398f1a 100644
--- a/packages/trpc-interface/src/shared/authorization-router.ts
+++ b/packages/trpc-interface/src/shared/authorization-router.ts
@@ -4,6 +4,7 @@ import { router, procedure } from "./trpc";
import { prisma } from "@webstudio-is/prisma-client";
const Relation = z.enum(["viewers", "editors", "builders", "owners"]);
+const Permit = z.enum(["view", "edit", "build", "own"]);
const DeleteCreateInput = z.discriminatedUnion("namespace", [
z.object({
@@ -122,7 +123,7 @@ export const authorizationRouter = router({
namespace: z.enum(["Project"]),
id: z.string(),
- permit: z.enum(["view", "edit", "build", "own"]),
+ permit: Permit,
subjectSet: z.object({
namespace: z.enum(["User", "Token"]),