diff --git a/app/src/components/settings/delete-account.tsx b/app/src/components/settings/delete-account.tsx index 1e8bd06e95..c7a1fa6215 100644 --- a/app/src/components/settings/delete-account.tsx +++ b/app/src/components/settings/delete-account.tsx @@ -62,14 +62,17 @@ import { import { signInAgainAtProvider } from "@/lib/account-deletion/reauthenticate"; import { signOutMutationOptions } from "@/lib/auth/mutations"; import { currentUserQueryOptions } from "@/lib/auth/queries"; +import { isOrganizationAdmin } from "@/lib/auth/roles"; import { ClientError } from "@/lib/client"; import { deploymentCapabilitiesQueryOptions } from "@/lib/deployment/queries"; import { failureSaid } from "@/lib/load-failure"; import { useCurrentOrganizationId } from "@/lib/organizations/current"; import { switchOrganizationMutationOptions } from "@/lib/organizations/mutations"; +import { ownOrganizationsQueryOptions } from "@/lib/organizations/queries"; import { queryClient } from "@/query-client"; const ORGANIZATION_SETTINGS = "/settings/organization"; +const ADMIN_COMPUTERS = "/admin/computers"; const GENERAL_SETTINGS = "/settings"; /** @@ -166,6 +169,7 @@ export function DeleteAccountSection() { /** One organization that stops the account being deleted, and the one thing to do there. */ function BlockerRow({ blocker }: { blocker: AccountDeletionBlocker }) { const [leaving, setLeaving] = useState(false); + const adminThere = useAdminOf(blocker.organizationId); const organization = { id: blocker.organizationId, name: blocker.organizationName, @@ -181,12 +185,26 @@ function BlockerRow({ blocker }: { blocker: AccountDeletionBlocker }) { {/* Under the words on a phone, as on Organization's members: a button beside them squeezes them. - A blocker with nothing to do here (a Windows computer an admin settles) has no actions at all, - so its row keeps no empty line under the words. */} + A blocker with nothing to do here (a Windows computer, for somebody who isn't an admin there) + has no actions at all, so its row keeps no empty line under the words. */} {settledInItsSettings(blocker) ? ( - + + ) : blocker.reason === "windows_computer" ? ( + adminThere ? ( + + + + ) : null ) : settledByLeaving(blocker) ? ( {switching.error ? (

diff --git a/app/src/lib/auth/admin-gate.ts b/app/src/lib/auth/admin-gate.ts new file mode 100644 index 0000000000..d5b06bfef6 --- /dev/null +++ b/app/src/lib/auth/admin-gate.ts @@ -0,0 +1,24 @@ +import type { QueryClient } from "@tanstack/react-query"; +import { redirect } from "@tanstack/react-router"; +import { deploymentCapabilitiesQueryOptions } from "../deployment/queries"; +import { currentUserQueryOptions } from "./queries"; +import { isOrganizationAdmin } from "./roles"; + +/** + * Admin's gate: Admin is for an admin of the organization the person is working in, in every + * edition, and anybody else is sent home. In the personal edition the one person is the admin: + * single-user mode signs them in as one (`DEV_ACTOR` on the server). + * + * The edition is read first, though nothing here depends on it: a page that draws something else + * in the personal edition (`components/edition/for-a-team.tsx`) then knows which on its first + * frame. One that could not be read is the team edition, as everywhere else. + */ +export async function adminGate(queryClient: QueryClient): Promise { + await queryClient + .ensureQueryData(deploymentCapabilitiesQueryOptions()) + .catch(() => undefined); + const user = await queryClient.ensureQueryData(currentUserQueryOptions()); + if (!isOrganizationAdmin(user?.role)) { + throw redirect({ to: "/" }); + } +} diff --git a/app/src/routes/_authed/admin/route.tsx b/app/src/routes/_authed/admin/route.tsx index 50bd72abe9..68135d72bd 100644 --- a/app/src/routes/_authed/admin/route.tsx +++ b/app/src/routes/_authed/admin/route.tsx @@ -1,34 +1,15 @@ -import { createFileRoute, Outlet, redirect } from "@tanstack/react-router"; +import { createFileRoute, Outlet } from "@tanstack/react-router"; import { AdminSidebar } from "@/components/admin/admin-sidebar"; import { ConnectionBanner } from "@/components/layout/connection-banner"; import { PlanBanner } from "@/components/layout/plan-banner"; import { SidebarShell } from "@/components/layout/sidebar-shell"; import { SidebarInset } from "@/components/ui/sidebar"; import { BotListScopeProvider } from "@/lib/agents/list-scope"; -import { isOrganizationAdmin } from "@/lib/auth/roles"; -import { deploymentCapabilitiesQueryOptions } from "@/lib/deployment/queries"; -import { currentUserQueryOptions } from "../../../lib/auth/queries"; +import { adminGate } from "@/lib/auth/admin-gate"; export const Route = createFileRoute("/_authed/admin")({ - /* - * Admin is for an admin, in every edition. In the personal edition the one person is the admin: - * single-user mode signs them in as one (`DEV_ACTOR` on the server). - * - * The edition is read first, though nothing here depends on it: a page that draws something else - * in the personal edition (`components/edition/for-a-team.tsx`) then knows which on its first - * frame. One that could not be read is the team edition, as everywhere else. - */ - beforeLoad: async ({ context }) => { - await context.queryClient - .ensureQueryData(deploymentCapabilitiesQueryOptions()) - .catch(() => undefined); - const user = await context.queryClient.ensureQueryData( - currentUserQueryOptions(), - ); - if (!isOrganizationAdmin(user?.role)) { - throw redirect({ to: "/" }); - } - }, + // Admin is for an admin of the organization the person works in (lib/auth/admin-gate.ts). + beforeLoad: ({ context }) => adminGate(context.queryClient), component: RouteComponent, }); diff --git a/app/tests/delete-account.test.tsx b/app/tests/delete-account.test.tsx index d801c9f18f..b212bb9f60 100644 --- a/app/tests/delete-account.test.tsx +++ b/app/tests/delete-account.test.tsx @@ -46,6 +46,7 @@ import { RequestAccountDeletionHeader, RequestAccountDeletionResponse, } from "@/lib/api/generated/people/people.zod"; +import { adminGate } from "@/lib/auth/admin-gate"; import { signedInUser } from "@/lib/auth/queries"; import { ReportedErrorComponent } from "@/lib/error-reporting"; import { @@ -126,6 +127,8 @@ let server: { | null; /** The operation isn't served: an OpenBot older than it. */ older: boolean; + /** Dana's role in each organization, where it isn't owner. */ + roles: Record; /** Which product the deployment says it is; the personal edition doesn't serve the operation. */ edition: "team" | "personal"; /** The sign-in providers the deployment offers, and the ones the person signed in through. */ @@ -308,7 +311,7 @@ async function answer( return checked(ListOrganizationsResponse, { organizations: ORGANIZATIONS.map((one) => ({ ...one, - role: "owner", + role: server.roles[one.id] ?? "owner", active: one.id === server.active, })), canCreate: true, @@ -335,7 +338,8 @@ async function answer( name: "Dana Fresh", needsName: false, image: null, - role: "owner", + // As the server answers it: the role they have in the organization asked about. + role: server.roles[organization] ?? "owner", onboarding: null, providers: server.signedInWith, lastSignedInAt: null, @@ -365,6 +369,7 @@ beforeEach(() => { deleted: false, refuse: null, older: false, + roles: {}, edition: "team", providers: [], signedInWith: [], @@ -432,6 +437,13 @@ async function open(start = "/settings") { at("/", () =>

Home

), at("/sign", () =>

Sign in

), at("/settings/organization", () =>

Organization settings

), + createRoute({ + getParentRoute: () => rootRoute, + path: "/admin/computers", + // Admin's own gate, which sends anybody who isn't an admin where they work home. + beforeLoad: () => adminGate(queryClient), + component: () =>

Computers

, + }), at("/settings", () => ( @@ -554,7 +566,7 @@ describe("deleting your account", () => { ); }); - test("a Windows computer OpenBot made is listed with who settles it, and offers no Leave, which wouldn't", async () => { + test("a Windows computer OpenBot made is listed with who settles it, offers no Leave, which wouldn't, and opens Computers for an admin there", async () => { server.blockers = [WINDOWS]; const app = await open(); const acme = (await app.view.findByText("Acme")).closest( @@ -569,13 +581,62 @@ describe("deleting your account", () => { expect( within(acme).queryByRole("button", { name: "Open its settings" }), ).toBeNull(); - // No empty actions area: on a phone it would wrap onto a line of its own under the words. - expect(acme.querySelector("[data-slot=item-actions]")).toBeNull(); expect( app.view .getByRole("button", { name: "Delete account" }) .hasAttribute("disabled"), ).toBe(true); + // Computers is the organization's own: it is switched to first. + await app.person.click( + await within(acme).findByRole("button", { name: "Open Computers" }), + ); + await waitFor(() => + expect(app.router.state.location.pathname).toBe("/admin/computers"), + ); + expect(server.active).toBe("org_acme"); + expect(currentOrganizationId()).toBe("org_acme"); + }); + + test("Open Computers takes an admin of the computer's organization who is only a member where she works there, not home", async () => { + server.blockers = [WINDOWS]; + server.roles = { org_dana: "member", org_acme: "admin" }; + const app = await open(); + const acme = (await app.view.findByText("Acme")).closest( + "[data-slot=item]", + ) as HTMLElement; + await app.person.click( + await within(acme).findByRole("button", { name: "Open Computers" }), + ); + // Admin's gate reads her as she is in Acme, after the switch, never as a member where she was. + await waitFor(() => expect(currentOrganizationId()).toBe("org_acme")); + await waitFor(() => + expect(app.router.state.location.pathname).toBe("/admin/computers"), + ); + expect(await app.view.findByText("Computers")).toBeTruthy(); + }); + + test("a Windows computer where the person isn't an admin offers nothing to open, which would send them home", async () => { + // Beside it, one where she is an admin, so the list of her organizations has been read. + server.blockers = [ + WINDOWS, + { ...WINDOWS, organizationId: "org_globex", organizationName: "Globex" }, + ]; + server.roles = { org_acme: "member", org_globex: "admin" }; + const app = await open(); + const row = async (name: string) => + (await app.view.findByText(name)).closest( + "[data-slot=item]", + ) as HTMLElement; + const acme = await row("Acme"); + await within(await row("Globex")).findByRole("button", { + name: "Open Computers", + }); + expect(acme.textContent).toContain( + "An admin removes or forgets it under Computers first.", + ); + expect(within(acme).queryByRole("button")).toBeNull(); + // No empty actions area: on a phone it would wrap onto a line of its own under the words. + expect(acme.querySelector("[data-slot=item-actions]")).toBeNull(); }); test("the dialog's Delete account waits for both the email address and the phrase, GitHub style", async () => {