Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
or "Open in admin": the admin contributes those through `bar.actions`. `commit()` and
`patchFields()` stay on the manager object but are not part of core's `InlineEditManager`.
`onPageUpdate` carries the editor's name as `actor`.
The author limit (an `author` writes only the pages it created) follows `EditorIdentity.relation`,
which core derives from `system.role_grants`, not role names.

- **The bar declares its own extension point, and reads what it needs per request (#127).**
`bar.actions` is declared here, as the typed key `BAR_ACTIONS`, with its types `BarAction` and
Expand Down
4 changes: 3 additions & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export function createRequestHandler(opts: RequestHandlerOptions) {
error: "TSX format requires admin role. TSX pages execute server-side code and must be edited by trusted authors.",
}, 403);
}
if (!editor.roles.includes("admin") && !editor.roles.includes("editor")) {
// `relation` is core's decision from system.role_grants: admin and editor edit any page, an
// author only the pages it created.
if (editor.relation === "author") {
const frontmatter = await ctx.pageFrontmatter(sourcePath);
if (!frontmatter) return json({ error: "Page not found" }, 404);
if (frontmatter[PAGE_OWNER_KEY] !== editor.id) return json({ error: "Forbidden" }, 403);
Expand Down
21 changes: 18 additions & 3 deletions tests/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function setup(opts: { allowTsxFormat?: string[]; frontmatter?: Record<string, u
const run = (
method: string,
path: string,
editor: { id?: string; roles: string[] } = { roles: ["editor"] },
editor: { id?: string; roles: string[]; relation?: "admin" | "editor" | "author" } = { roles: ["editor"] },
body?: unknown,
) =>
handle(
Expand All @@ -36,7 +36,14 @@ function setup(opts: { allowTsxFormat?: string[]; frontmatter?: Record<string, u
body: body === undefined ? undefined : JSON.stringify(body),
}),
{
editor: { id: editor.id ?? "u1", username: "alice", roles: editor.roles, source: "site" },
editor: {
id: editor.id ?? "u1",
username: "alice",
roles: editor.roles,
// Core decides this from the site's role_grants; by default a role name is its own relation.
relation: editor.relation ?? (editor.roles.includes("admin") ? "admin" : editor.roles.includes("editor") ? "editor" : "author"),
source: "site",
},
path,
pageFrontmatter: () => Promise.resolve(frontmatter),
},
Expand Down Expand Up @@ -134,10 +141,18 @@ Deno.test("a failing commit is a 500 that does not leak the error", async () =>
patchFields: () => Promise.resolve(),
});
const res = await handle(new Request("http://localhost/x", { method: "POST" }), {
editor: { id: "u1", username: "alice", roles: ["admin"], source: "site" },
editor: { id: "u1", username: "alice", roles: ["admin"], relation: "admin", source: "site" },
path: `content/${PAGE}/commit`,
pageFrontmatter: () => Promise.resolve({}),
});
assertEquals(res.status, 500);
assertEquals(await res.json(), { error: "Internal error" });
});

Deno.test("ownership follows the relation core decided, not the role name: a mapped group is an editor", async () => {
const theirs = setup({ frontmatter: { createdBy: "someone-else" } });
// role_grants: { staff: editor } makes "staff" an editor relation, so it may write any page...
assertEquals((await theirs.run("POST", `content/${PAGE}/commit`, { roles: ["staff"], relation: "editor" })).status, 200);
// ...and an "editor" role the site mapped down to author is limited to its own pages.
assertEquals((await theirs.run("POST", `content/${PAGE}/commit`, { roles: ["editor"], relation: "author" })).status, 403);
});
Loading