diff --git a/apps/auth-service/src/web/admin/handles.rs b/apps/auth-service/src/web/admin/handles.rs index 044270df0..7137141f3 100644 --- a/apps/auth-service/src/web/admin/handles.rs +++ b/apps/auth-service/src/web/admin/handles.rs @@ -31,6 +31,7 @@ struct Reservation { /// The handle to reserve. handle: String, /// Who or what it is kept for. + #[serde(default)] reason: String, } diff --git a/apps/auth-service/src/web/admin/settings.rs b/apps/auth-service/src/web/admin/settings.rs index 4a3237420..6b300f3a8 100644 --- a/apps/auth-service/src/web/admin/settings.rs +++ b/apps/auth-service/src/web/admin/settings.rs @@ -49,6 +49,7 @@ struct Administered { #[serde(flatten)] change: C, /// Why, recorded with the change and shown to the owner in their history. + #[serde(default)] reason: String, } @@ -58,6 +59,7 @@ 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)] @@ -70,6 +72,7 @@ struct NewDisplayName { /// The name. display_name: String, /// Why, recorded with the change and shown to the owner in their history. + #[serde(default)] reason: String, } diff --git a/apps/auth-service/src/web/admin/tests/operations.rs b/apps/auth-service/src/web/admin/tests/operations.rs index b7c0cc039..9bc3451f7 100644 --- a/apps/auth-service/src/web/admin/tests/operations.rs +++ b/apps/auth-service/src/web/admin/tests/operations.rs @@ -285,6 +285,87 @@ async fn handles_are_reserved_given_released_and_unreserved() { ); } +/// A change posted without a reason is refused on its page, which asks for one, +/// whatever else its form takes and however the page decodes it: reserving and +/// unreserving a handle, changing an account's handle and display name, and the +/// settings tabs, which read their form from the body themselves. None fails to +/// decode, and none changes anything. +#[tokio::test] +#[ignore = "requires TEST_DATABASE_URL"] +async fn a_change_without_a_reason_is_refused_on_its_page() { + let harness = Harness::new().await; + let admin = harness.admin().await; + let owner = Browser::new_account(&harness.database).await; + let user = owner.user_id; + let suffix = &user.to_string()[24..]; + let (kept, fresh) = (format!("kept-{suffix}"), format!("fresh-{suffix}")); + let primary = format!("primary-{user}@example.com"); + crate::tests::verified_address(&harness.database, user, &primary, true).await; + let address = format!("other-{user}@example.com"); + let other = crate::tests::verified_address(&harness.database, user, &address, false) + .await + .to_string(); + let stored = async || { + harness + .database + .users() + .find_by_id(user) + .await + .expect("lookup") + .expect("user") + }; + let before = stored().await; + let dashboard = harness + .confirmed(&admin, "/handles/reserve", Utc::now()) + .await; + let post = async |path: &str, fields: &[(&str, &str)]| { + let url = format!("{DASHBOARD}{path}"); + dashboard + .submit(&harness.dashboard, &url, fields, OWN_PAGE) + .await + }; + let reserve = [("handle", kept.as_str()), REASON]; + assert_eq!( + redirect(&post("/handles/reserve", &reserve).await), + "/handles" + ); + + let unreserve = format!("/handles/unreserve?handle={kept}"); + let (handle, name, emails) = ( + format!("/users/{user}/handle"), + format!("/users/{user}/name"), + format!("/users/{user}/emails"), + ); + let remove = [("action", "remove"), ("id", other.as_str())]; + for (path, fields) in [ + ("/handles/reserve", [("handle", fresh.as_str())].as_slice()), + (&unreserve, &[]), + (&handle, &[("handle", fresh.as_str())]), + (&name, &[("display_name", "Renamed by support")]), + (&emails, &remove), + ] { + let refused = post(path, fields).await; + assert_eq!(refused.status(), StatusCode::BAD_REQUEST, "{path}"); + let page = html(refused).await; + assert!( + page.contains("Give a reason of 1 to 500 characters"), + "{path}: {page}" + ); + } + + let page = html(harness.visit(&dashboard, "/handles").await).await; + assert!(page.contains(&unreserve), "still reserved: {page}"); + assert!(!page.contains(&fresh), "nothing reserved: {page}"); + let after = stored().await; + assert_eq!(after.handle, before.handle); + assert_eq!(after.display_name, before.display_name); + let addresses = harness.database.emails().list(user).await.expect("emails"); + assert!( + addresses.iter().any(|email| email.id.to_string() == other), + "the address stays" + ); +} + /// The Alerts page shows an open alert as needing attention, with posting an /// update on it, and the overview's banner leads there. An update is recorded /// as the administrator's, and the alert then shows its status and note. The diff --git a/docs/dev/architecture/surfaces.md b/docs/dev/architecture/surfaces.md index 8a27b3623..bc6ee8df8 100644 --- a/docs/dev/architecture/surfaces.md +++ b/docs/dev/architecture/surfaces.md @@ -291,14 +291,15 @@ When an administrator or operator acts: - Every change needs their reason, on the dashboard and at the terminal alike: changes to accounts, and to clients, keys, invitations, handles, deliveries, alerts and the audit history too. `AdminService` takes it by type, so no - change records an event without one. It is stored with each event as - personal data, and the owner of an account it changes sees it in their - security history, next to "By a SourceOrigin administrator". The owner never - sees who that administrator is, or the IP address and device of their - request: only the dashboard's event lists and `audit list` show them. Announcements - and messages to one account ask for none: their text says why, so sending - one records "See message contents", and a draft's own changes record - nothing. + change records an event without one. A dashboard change posted without one, + or with a blank one, is refused on its page, which asks for it again. The + reason is stored with each event as personal data, and the owner of an + account it changes sees it in their security history, next to "By a + SourceOrigin administrator". The owner never sees who that administrator is, or the IP + address and device of their request: only the dashboard's event lists and + `audit list` show them. Announcements and messages to one account ask for + none: their text says why, so sending one records "See message contents", + and a draft's own changes record nothing. - Events record the administrator or operator as their actor. Their reads are recorded as a view of the account, as `user show` is, once an hour per administrator session or operator. The owner's history leaves views out: it diff --git a/docs/dev/extending/admin-operation.md b/docs/dev/extending/admin-operation.md index e3f20eefa..5c11452af 100644 --- a/docs/dev/extending/admin-operation.md +++ b/docs/dev/extending/admin-operation.md @@ -174,7 +174,9 @@ pub(super) async fn reserve(cx: &Cx, form: Option>) -> Result< - `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. + `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. 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