diff --git a/.changeset/console-invite-role-and-permissions.md b/.changeset/console-invite-role-and-permissions.md new file mode 100644 index 0000000000..fecd120ac1 --- /dev/null +++ b/.changeset/console-invite-role-and-permissions.md @@ -0,0 +1,5 @@ +--- +"@zitadel/server": patch +--- + +In the console's **Invite member** dialog no role is preselected any more, and **Create invitation** stays disabled until you pick one, so a link no longer grants Admin by default. People who are not admins of a project no longer see **Invite member**, and the Invitations tab tells them only admins can see invitations instead of showing an error. diff --git a/apps/console/src/features/invitations/api.ts b/apps/console/src/features/invitations/api.ts index 52d1d21194..7d3781006f 100644 --- a/apps/console/src/features/invitations/api.ts +++ b/apps/console/src/features/invitations/api.ts @@ -1,3 +1,5 @@ +import { apiErrorCode } from "@zitadel/api/runtime/fetch"; + import { trackEvent } from "@/addon/analytics"; import { api } from "@/api/zitadel"; import { describeError } from "@/lib/api-error"; @@ -14,11 +16,40 @@ import { } from "./model"; import { clearInvitationToken, holdInvitationTokenFor } from "./token"; -/** The first page of a project's invitations, or why it could not be read. */ +/** + * The first page of a project's invitations, or why it could not be read. + * `denied` is the server's answer that the viewer may not manage invitations, + * which only the project's admins may: the page then offers no inviting. + */ export type InvitationsRead = | { invitations: Invitation[]; nextPageToken?: string } + | { denied: true } | { error: string }; +/** + * The server's refusals of the list for anyone who is not an admin of the + * project: `inv.permission_denied` for a role that is too weak, and + * `inv.not_found` where the caller's access does not reach the project's + * invitations at all (a narrower grant). A list names no invitation, so its + * `not_found` can mean nothing else. + */ +const LIST_DENIED = new Set(["inv.permission_denied", "inv.not_found"]); + +/** + * The page's first read, with a failure as a value: the Members tab stays + * readable beside it. The effective permissions are not readable yet (console + * ADR 0004 ยง5), so the refusal of this read is what says the viewer is no + * admin. + */ +export async function readInvitationsForPage(projectId: string): Promise { + try { + return await readInvitations(projectId); + } catch (cause) { + if (LIST_DENIED.has(apiErrorCode(cause) ?? "")) return { denied: true }; + return { error: describeError(cause, "The invitations could not be read.") }; + } +} + /** * The statuses the tab lists. An accepted invitation is left out: the person * it brought in is a row of the Members tab, and the server has erased the diff --git a/apps/console/src/features/invitations/components/invite-member-dialog.tsx b/apps/console/src/features/invitations/components/invite-member-dialog.tsx index d977eba8d9..ccaf74032d 100644 --- a/apps/console/src/features/invitations/components/invite-member-dialog.tsx +++ b/apps/console/src/features/invitations/components/invite-member-dialog.tsx @@ -30,6 +30,10 @@ const NAME_MAX = 200; * can tell the rows of the invitations table apart; it is never matched * against an account, and it is optional. * + * No role is chosen for the administrator: the link is a bearer link, so a + * default would hand out that role on a click-through. Create stays disabled + * until one is picked, as the CLI requires `--relation` (#3329). + * * One dialog, two steps. Creating swaps the form for the link in place, since * that response is the only time the link exists; closing after that reloads * the table behind it, where the new row is. @@ -89,10 +93,13 @@ function InviteForm({ const nameId = `${id}-name`; const roleId = `${id}-role`; const [name, setName] = useState(""); - const [relation, setRelation] = useState("admin"); + const [relation, setRelation] = useState(undefined); // ADR 030: a refusal's own message is the human-facing string. const create = useSubmit(async () => { + // Unreachable from the form, whose only submit button is disabled until a + // role is picked; it narrows the type. + if (!relation) return; onCreated(await createProjectInvitation(projectId, { relation, name: name.trim() })); }, "The invitation could not be created."); @@ -130,7 +137,9 @@ function InviteForm({ { setRelation(value as ProjectRelation); create.clearError(); @@ -183,7 +192,7 @@ function InviteForm({ > Cancel - diff --git a/apps/console/src/features/invitations/components/project-invitations.tsx b/apps/console/src/features/invitations/components/project-invitations.tsx index 4de2b522aa..2ac35b78b0 100644 --- a/apps/console/src/features/invitations/components/project-invitations.tsx +++ b/apps/console/src/features/invitations/components/project-invitations.tsx @@ -91,7 +91,21 @@ export function ProjectInvitations({ return ( // Labelled so the section is a landmark assistive tech can jump to.
- {"error" in read ? ( + {"denied" in read ? ( + + + + + + + Invitations are for admins + + Only project admins can see and create invitations. + + + + + ) : "error" in read ? ( ) : ( ({ - error: describeError(cause, "The invitations could not be read."), - }), - ), + readInvitationsForPage(projectId), ]); return { ...access, invitations }; }, @@ -80,15 +79,17 @@ function ProjectMembersPage() { Members Invitations - { - // Closing the dialog lands on the table the new row is in. - open("invitations"); - reload(); - }} - /> + {!("denied" in invitations) && ( + { + // Closing the dialog lands on the table the new row is in. + open("invitations"); + reload(); + }} + /> + )} diff --git a/apps/console/src/routes/_authed/project/project-invitations.spec.tsx b/apps/console/src/routes/_authed/project/project-invitations.spec.tsx index dfdb15b685..7fd370b982 100644 --- a/apps/console/src/routes/_authed/project/project-invitations.spec.tsx +++ b/apps/console/src/routes/_authed/project/project-invitations.spec.tsx @@ -175,6 +175,7 @@ describe("project members tabs", () => { await userEvent.click(inviteButton()); const dialog = within(await screen.findByRole("dialog", { name: "Invite member" })); + await userEvent.click(dialog.getByRole("radio", { name: "Admin" })); await userEvent.click(dialog.getByRole("button", { name: "Create invitation" })); // The link is read first; closing lands on the table its row is in. @@ -301,6 +302,29 @@ describe("project invitations", () => { expect(screen.getByRole("heading", { name: "acme-app" })).toBeInTheDocument(); }); + it.each([ + [403, "inv.permission_denied", "insufficient permissions to manage invitations"], + // A narrower grant: the server answers as if there were nothing to list. + [404, "inv.not_found", "This invitation is no longer available."], + ])( + "offers no inviting and explains the tab to someone who is not an admin (%i %s)", + async (status, code, message) => { + stubPage(() => HttpResponse.json({ code, message }, { status })); + openMembers(); + // The members stay readable, without the button. + await screen.findByRole("region", { name: "Members" }); + expect(screen.queryByRole("button", { name: "Invite member" })).not.toBeInTheDocument(); + + await userEvent.click(screen.getByRole("tab", { name: "Invitations" })); + const section = within(await screen.findByRole("region", { name: "Invitations" })); + expect( + section.getByText("Only project admins can see and create invitations."), + ).toBeInTheDocument(); + expect(section.queryByText(message)).not.toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Invite member" })).not.toBeInTheDocument(); + }, + ); + it("loads the next page with the token it was given", async () => { const reads = stubPage(({ pageToken }) => pageToken @@ -367,20 +391,28 @@ describe("inviting a member", () => { await waitFor(() => expect(reads.length).toBeGreaterThan(before)); }); - it("defaults to admin and sends no metadata without a name", async () => { + it("preselects no role, creates with the one picked and sends no metadata without a name", async () => { stubPage(); const created = stubCreate(); await openInvitations(); await userEvent.click(inviteButton()); const dialog = within(await screen.findByRole("dialog", { name: "Invite member" })); - expect(dialog.getByRole("radio", { name: "Admin" })).toBeChecked(); - await userEvent.click(dialog.getByRole("button", { name: "Create invitation" })); + // No role is chosen for the administrator, and there is nothing to create yet. + for (const role of ["Admin", "Editor", "Viewer"]) { + expect(dialog.getByRole("radio", { name: role })).not.toBeChecked(); + } + const create = dialog.getByRole("button", { name: "Create invitation" }); + expect(create).toBeDisabled(); + + await userEvent.click(dialog.getByRole("radio", { name: "Viewer" })); + expect(create).toBeEnabled(); + await userEvent.click(create); await screen.findByRole("dialog", { name: "Invitation created" }); expect(created[0]?.body).toEqual({ type: "project_member", - project_member: { relation: "admin" }, + project_member: { relation: "viewer" }, }); }); @@ -392,6 +424,7 @@ describe("inviting a member", () => { await userEvent.click(inviteButton()); const dialog = within(await screen.findByRole("dialog", { name: "Invite member" })); await userEvent.type(dialog.getByRole("textbox", { name: /Invitee name/ }), "Jane Doe"); + await userEvent.click(dialog.getByRole("radio", { name: "Admin" })); await userEvent.click(dialog.getByRole("button", { name: "Create invitation" })); await screen.findByRole("dialog", { name: "Invitation created" }); @@ -418,6 +451,7 @@ describe("inviting a member", () => { await userEvent.click(inviteButton()); const dialog = within(await screen.findByRole("dialog", { name: "Invite member" })); + await userEvent.click(dialog.getByRole("radio", { name: "Admin" })); await userEvent.click(dialog.getByRole("button", { name: "Create invitation" })); expect( diff --git a/e2e/journey/src/claim.spec.ts b/e2e/journey/src/claim.spec.ts index 44a8af0cdf..95c9fbee39 100644 --- a/e2e/journey/src/claim.spec.ts +++ b/e2e/journey/src/claim.spec.ts @@ -110,9 +110,10 @@ test("the setup admin can invite a colleague and remove them, and the colleague await expect(page.getByRole("region", { name: "Members" })).toBeVisible(); // The admin makes a link. It is shown once, in the message to pass on, and - // names nobody: whoever opens it gets the role (admin by default). + // names nobody: whoever opens it gets the role the admin picked. await page.getByRole("button", { name: "Invite member", exact: true }).click(); const invite = page.getByRole("dialog", { name: "Invite member" }); + await invite.getByRole("radio", { name: "Admin", exact: true }).click(); await invite.getByRole("button", { name: "Create invitation", exact: true }).click(); const created = page.getByRole("dialog", { name: "Invitation created" }); const message = await created.getByRole("textbox").inputValue();