Soft-delete and restore users #19

Open
opened 2026-10-07 21:29:40 +03:00 by ars9 · 0 comments
Owner

Summary

Deleting a user (npm run user:delete from #15, DELETE /api/v1/users/:id from #18) removes the database record. Keep the record instead and mark the user as deleted, so references to users (now and in the future) stay valid, and add a way to restore deleted users.

Requirements

Data model

  • Add status to the user entity, with values active and deleted (default active), plus a migration. Existing users become active.
  • login stays unique across all users, deleted ones included. A deleted user's login can't be reused.
  • A deleted user's role assignments are kept, but ignored while the user is deleted.

Operations

Add to the operations list from #17:

Entity Modes Covers
user restore restoring a deleted user
  • Restoring a user whose kept roles include admin also requires user:grant_admin (#18).
  • admin (*:*) gets this automatically; nobody doesn't.

Deletion

  • npm run user:delete <id> and DELETE /api/v1/users/:id set status to deleted instead of removing the record.
  • The existing rules stay:
    • the last admin can't be deleted (deleted users don't count as admins)
    • you can't delete your own account through the API
  • Deleting an already deleted user is treated as an unknown user (CLI error, API 404).

Restore

  • npm run user:restore <id> and POST /api/v1/users/:id/restore (user:restore) set status back to active.
  • The user gets their kept roles back and can log in again with their old password.
  • Tokens issued before the deletion stay rejected after a restore. Only tokens issued after the restore work. (Hint: the user's deletedAt timestamp compared with the token's iat.)
  • Restoring an active user: CLI error, API 409. Unknown id: CLI error, API 404.
  • Documented in Swagger.

Deleted users are inactive everywhere

  • Authentication:
    • a deleted user can't log in; the response is the same as for wrong credentials
    • their existing tokens (cookie or Bearer) are rejected on the next request with 401
  • RBAC (#17): deleted users don't count toward the "at least one admin" rule.
  • CLI: user:get, user:token, user:list_roles and user:set_roles treat a deleted user as unknown.
  • API:
    • GET /api/v1/users returns only active users by default.
    • GET /api/v1/users?includeDeleted=true returns deleted users too, with their status.
    • GET /api/v1/users/:id and all write endpoints (PATCH, PUT .../password, PUT .../roles, DELETE) return 404 for a deleted user. POST .../restore is the only exception.
  • Creating or renaming users: using a deleted user's login returns 409 (CLI error), with a message saying the login belongs to a deleted user who can be restored.

Client

  • The Users page gets a "Show deleted" toggle, off by default.
  • When it's on, deleted users appear in the table, visibly marked (e.g. a "Deleted" badge, muted row).
    • Deleted users have no actions except "Restore", which is shown only with user:restore, with a confirmation dialog.
  • Deleted users don't appear anywhere else in the UI.
  • New strings in en.json; Storybook stories for any new or changed client/src/ui/ components.

Docs

  • docs/architecture.md: user status, soft deletion, restore and the user:restore operation.
  • docs/development.md: user:delete marks the user as deleted; user:restore.

Note for later

When runs, versions or other records start referencing users, a deleted user should still be shown there, marked as deleted (e.g. "jdoe (deleted)"). That belongs to whichever issue adds those references.

Acceptance criteria

  • Deleting a user, via the CLI or the API, keeps the record with status = deleted.
  • A deleted user can't log in, and their existing tokens return 401.
  • Creating a user or renaming one to a deleted user's login is rejected.
  • GET /users excludes deleted users; GET /users?includeDeleted=true includes them.
  • Every other API endpoint and CLI command, except restore, treats a deleted user as unknown.
  • The last active admin can't be deleted.
  • Restoring a user, via the CLI or the API:
    • sets them back to active with their previous roles, and they can log in with their old password
    • doesn't bring back tokens issued before the deletion
  • Restore requires user:restore, plus user:grant_admin for a user who had admin. Restoring an active user returns 409.
  • The Users page shows deleted users only with "Show deleted" on, marked, with only a Restore action (if allowed).
  • Migration test (test/migrations.spec.ts) passes; server tests cover the cases above.
## Summary Deleting a user (`npm run user:delete` from #15, `DELETE /api/v1/users/:id` from #18) removes the database record. Keep the record instead and mark the user as deleted, so references to users (now and in the future) stay valid, and add a way to restore deleted users. ## Requirements ### Data model - Add `status` to the `user` entity, with values `active` and `deleted` (default `active`), plus a migration. Existing users become `active`. - `login` stays unique across **all** users, deleted ones included. A deleted user's login can't be reused. - A deleted user's role assignments are kept, but ignored while the user is deleted. ### Operations Add to the operations list from #17: | Entity | Modes | Covers | |---|---|---| | `user` | `restore` | restoring a deleted user | - Restoring a user whose kept roles include `admin` also requires `user:grant_admin` (#18). - `admin` (`*:*`) gets this automatically; `nobody` doesn't. ### Deletion - `npm run user:delete <id>` and `DELETE /api/v1/users/:id` set `status` to `deleted` instead of removing the record. - The existing rules stay: - the last admin can't be deleted (deleted users don't count as admins) - you can't delete your own account through the API - Deleting an already deleted user is treated as an unknown user (CLI error, API `404`). ### Restore - `npm run user:restore <id>` and `POST /api/v1/users/:id/restore` (`user:restore`) set `status` back to `active`. - The user gets their kept roles back and can log in again with their old password. - Tokens issued before the deletion stay rejected after a restore. Only tokens issued after the restore work. (Hint: the user's `deletedAt` timestamp compared with the token's `iat`.) - Restoring an active user: CLI error, API `409`. Unknown id: CLI error, API `404`. - Documented in Swagger. ### Deleted users are inactive everywhere - **Authentication:** - a deleted user can't log in; the response is the same as for wrong credentials - their existing tokens (cookie or Bearer) are rejected on the next request with `401` - **RBAC (#17):** deleted users don't count toward the "at least one admin" rule. - **CLI:** `user:get`, `user:token`, `user:list_roles` and `user:set_roles` treat a deleted user as unknown. - **API:** - `GET /api/v1/users` returns only active users by default. - `GET /api/v1/users?includeDeleted=true` returns deleted users too, with their `status`. - `GET /api/v1/users/:id` and all write endpoints (`PATCH`, `PUT .../password`, `PUT .../roles`, `DELETE`) return `404` for a deleted user. `POST .../restore` is the only exception. - **Creating or renaming users:** using a deleted user's login returns `409` (CLI error), with a message saying the login belongs to a deleted user who can be restored. ### Client - The Users page gets a "Show deleted" toggle, off by default. - When it's on, deleted users appear in the table, visibly marked (e.g. a "Deleted" badge, muted row). - Deleted users have no actions except "Restore", which is shown only with `user:restore`, with a confirmation dialog. - Deleted users don't appear anywhere else in the UI. - New strings in `en.json`; Storybook stories for any new or changed `client/src/ui/` components. ### Docs - `docs/architecture.md`: user status, soft deletion, restore and the `user:restore` operation. - `docs/development.md`: `user:delete` marks the user as deleted; `user:restore`. ### Note for later When runs, versions or other records start referencing users, a deleted user should still be shown there, marked as deleted (e.g. "jdoe (deleted)"). That belongs to whichever issue adds those references. ## Acceptance criteria - Deleting a user, via the CLI or the API, keeps the record with `status = deleted`. - A deleted user can't log in, and their existing tokens return `401`. - Creating a user or renaming one to a deleted user's login is rejected. - `GET /users` excludes deleted users; `GET /users?includeDeleted=true` includes them. - Every other API endpoint and CLI command, except restore, treats a deleted user as unknown. - The last active admin can't be deleted. - Restoring a user, via the CLI or the API: - sets them back to `active` with their previous roles, and they can log in with their old password - doesn't bring back tokens issued before the deletion - Restore requires `user:restore`, plus `user:grant_admin` for a user who had `admin`. Restoring an active user returns `409`. - The Users page shows deleted users only with "Show deleted" on, marked, with only a Restore action (if allowed). - Migration test (`test/migrations.spec.ts`) passes; server tests cover the cases above.
ars9 added this to the User management milestone 2026-10-07 21:29:45 +03:00
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ars9/liqa#19