Skip to content

feat(server): reader / shopper / editor / admin roles for signed-in users - #563

Merged
LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:feat/server-roles
Sep 28, 2026
Merged

LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:feat/server-roles

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #559. Follow-up to #534.

With sign-in on, a signed-in user could change everything. This adds a role to each user in users.toml. Each role can do everything the ones before it can:

Role Read Shopping list & pantry Recipes & menus (create, edit, delete, title picture), editor's language server Sync controls
guest (not signed in) ✅
reader ✅
shopper ✅ ✅
editor ✅ ✅ ✅
admin ✅ ✅ ✅ ✅

Decisions on the issue's open questions

  • The editor's language server is for editors, not only admins. The issue's table kept /api/ws/lsp to admins, which left editors editing without completions or diagnostics. The server doesn't need that restriction. The bridge rewrites initialize to the served directory and drops workspace/didChangeWorkspaceFolders. cooklang-language-server 0.2.4 only falls back to the document URI's folder when it has no root, and the bridge always gives it one. All it reads is the names of .cook/.menu files under that root and config/aisle.conf: nothing an editor can't already read in the web UI. It writes nothing and starts no processes. What an editor can still do is start cook lsp processes, and --max-lsp-sessions already caps those.
  • A shopper role. The issue's second use case ("ticks off the shopping list and updates the pantry, but doesn't edit recipes") didn't fit the three-role table, so the roles are ordered reader < shopper < editor < admin.
  • user add defaults to admin. That is what a plain hash has always meant, so scripts that run cook server user add behave as before.
  • An admin is written as a bare hash. Older servers can still read the file. Any other role is written as { hash = "…", role = "…" }, which an older server refuses instead of making that user an admin.
  • Parsed by hand, not with an untagged enum. Each entry is read as a toml::Value and checked separately, so a mistake names the user: "bob" has an unknown role "chef": use reader, shopper, editor, admin. Unknown keys (rol = …) are still refused, and the server fails closed at startup.

Users file

[users]
alice = "$argon2id$…"                            # admin, as today
bob   = { hash = "$argon2id$…", role = "editor" }

[users.bob] sub-tables work too.

CLI

cook server user add bob --role editor   # default: admin
cook server user role bob reader         # keeps the password, doesn't sign them out
cook server user list                    # alice  admin / bob    reader

The role argument uses clap's possible values, so --help and the shell completions list the four roles. passwd keeps the role and role keeps the password. Both keep the entry's form (bare string, inline table or sub-table) and the comments around it.

Server

  • Viewer carries the role. can_edit() is replaced by can(Capability), plus can_edit_lists() / can_edit_recipes() / can_admin() for the templates.
  • auth::middleware::required_capability maps each request to a Capability:
    • Writes are still denied by default. A write not listed in WRITES needs Administer, so a new route is kept to admins until someone lists it.
    • The protected reads now carry capabilities too: /new, /edit, /api/ws/lsp → recipes; /api/sync → admin.
  • Auth::viewer reads the role from the live users file on every request, so cook server user role applies to the next request without a restart or a new sign-in.
  • A guest still gets 401 or the sign-in redirect. A signed-in user whose role falls short gets 403: JSON for the API, and a translated error page for /new and /edit (redirecting to sign-in there would loop, because the login page sends signed-in users straight back).
    • That page's title and its "Back to recipes" link had no German, Spanish, Basque, French or Dutch translation (missing since dec59f1 added the error page), so the page was half in English. Both are now translated. fix(i18n): translate the messages that only existed in English #567 adds the same lines, plus a test that every locale has every message; whichever merges second gets rebased.
  • Templates:
    • The Edit and "add to shopping list" buttons are gated separately.
    • The reference checkboxes follow the list capability.
    • The sync section in Preferences is admin-only.
  • window.__CAN_EDIT__ is renamed __CAN_EDIT_LISTS__, since the shopping list page is the only thing that reads it.

Tests

  • Unit:
    • both entry forms and sub-tables; unknown role, key and value type; wrong-case role;
    • document edits that keep form, comments, password and role;
    • the capability table for every write route, plus look-alike paths (/api/pantryx, /api/recipes-bulk);
    • the role ordering in Viewer.
  • End-to-end (tests/server_auth_test.rs):
    • each_role_can_do_only_what_it_allows: each role against list and pantry writes, recipe saves (file checked on disk), /edit and /new, /api/ws/lsp, /api/sync/status and /api/sync/logout, and which controls the recipe, home, editor and preferences pages render;
    • a_changed_role_applies_without_signing_in_again;
    • a_refused_page_is_translated_throughout: the 403 page in French, title and link included;
    • user_commands_manage_roles;
    • refuses_a_users_file_with_an_unknown_role.
  • cargo fmt --check, cargo clippy --all-targets (no warnings), and cargo test all pass. The Playwright chromium project: 185 passed, 5 skipped.

Docs

docs/server.md has a new Roles section and updated users-file and command tables. docs/api.md (regenerated from src/web/api_docs.rs) documents the new 403, and CLAUDE.md describes the capability table. The new role-forbidden string is translated into all eight locales.

Out of scope

Per-folder and per-recipe access, as the issue says. Nothing here depends on it or gets in its way: a future viewer-scoped data layer can take the same Viewer.

🤖 Generated with Claude Code

…sers

With sign-in on, a signed-in user could change everything. Each user in
users.toml now has a role, and each role can do everything the ones
before it can:

- reader: reads, like a guest
- shopper: also the shopping list and the pantry
- editor: also recipes, menus and title pictures
- admin: also cook.md sync and the editor's language server

A bare hash still means admin, so existing files keep working. Other
roles are written as `{ hash = "…", role = "editor" }`, which an older
server refuses instead of making that user an admin; an unknown role
or key stops the server from starting.

`cook server user add --role <role>` (default admin), `user role <name>
<role>` and a role column in `user list`. Edits keep the entry's form
and its comments.

The middleware now maps each request to the capability it needs; a
write route it does not list still needs the highest one. Roles are read
from the live users file on every request, so a changed role applies at
once without signing anyone out. A signed-in user whose role falls short
gets 403 (a page, not a sign-in redirect loop), and pages leave out the
controls they cannot use; an editor's editor page no longer tries to
reach the language server.

Closes cooklang#559

Co-Authored-By: Claude Opus 5.5 <[email protected]>
/api/ws/lsp now needs EditRecipes instead of Administer, and the editor
page connects to it for everyone who can open that page. The bridge pins
the workspace root to the served directory and drops workspace-folder
changes; cooklang-language-server 0.2.4 then only lists .cook/.menu file
names under that root and reads config/aisle.conf, which an editor can
already read. It writes nothing, and --max-lsp-sessions caps the
processes it starts.

Also translate the error page's title and "Back to recipes" link into
German, Spanish, Basque, French and Dutch. They were missing since the
error page was added, so the new 403 page was half in English.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dubadub

dubadub commented Sep 28, 2026

Copy link
Copy Markdown
Member

@LeMyst that looks great, thank you! I think it leaves room for future defining own roles with granular access without needing to update interface

LeMyst added a commit that referenced this pull request Sep 28, 2026
#563 (2c82f28) and #567 (347e0e2) each added `error-title` and
`error-back-home`, under the same `# Errors` block, to the common.ftl
of de-DE, es-ES, eu-ES, fr-FR and nl-NL, and merged seconds apart.
Fluent refuses a bundle that defines a message twice, so since 347e0e2
every page the server renders panics ("Overriding { kind: Message, id:
"error-title" }") and the tests using translations fail with it. The
two copies are identical; keep the first.

tests/locales_test.rs gains a check that no locale defines a message
twice, across all its files, and names each one — rather than leaving
dozens of unrelated tests to panic on a poisoned loader.

Co-authored-by: myst <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(server): reader / editor / admin roles for signed-in users

2 participants