Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/console-invite-role-and-permissions.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 32 additions & 1 deletion apps/console/src/features/invitations/api.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<InvitationsRead> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -89,10 +93,13 @@ function InviteForm({
const nameId = `${id}-name`;
const roleId = `${id}-role`;
const [name, setName] = useState("");
const [relation, setRelation] = useState<ProjectRelation>("admin");
const [relation, setRelation] = useState<ProjectRelation | undefined>(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.");

Expand Down Expand Up @@ -130,7 +137,9 @@ function InviteForm({
<RadioGroup
aria-labelledby={roleId}
className="gap-2"
value={relation}
// Controlled from the start: an empty value is "none chosen".
value={relation ?? ""}
aria-required
onValueChange={(value) => {
setRelation(value as ProjectRelation);
create.clearError();
Expand Down Expand Up @@ -183,7 +192,7 @@ function InviteForm({
>
Cancel
</Button>
<Button type="submit" className="px-2.5" loading={create.pending}>
<Button type="submit" className="px-2.5" loading={create.pending} disabled={!relation}>
Create invitation
</Button>
</FormDialogFooter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,21 @@ export function ProjectInvitations({
return (
// Labelled so the section is a landmark assistive tech can jump to.
<section aria-label="Invitations" className="flex flex-col">
{"error" in read ? (
{"denied" in read ? (
<Card className="items-center gap-0 p-6">
<Empty className="w-full">
<EmptyHeader>
<EmptyMedia variant="icon">
<UserCog aria-hidden className="stroke-[1.5]" />
</EmptyMedia>
<EmptyTitle>Invitations are for admins</EmptyTitle>
<EmptyDescription>
Only project admins can see and create invitations.
</EmptyDescription>
</EmptyHeader>
</Empty>
</Card>
) : "error" in read ? (
<FormError message={read.error} />
) : (
<InvitationsTable
Expand Down
33 changes: 17 additions & 16 deletions apps/console/src/routes/_authed/project/members.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createFileRoute, getRouteApi, useRouter } from "@tanstack/react-router"
import { Users } from "lucide-react";

import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { type InvitationsRead, readInvitations } from "@/features/invitations/api";
import { readInvitationsForPage } from "@/features/invitations/api";
import {
InviteMemberButton,
ProjectInvitations,
Expand All @@ -14,7 +14,6 @@ import {
requireProjectScope,
useRequiredProjectScope,
} from "@/features/projects/project-scope";
import { describeError } from "@/lib/api-error";

/** The page's tabs; `members` is the one a bare URL opens. */
type MembersTab = "members" | "invitations";
Expand All @@ -33,6 +32,10 @@ type MembersTab = "members" | "invitations";
* The members and the grants are the same viewer check on the same project and
* succeed or fail as one. A failed invitations read is shown in its tab rather
* than thrown, so the members stay readable and the API's own message says why.
*
* Only the project's admins manage invitations. For anyone else the read is
* refused, and the page then has no Invite member and the tab says who can
* see invitations, instead of an error (#3331).
*/
export const Route = createFileRoute("/_authed/project/members")({
staticData: {
Expand All @@ -46,11 +49,7 @@ export const Route = createFileRoute("/_authed/project/members")({
const projectId = requireProjectScope(deps.project);
const [access, invitations] = await Promise.all([
readProjectMembers(projectId),
readInvitations(projectId).catch(
(cause): InvitationsRead => ({
error: describeError(cause, "The invitations could not be read."),
}),
),
readInvitationsForPage(projectId),
]);
return { ...access, invitations };
},
Expand Down Expand Up @@ -80,15 +79,17 @@ function ProjectMembersPage() {
<TabsTrigger value="members">Members</TabsTrigger>
<TabsTrigger value="invitations">Invitations</TabsTrigger>
</TabsList>
<InviteMemberButton
projectId={projectId}
projectName={project.name}
onInvited={() => {
// Closing the dialog lands on the table the new row is in.
open("invitations");
reload();
}}
/>
{!("denied" in invitations) && (
<InviteMemberButton
projectId={projectId}
projectName={project.name}
onInvited={() => {
// Closing the dialog lands on the table the new row is in.
open("invitations");
reload();
}}
/>
)}
</div>

<TabsContent value="members">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" },
});
});

Expand All @@ -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" });

Expand All @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion e2e/journey/src/claim.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading