From f6f6adc0f62151bb6a17487c1c7cb905216a9e8b Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 12:36:12 +0100 Subject: [PATCH] refactor(admin): every dashboard form is a change plus its reason Administered moves from the settings pages to the dashboard's module, and every form there with a reason takes it: ReasonForm is Administered<()>, and submit_reason hands the change and its parsed Reason on. --- .../src/web/admin/administrators.rs | 4 + apps/auth-service/src/web/admin/clients.rs | 21 ++--- apps/auth-service/src/web/admin/handles.rs | 20 +++-- apps/auth-service/src/web/admin/keys.rs | 22 ++--- apps/auth-service/src/web/admin/mod.rs | 46 +++++++--- apps/auth-service/src/web/admin/settings.rs | 90 ++++++++----------- .../src/web/admin/tests/accounts.rs | 7 +- apps/auth-service/src/web/admin/users.rs | 2 +- docs/dev/extending/admin-operation.md | 36 +++++--- 9 files changed, 129 insertions(+), 119 deletions(-) diff --git a/apps/auth-service/src/web/admin/administrators.rs b/apps/auth-service/src/web/admin/administrators.rs index 6e6b4719f..7af7f88fe 100644 --- a/apps/auth-service/src/web/admin/administrators.rs +++ b/apps/auth-service/src/web/admin/administrators.rs @@ -51,6 +51,10 @@ pub(super) async fn list(cx: &Cx) -> Result { /// What [`grant`] takes: by `GET`, the search for the account, from its query; /// by `POST`, the account to grant and why, from its form. One type, since the /// extractor reads a `GET`'s query as its form. +/// +/// Not an [`Administered`](super::Administered): flattened fields reach their +/// type as text, so `page=2`, the pager's own link, and an empty `user_id` would +/// fail to decode. Its own `reason` defaults the same way. #[derive(Clone, Deserialize)] pub(super) struct GrantForm { /// The account to grant rights, which the button posts. diff --git a/apps/auth-service/src/web/admin/clients.rs b/apps/auth-service/src/web/admin/clients.rs index 53befdf97..bf8872bf8 100644 --- a/apps/auth-service/src/web/admin/clients.rs +++ b/apps/auth-service/src/web/admin/clients.rs @@ -15,7 +15,7 @@ //! page at `/clients/new`, whatever the client's ID. use super::{ - AdminApplication as App, ReasonForm, ReasonPage, Refusal, + AdminApplication as App, Administered, ReasonForm, ReasonPage, Refusal, origins::{self, OriginQuery}, }; use crate::{ @@ -25,7 +25,6 @@ use crate::{ security::ApiError, }; use chrono::Utc; -use serde::Deserialize; use sourceorigin_common::{ ClientId, clients::{ClientKind, ClientRegistration}, @@ -232,7 +231,10 @@ pub(super) async fn edit(cx: &Cx, form: Option>) -> Result>) -> Result { +pub(super) async fn rotate( + cx: &Cx, + form: Option>>, +) -> Result { let admin = App::admin(cx).await?; // The page showing a new secret needs no confirmation: sending the browser // to confirm would lose the secret. @@ -247,7 +249,7 @@ pub(super) async fn rotate(cx: &Cx, form: Option>) -> Result>) -> Result Result { /// `/handles/reserve`: reserves a handle, saying who or what it is kept for. #[route([GET, POST] "/handles/reserve")] -pub(super) async fn reserve(cx: &Cx, form: Option>) -> Result { +pub(super) async fn reserve( + cx: &Cx, + form: Option>>, +) -> Result { let admin = App::confirmed(cx).await?; let service = &App::of(cx).service; let back = href!(check).resolve(cx); - let refused = App::submit(cx, form, back.clone(), |input: Reservation| async move { - let handle = Handle::parse(&input.handle).map_err(HandlesError::from)?; + let refused = App::submit(cx, form, back.clone(), |input| async move { + let handle = Handle::parse(&input.change.handle).map_err(HandlesError::from)?; let reason = Reason::parse(input.reason)?; service .reserve_handle(&admin.operator, handle, reason) @@ -111,7 +113,7 @@ pub(super) async fn reserve(cx: &Cx, form: Option>) -> Result< }) .await?; let (typed, reason) = refused.as_ref().map_or(("", ""), |refused| { - (refused.form.handle.as_str(), refused.form.reason.as_str()) + (refused.form.change.handle.as_str(), refused.reason()) }); let refusal = refused.as_deref(); let page = view! { cx => diff --git a/apps/auth-service/src/web/admin/keys.rs b/apps/auth-service/src/web/admin/keys.rs index 15b7c97fe..329cc1fee 100644 --- a/apps/auth-service/src/web/admin/keys.rs +++ b/apps/auth-service/src/web/admin/keys.rs @@ -1,7 +1,6 @@ //! Signing keys: listing, rotating, revoking and rewrapping them. -use super::{AdminApplication as App, ReasonForm, ReasonPage}; -use crate::Reason; +use super::{AdminApplication as App, Administered, ReasonForm, ReasonPage, Refused}; use serde::Deserialize; use sourceorigin_common::protocol::jwt::KeyId; use sourceorigin_ui::{Action, Change, Checkbox, SigningKeyRow, Target}; @@ -20,9 +19,6 @@ struct Rotation { /// Whether the new key signs at once, skipping its day of waiting. #[serde(default)] now: Checkbox, - /// Why. - #[serde(default)] - reason: String, } /// `/keys`: every signing key, oldest first, each with the change revoking it. @@ -57,20 +53,24 @@ pub(super) async fn list(cx: &Cx) -> Result { /// `/keys/rotate`: publishes a new signing key, or makes it sign at once. #[route([GET, POST] "/keys/rotate")] -pub(super) async fn rotate(cx: &Cx, form: Option>) -> Result { +pub(super) async fn rotate( + cx: &Cx, + form: Option>>, +) -> Result { let admin = App::confirmed(cx).await?; let app = App::of(cx); let back = href!(list).resolve(cx); - let refused = App::submit(cx, form, back.clone(), |input: Rotation| async move { - let reason = Reason::parse(input.reason)?; + let refused = App::submit_reason(cx, form, back.clone(), |rotation, reason| async move { app.service - .rotate_signing_key(&admin.operator, &app.keys, input.now.0, reason) + .rotate_signing_key(&admin.operator, &app.keys, rotation.now.0, reason) .await .map(drop) }) .await?; - let now = refused.as_ref().is_some_and(|refused| refused.form.now.0); - let reason = refused.as_ref().map(|refused| refused.form.reason.as_str()); + let now = refused + .as_ref() + .is_some_and(|refused| refused.form.change.now.0); + let reason = refused.as_ref().map(Refused::reason); let (back, refused) = (&back, refused.as_deref()); let page = view! { cx => sourceorigin_ui::signing_key_rotation(back: back.as_str(), refused: refused, now: now, reason: reason) diff --git a/apps/auth-service/src/web/admin/mod.rs b/apps/auth-service/src/web/admin/mod.rs index 7ee05418f..69e11f27d 100644 --- a/apps/auth-service/src/web/admin/mod.rs +++ b/apps/auth-service/src/web/admin/mod.rs @@ -732,22 +732,25 @@ impl AdminApplication { .map(Body::new) } - /// [`Self::submit`] for a change whose form posts only why: parses the - /// reason and makes the change with it. - async fn submit_reason( + /// [`Self::submit`] for a change that checks its reason first: parses the + /// reason, then makes the change with it. + async fn submit_reason( cx: &Cx, - form: Option>, + form: Option>>, done: String, call: F, - ) -> Result>> + ) -> Result>>> where + C: Clone, E: Into, - F: FnOnce(Reason) -> Fut, + F: FnOnce(C, Reason) -> Fut, Fut: Future>, { - Self::submit(cx, form, done, async |input: ReasonForm| { + Self::submit(cx, form, done, async |input: Administered| { let reason = Reason::parse(input.reason)?; - call(reason).await.map_err(Into::::into) + call(input.change, reason) + .await + .map_err(Into::::into) }) .await } @@ -767,7 +770,8 @@ impl AdminApplication { F: FnOnce(Reason) -> Fut, Fut: Future>, { - let refused = Self::submit_reason(cx, form, done, call).await?; + let refused = + Self::submit_reason(cx, form, done, async move |(), reason| call(reason).await).await?; Self::reason_page(cx, page, refused, ()).await } @@ -856,16 +860,30 @@ struct Refused { form: I, } -/// The form of a change that asks only why: its reason, recorded with it, which -/// the owner of an account it changes sees in their history. A `GET`'s query, -/// which the extractor reads as its form, has none. +/// The form of a dashboard change: the change, and why, recorded with it, which +/// the owner of an account it changes sees in their history. The reason +/// defaults to empty, so a form posted without one decodes and is refused on its +/// page, as a blank one is, and a `GET`'s query, which the extractor reads as +/// its form, needs none. +/// +/// `C`'s fields reach it flattened, as text, past the form's own parsing: each +/// must decode from a string, as strings, IDs, `Checkbox` and text enums do. +/// A number, or an `Option` that reads an empty value as `None`, fails to +/// decode, so a form with one, such as the administrator grant's, keeps its +/// own `reason` field instead. #[derive(Clone, Deserialize)] -struct ReasonForm { +struct Administered { + /// The change, as its form names it. + #[serde(flatten)] + change: C, /// Why, as typed. #[serde(default)] reason: String, } +/// The form of a change that asks only why. +type ReasonForm = Administered<()>; + /// The page of a change whose form asks only why, which /// [`AdminApplication::reason_change`] serves. struct ReasonPage<'a> { @@ -893,7 +911,7 @@ impl<'a> ReasonPage<'a> { } } -impl Refused { +impl Refused> { /// The reason as it was typed, which the page shows again. fn reason(&self) -> &str { &self.form.reason diff --git a/apps/auth-service/src/web/admin/settings.rs b/apps/auth-service/src/web/admin/settings.rs index 6b300f3a8..f846b2e44 100644 --- a/apps/auth-service/src/web/admin/settings.rs +++ b/apps/auth-service/src/web/admin/settings.rs @@ -9,7 +9,7 @@ //! the reason with each change, which the owner sees in their history. Nothing //! here adds a way in or an address: those are the owner's alone. -use super::{AdminApplication as App, Refusal, Refused, users::Account}; +use super::{AdminApplication as App, Administered, Refusal, Refused, users::Account}; use crate::{ Reason, account::{Subject, Viewer}, @@ -42,25 +42,11 @@ use topcoat::{ view::{Child, View, view}, }; -/// A settings change an administrator posts: the owner's change, and why. -#[derive(Clone, Deserialize)] -struct Administered { - /// The change, as the owner's form names it. - #[serde(flatten)] - change: C, - /// Why, recorded with the change and shown to the owner in their history. - #[serde(default)] - reason: String, -} - /// The form giving an account a new handle. #[derive(Clone, Deserialize)] struct NewHandle { /// The handle. handle: String, - /// Why, recorded with the change and shown to the owner in their history. - #[serde(default)] - reason: String, /// Whether a reserved handle, or one that looks like another, is allowed. #[serde(default)] allow_reserved: Checkbox, @@ -71,9 +57,6 @@ struct NewHandle { struct NewDisplayName { /// The name. display_name: String, - /// Why, recorded with the change and shown to the owner in their history. - #[serde(default)] - reason: String, } /// The query of [`history`]: the entry the page starts after, if not the newest. @@ -89,10 +72,10 @@ struct HistoryQuery { pub(super) async fn sign_in(cx: &Cx, body: Body) -> Result { let (admin, user) = App::settings(cx).await?; let form = App::posted::>(cx, body).await?; - let refused = App::submit(cx, form, App::here(cx), |posted| async move { - let subject = App::subject(user, admin, posted.reason)?; + let refused = App::submit_reason(cx, form, App::here(cx), |change, reason| async move { + let subject = App::subject(user, admin, reason); let account = &App::of(cx).account; - match posted.change { + match change { SignInChange::Rename { id, friendly_name } => { Ok(account.rename_passkey(&subject, id, friendly_name).await?) } @@ -149,9 +132,9 @@ pub(super) async fn sign_in(cx: &Cx, body: Body) -> Result { pub(super) async fn profile(cx: &Cx, body: Body) -> Result { let (admin, user) = App::settings(cx).await?; let form = App::posted::>(cx, body).await?; - let refused = App::submit(cx, form, App::here(cx), |posted| async move { - let subject = App::subject(user, admin, posted.reason)?; - match posted.change { + let refused = App::submit_reason(cx, form, App::here(cx), |change, reason| async move { + let subject = App::subject(user, admin, reason); + match change { ProfileChange::Remove => Ok(App::of(cx).account.remove_picture(&subject).await?), // An administrator changes the name and the handle on their own pages, // which take no cooldown. @@ -168,9 +151,7 @@ pub(super) async fn profile(cx: &Cx, body: Body) -> Result { // Stored in any status; shown only while the account is active. let stored = account.picture(&viewer).await?.is_some(); let picture = App::picture(cx, profile.picture); - let reason = refused - .as_ref() - .map_or("", |refused| refused.form.reason.as_str()); + let reason = refused.as_ref().map_or("", Refused::reason); let page = &App::here(cx); let who = Who { name: &profile.display_name, @@ -212,10 +193,10 @@ pub(super) async fn profile(cx: &Cx, body: Body) -> Result { pub(super) async fn emails(cx: &Cx, body: Body) -> Result { let (admin, user) = App::settings(cx).await?; let form = App::posted::>(cx, body).await?; - let refused = App::submit(cx, form, App::here(cx), |posted| async move { - let subject = App::subject(user, admin, posted.reason)?; + let refused = App::submit_reason(cx, form, App::here(cx), |change, reason| async move { + let subject = App::subject(user, admin, reason); let account = &App::of(cx).account; - match posted.change { + match change { EmailChange::Primary { id } => Ok(account.make_primary_email(&subject, id).await?), EmailChange::Remove { id } => Ok(account.remove_email(&subject, id).await?), EmailChange::Add { .. } => Err(App::owners()), @@ -247,9 +228,9 @@ pub(super) async fn emails(cx: &Cx, body: Body) -> Result { pub(super) async fn apps(cx: &Cx, body: Body) -> Result { let (admin, user) = App::settings(cx).await?; let form = App::posted::>(cx, body).await?; - let refused = App::submit(cx, form, App::here(cx), |posted| async move { - let subject = App::subject(user, admin, posted.reason)?; - let ConnectedAppChange::Remove { client_id } = posted.change; + let refused = App::submit_reason(cx, form, App::here(cx), |change, reason| async move { + let subject = App::subject(user, admin, reason); + let ConnectedAppChange::Remove { client_id } = change; let account = &App::of(cx).account; account .remove_app(&subject, &client_id) @@ -369,11 +350,14 @@ pub(super) async fn notifications(cx: &Cx) -> Result { /// one only when the form allows it. It leads back to the account's Profile tab, /// which shows the handle. #[route([GET, POST] "/users/{account}/handle")] -pub(super) async fn handle(cx: &Cx, form: Option>) -> Result { +pub(super) async fn handle( + cx: &Cx, + form: Option>>, +) -> Result { let (admin, user) = App::settings(cx).await?; let back = href!(profile, Account(user)).resolve(cx); - let refused = App::submit(cx, form, back.clone(), |input: NewHandle| async move { - let handle = Handle::parse(&input.handle).map_err(HandlesError::from)?; + let refused = App::submit(cx, form, back.clone(), |input| async move { + let handle = Handle::parse(&input.change.handle).map_err(HandlesError::from)?; let reason = Reason::parse(input.reason)?; App::of(cx) .service @@ -382,16 +366,16 @@ pub(super) async fn handle(cx: &Cx, form: Option>) -> Result>) -> Result>) -> Result { +pub(super) async fn display_name( + cx: &Cx, + form: Option>>, +) -> Result { let (admin, user) = App::settings(cx).await?; let back = href!(profile, Account(user)).resolve(cx); - let refused = App::submit(cx, form, back.clone(), |input: NewDisplayName| async move { - let subject = App::subject(user, admin, input.reason)?; + let refused = App::submit_reason(cx, form, back.clone(), |new, reason| async move { + let subject = App::subject(user, admin, reason); App::of(cx) .account - .change_display_name(&subject, input.display_name) + .change_display_name(&subject, new.display_name) .await .map_err(Refusal::from) }) .await?; let (typed, reason) = refused.as_ref().map_or(("", ""), |refused| { - ( - refused.form.display_name.as_str(), - refused.form.reason.as_str(), - ) + (refused.form.change.display_name.as_str(), refused.reason()) }); let refused = refused.as_deref(); let target = App::target(cx).await; @@ -485,10 +469,7 @@ impl App { refused: Option<&'a Refused>>, ) -> (P::Row, &'a str) { match refused { - Some(refused) => ( - P::reopened(&refused.form.change), - refused.form.reason.as_str(), - ), + Some(refused) => (P::reopened(&refused.form.change), refused.reason()), None => (parse_query_params(cx).unwrap_or_default(), ""), } } @@ -499,9 +480,8 @@ impl App { } /// `admin` changing `user`'s settings, for `reason`. - fn subject(user: UserId, admin: &SignedInAdmin, reason: String) -> Result { - let reason = Reason::parse(reason)?; - Ok(Subject::operator(user, admin.operator.clone(), reason)) + fn subject(user: UserId, admin: &SignedInAdmin, reason: Reason) -> Subject { + Subject::operator(user, admin.operator.clone(), reason) } /// Why a change only the owner makes, such as adding a way in, is refused. diff --git a/apps/auth-service/src/web/admin/tests/accounts.rs b/apps/auth-service/src/web/admin/tests/accounts.rs index 19e0e84d0..95adfbbf1 100644 --- a/apps/auth-service/src/web/admin/tests/accounts.rs +++ b/apps/auth-service/src/web/admin/tests/accounts.rs @@ -410,8 +410,8 @@ async fn administrators_change_an_accounts_settings_with_a_reason() { } /// Granting rights starts from a search: the page lists the accounts matching a -/// handle, with a button for each that may be granted, and the button's form -/// makes the account an administrator. +/// handle, a page at a time, with a button for each that may be granted, and the +/// button's form makes the account an administrator. #[tokio::test] #[ignore = "requires TEST_DATABASE_URL"] async fn rights_are_granted_from_a_search() { @@ -433,6 +433,9 @@ async fn rights_are_granted_from_a_search() { page.contains(&format!(r#"name="user_id" value="{}""#, other.user_id)), "{page}" ); + // The pager's links name a page, which the search reads as a number. + let paged = harness.visit(&dashboard, &format!("{search}&page=2")).await; + assert_eq!(paged.status(), StatusCode::OK, "{search}&page=2"); let granted = dashboard .submit( &harness.dashboard, diff --git a/apps/auth-service/src/web/admin/users.rs b/apps/auth-service/src/web/admin/users.rs index b3f17648e..fe10d2a2d 100644 --- a/apps/auth-service/src/web/admin/users.rs +++ b/apps/auth-service/src/web/admin/users.rs @@ -380,7 +380,7 @@ impl AccountChange { let app = App::of(cx); let id = *path_param::(cx)?; let back = App::account(cx, id); - let refused = App::submit_reason(cx, form, back.clone(), async |reason| { + let refused = App::submit_reason(cx, form, back.clone(), async |(), reason| { self.make(app, admin, id, reason).await }) .await?; diff --git a/docs/dev/extending/admin-operation.md b/docs/dev/extending/admin-operation.md index 5c11452af..21795e6e1 100644 --- a/docs/dev/extending/admin-operation.md +++ b/docs/dev/extending/admin-operation.md @@ -155,12 +155,14 @@ as `handles.rs`. A change's page shows its form on `GET` and makes the change on ```rust /// `/handles/reserve`: reserves a handle, saying who or what it is kept for. #[route([GET, POST] "/handles/reserve")] -pub(super) async fn reserve(cx: &Cx, form: Option>) -> Result { - let admin = App::admin(cx).await?; - App::confirm(cx, admin)?; +pub(super) async fn reserve( + cx: &Cx, + form: Option>>, +) -> Result { + let admin = App::confirmed(cx).await?; let service = &App::of(cx).service; let back = href!(check).resolve(cx); - let refused = App::submit(cx, form, back.clone(), |input: Reservation| async move { + let refused = App::submit(cx, form, back.clone(), |input| async move { // Parse the form, then call the service as `admin.operator`. }) .await?; @@ -168,15 +170,25 @@ pub(super) async fn reserve(cx: &Cx, form: Option>) -> Result< } ``` -- `App::admin` finds the administrator, or sends the browser to sign in. -- `App::confirm` requires the dashboard's own recent passkey confirmation, or - sends the browser to confirm and back. +- `App::confirmed` finds the administrator and requires the dashboard's own + recent passkey confirmation, or sends the browser to sign in or confirm, and + back. +- Most change forms are an `Administered`: the form's own fields, `C`, and + the `reason` every change posts. The reason defaults to empty, so a form + posted without one decodes, and `Reason::parse` refuses it on the page as it + refuses a blank one. `C`'s fields are flattened, so each must decode from + text: strings, IDs, `Checkbox` and text enums do, but numbers, and `Option`s + that read an empty value as `None`, don't. A form with one declares its own + `#[serde(default)] reason` instead, as the administrator grant's + `GrantForm` does. So do `CreateInvitation` and `ApproveOrigin` + (`sourceorigin_common::admin`) and `OperatorDraft` + (`crates/ui/src/admin/clients.rs`), and the alert update's `UpdateForm` + posts its reason as `note`. - `App::submit` makes the change, goes back to `back` once it is made, and - otherwise returns the refusal, which the form shows with what was typed. A - change whose form posts only its reason uses `App::submit_reason` and - `ReasonForm`, which parse the reason first. Every form's `reason` field is - `#[serde(default)]`, so a form posted without one decodes, and `Reason::parse` - refuses it on the page as it refuses a blank one. + otherwise returns the refusal, which the form shows with what was typed. + `App::submit_reason` parses the reason first, then hands the change and its + `Reason` on. A change whose form posts only its reason takes a `ReasonForm`, + an `Administered<()>`, and `App::reason_change` serves its whole page. The page's words are a `Change` variant in `crates/ui/src/admin/mod.rs`: its title, description, button, label, whether it takes something away, and the