Skip to content

feat(admin): manage users from the Administration page and log security events - #68

Merged
Jeanball merged 8 commits into
Jeanball:developfrom
npozs77:feat/admin-add-edit-users
Nov 25, 2026
Merged

Jeanball merged 8 commits into
Jeanball:developfrom
npozs77:feat/admin-add-edit-users

Conversation

@npozs77

@npozs77 npozs77 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The Administration page can toggle a role, reset a password and delete a user, but there is no way to add a user, or to change a username or email, short of signing up or editing MongoDB by hand. POST /api/users/admin/create exists but nothing calls it, and the admin routes behind it trust their input. This adds the missing UI and tightens the routes it relies on. Opened as a pull request straight away rather than a discussion issue; happy to rework or drop any part.

Changes

  • fix(users): validate and protect the admin user routes

    • Create took isAdmin: true for everyone, had no validation or error handling, and only looked for the email. It now takes isAdmin from the body (default false), trims the fields, requires a valid email (with a top-level domain, so user@host is refused; localhost is the one exception, for self-hosted setups) and the same 6-character password minimum as the profile route, and refuses a username or email another user has. It answers 201 with the new user.
    • Update accepted any username or email (a duplicate was a generic 500, and login accepts either as the identifier, so a shared username is ambiguous) and a password of any length. It now validates the same way, looking only at the other users, and refuses to demote the last administrator.
    • Delete refuses to delete your own account. (The last administrator is covered by that: only an administrator can call the route, so the caller always remains.)
    • Read (GET /users/:id) returned the whole document, including the password hash and the encrypted Discogs token. It now leaves both out.
    • A user that does not exist, or an id that is not an id, is a 404 instead of a 500.
  • feat(admin): add and edit users from the Administration page: an "Add user" button opens a dialog (username, email, password, administrator), and a pencil on each row opens the same dialog for that user. Editing sends only what changed, a blank password keeps the current one, and the role is fixed on your own account (the page already blocked that). The server's message is shown when it refuses. 8 new strings in all 7 locales.

  • feat(audit): log logins, admin user actions and refused admin requests: security events as one structured line each, so an instance that is reachable from the internet can see who tried what. A small audit() helper writes audit <action> <outcome> key="value" ... ip=... (grep, fail2ban or a log shipper can pick it out) and the same fields under audit in the JSON. It records: login (success, unknown user, wrong password), signup (success, closed, email taken), throttled attempts (the rate limiter used to answer 429 silently), user create, edit (the names of the changed fields, never their values; a role change is shown as role=user->admin or role=admin->user, since a role is not a secret and who was promoted matters) and delete, a non-administrator calling an admin route, and a manual task run. Never recorded: passwords, tokens, or what was typed as the identifier (people sometimes type a password there); a failed login says only whether the user exists, and names the real account when it does. Names are quoted so a line break cannot forge a second line. The address is req.ip: right behind the bundled Nginx (production trusts one proxy hop), and in development behind a proxy it needs TRUST_PROXY=true. Nothing is stored or shown in the UI. If you would like an Audit view in the Administration page, this is the one place to also write to the database, and I am happy to build it.

Not changed: deleting a user does not remove their collection items (that is how it behaved before and is a separate question), and the existing reset-password prompt and role toggle stay as they are.

How it was verified

  • cd backend && npm run build && npm run test:run: pass, 259 tests. 48 new tests for the four user handlers (users.controller.test.ts), most of which fail against the previous controller, and 23 for the audit events (the helper, login and signup, the admin guard, a task run).
  • cd frontend && npm run lint && npm run test:run && npm run build: 0 lint errors, 179 tests pass. 17 new tests: the dialog (create, administrator, required fields, short password, server message, edit sending only changes, own role locked, cancel) and the page wiring (Add user opens an empty form and reloads the list after saving, the edit button opens the user, own role disabled).
  • Against a real backend on a spare port with an empty database and a seeded admin, with curl: create with the default role is a regular user; a duplicate username, a duplicate email in another case, a 3-character password and a malformed email each answer 400 with the message; GET /users/:id has no password or discogs.tokenEncrypted; editing a username works, and an email that is root's answers 400; a sole administrator demoting themselves answers 400 and deleting your own account answers 400; with a second administrator the demotion works, and the second administrator can delete the first.

Audit lines, on a throwaway backend with an empty database: a login, an unknown user, a wrong password, a user create, an edit, a login as that user, that user calling GET /api/users, and a delete each print the expected line (the typed identifier of the failed login is not in it), and 101 login attempts end in a rate_limited line and a 429. The task-run line is covered by a unit test only. While throttled, every rejected request writes a line, so a flood fills the log at the rate of the flood.

Tried by hand in the browser on a local dev server: adding and editing a user work. Not checked: phone width. The tests use the translation keys, not the translated text.

Checklist

  • Targets develop (not main)
  • Title follows Conventional Commits with a scope, e.g. fix(discogs): …
  • Line added to CHANGELOG.md under ## [Unreleased]
  • Frontend: npm run lint, npm run test:run and npm run build pass
  • Backend: npm run build passes
  • New UI strings added to all locales
  • Data changes come with a migration in backend/src/scripts/migration-runner.ts (none needed)
  • Screenshots attached for UI changes

POST /users/admin/create always made an administrator, had no validation or
error handling, and only looked for the email. PUT /users/:id accepted any
username or email (a duplicate was a generic 500, and login takes either as the
identifier) and a password of any length, and could demote the last
administrator. DELETE /users/:id let an administrator delete their own
account, and GET /users/:id returned the whole document, password hash and
encrypted Discogs token included.

Create takes isAdmin from the body (default false) and answers with the new
user. Create and update trim the fields, require a valid email and at least 6
characters of password like the profile route, and refuse a username or email
another user has. The last administrator cannot be demoted, and nobody can
delete their own account (which also keeps the last administrator, since only an
administrator can call the route). A user that does not exist, or an id that is
not an id, is a 404 instead of a 500.
The page could toggle the role, reset a password and delete, but there was no
way to add a user, or to change a username or email, short of signing up or
editing MongoDB by hand. An "Add user" button opens a dialog with username,
email, password and an administrator checkbox; a pencil on each row opens the
same dialog for that user, sending only what changed (a blank password keeps
the current one) and keeping the role fixed on your own account. The server's
message is shown when it refuses (a taken username or email, the last
administrator). New strings in all 7 locales.
The check only wanted something@something, so testuser@known was accepted. The
domain now needs at least one dot with text on both sides of every dot.
Self-hosted instances can use admin@localhost. It is the one domain without a
dot that the admin email check accepts; other single-label hosts such as
user@known are still refused.
Failed logins left one line without an address, admin actions left none, and
the rate limiter answered 429 silently. A small audit() helper now writes one
structured line per security event: `audit <action> <outcome> key="value"`
for grep, fail2ban or a log shipper, with the same fields under `audit` in the
JSON.

Recorded: login (success, unknown user, wrong password), signup (success, closed,
email taken), throttled attempts, user create, update (the changed field names,
never values) and delete, a non-administrator calling an admin route, and a
manual task run. Never recorded: passwords, tokens, or what was typed as the
identifier. Names are quoted so a line break cannot forge a second line. The
address is req.ip, so behind a reverse proxy it needs TRUST_PROXY.
Production always trusts one proxy hop; only development behind a proxy needs
TRUST_PROXY=true.
@npozs77 npozs77 changed the title feat(admin): add and edit users from the Administration page feat(admin): manage users from the Administration page and log security events Nov 19, 2026
npozs77 and others added 2 commits November 19, 2026 18:10
changed=role did not tell a promotion from a demotion. An edit that changes the
role now logs role=user->admin or role=admin->user, and a new account logs its
starting role. The other changed fields are still names only.
@Jeanball
Jeanball merged commit 75f005f into Jeanball:develop Nov 25, 2026
3 of 5 checks passed
@npozs77
npozs77 deleted the feat/admin-add-edit-users branch November 25, 2026 08:29
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.

2 participants