#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
Reconcile policies again when the signed-in user changes (#7966)
Anthony Stirlinge610eba8db09
diff --git a/frontend/editor/src/proprietary/hooks/usePolicies.test.ts b/frontend/editor/src/proprietary/hooks/usePolicies.test.tsindex 1228532e91..65c55fd2a7 100644--- a/frontend/editor/src/proprietary/hooks/usePolicies.test.ts+++ b/frontend/editor/src/proprietary/hooks/usePolicies.test.ts@@ -5,9 +5,15 @@ import { act, renderHook, waitFor } from "@testing-library/react";// In-memory stand-in for the backend policy store, so the hook's reconcile path// is exercised without a real server. The editor only reads policies now, so the// read (`listPolicies`) is all the hook touches.-const api = vi.hoisted(() => ({ store: new Map<string, { id: string }>() }));-vi.mock("@app/services/policyApi", () => ({- listPolicies: vi.fn(async () => [...api.store.values()]),+const api = vi.hoisted(() => ({+ store: new Map<string, { id: string }>(),+ listPolicies: vi.fn(),+ userId: null as string | null,+}));+vi.mock("@app/services/policyApi", () => ({ listPolicies: api.listPolicies }));+// Defaults to signed out, which is what the context default gives the tests below anyway.+vi.mock("@app/auth/UseSession", () => ({+ useAuth: () => ({ user: api.userId ? { id: api.userId } : null }),}));import { usePolicies } from "@app/hooks/usePolicies";@@ -16,6 +22,10 @@ describe("usePolicies", () => {beforeEach(() => {localStorage.clear();api.store.clear();+ api.userId = null;+ api.listPolicies+ .mockReset()+ .mockImplementation(async () => [...api.store.values()]);});it("starts with every category unconfigured (no seed)", async () => {@@ -114,4 +124,55 @@ describe("usePolicies", () => {// A catalogue tile is never forgotten: it reseeds from the catalogue.expect(result.current.policies.security).toBeDefined();});++ // A reconcile that ran before there was a session leaves every category in its unconfigured+ // default, and the local-pass engine reads that default as "no policy" and stops running.+ it("reconciles again when a user signs in", async () => {+ const { rerender } = renderHook(() => usePolicies());+ await waitFor(() => expect(api.listPolicies).toHaveBeenCalledTimes(1));++ api.userId = "user-1";+ rerender();+ await waitFor(() => expect(api.listPolicies).toHaveBeenCalledTimes(2));+ });++ it("picks up a policy that only became visible once signed in", async () => {+ const { result, rerender } = renderHook(() => usePolicies());+ await act(async () => {});+ expect(result.current.policies.security.configured).toBe(false);++ api.store.set("be-sec", {+ id: "be-sec",+ name: "Security",+ enabled: true,+ steps: [{ operation: "/api/v1/misc/compress-pdf", parameters: {} }],+ output: { type: "inline", options: { categoryId: "security" } },+ editor: { allowed: true, runOn: "upload" },+ } as unknown as { id: string });+ api.userId = "user-1";+ rerender();++ await waitFor(() =>+ expect(result.current.policies.security.configured).toBe(true),+ );+ });++ it("does not re-reconcile while the same user stays signed in", async () => {+ api.userId = "user-1";+ const { rerender } = renderHook(() => usePolicies());+ await waitFor(() => expect(api.listPolicies).toHaveBeenCalledTimes(1));+ rerender();+ rerender();+ expect(api.listPolicies).toHaveBeenCalledTimes(1);+ });++ it("reconciles again on sign-out, so one user's policies do not linger", async () => {+ api.userId = "user-1";+ const { rerender } = renderHook(() => usePolicies());+ await waitFor(() => expect(api.listPolicies).toHaveBeenCalledTimes(1));++ api.userId = null;+ rerender();+ await waitFor(() => expect(api.listPolicies).toHaveBeenCalledTimes(2));+ });});diff --git a/frontend/editor/src/proprietary/hooks/usePolicies.ts b/frontend/editor/src/proprietary/hooks/usePolicies.tsindex 736638d34b..70e082869f 100644--- a/frontend/editor/src/proprietary/hooks/usePolicies.ts+++ b/frontend/editor/src/proprietary/hooks/usePolicies.ts@@ -1,13 +1,15 @@/*** Read-only Policies state for the editor's enforcement path. The backend- * (`/api/v1/policies`) is the source of truth: on mount we reconcile the local- * cache against the stored policies. localStorage is a fast-render cache +- * offline fallback. Managing policies (create/edit/pause/delete) lives on the- * portal Pipelines page, not here; the editor only reads them and runs them.+ * (`/api/v1/policies`) is the source of truth: we reconcile the local cache+ * against the stored policies on mount and whenever the signed-in user+ * changes. localStorage is a fast-render cache + offline fallback. Managing+ * policies (create/edit/pause/delete) lives on the portal Pipelines page, not+ * here; the editor only reads them and runs them.*/import { useState, useEffect, useRef } from "react";import { useAppConfig } from "@app/contexts/AppConfigContext";+import { useAuth } from "@app/auth/UseSession";import {loadPolicies,onPoliciesChange,@@ -30,6 +32,10 @@ const reconcileRetryDelay = (attempt: number) =>export function usePolicies() {const [policies, setPolicies] = useState<PoliciesByKey>(loadPolicies);const { refetch: refetchAppConfig } = useAppConfig();+ // Reconciling only on mount leaves the cache in its unconfigured default when the fetch ran+ // before there was a session, and every consumer reads that default as "no policy".+ const { user } = useAuth();+ const sessionKey = user?.id ?? null;useEffect(() => onPoliciesChange(() => setPolicies(loadPolicies())), []);@@ -94,7 +100,7 @@ export function usePolicies() {cancelled = true;if (timer) clearTimeout(timer);};- }, []);+ }, [sessionKey]);return { policies };}