Skip to content

Commit 183e087

Browse files
committed
docs(dev): adding an admin operation takes the reason every change needs
1 parent 1daf4a6 commit 183e087

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

‎docs/dev/extending/admin-operation.md‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ checks its operator itself and both surfaces run the same rules.
2323

2424
- Decide whether the operation reads or changes. A read records nothing, unless
2525
it shows an account's personal data, which records a view. A change records
26-
an event. One the owner should understand takes a reason, which their history
27-
shows, as disabling an account does.
26+
an event, with the reason every change takes. The owner of an account it
27+
changes sees that reason in their history, as for disabling an account.
2828
- If the operation changes a setting the owner also changes, it is an account
2929
setting instead: follow [adding an account setting](account-setting.md).
3030
- New tables or columns come first: [adding a migration](migration.md).
@@ -57,11 +57,12 @@ Add the method to `AdminService`, in the module of its area under
5757
renders and the CLI prints as JSON: `Self::views` converts stored records into
5858
them. A read of an account's personal data records a view with
5959
`operator.record_view`, as `view_user` does.
60-
- **A change** runs in one transaction. On an account, it locks the account with
61-
`operator.lock(&mut tx, user_id)` first, which refuses another administrator's
62-
account unless the CLI acts. It ends with `Self::finish(tx, operator, subject,
63-
events)`, which checks the operator again after the last write, records the
64-
events and commits.
60+
- **A change** takes the operator's `Reason` and runs in one transaction. On an
61+
account, it locks the account with `operator.lock(&mut tx, user_id)` first,
62+
which refuses another administrator's account unless the CLI acts. It ends
63+
with `Self::finish(tx, operator, &reason, subject, events)`, which checks the
64+
operator again after the last write, records the events with the reason and
65+
commits.
6566

6667
```rust
6768
pub(crate) async fn reserve_handle(
@@ -70,18 +71,23 @@ pub(crate) async fn reserve_handle(
7071
handle: Handle,
7172
reason: Reason,
7273
) -> Result<(), AdminError> {
73-
let context = Self::context(operator, reason);
7474
let mut tx = self.database.begin().await?;
75-
// ...the change...
76-
tx.record(&context, None, DomainEvent::HandleReserved { handle })
77-
.await?;
78-
Self::finish(tx, operator, None, vec![]).await
75+
if !tx.handles().reserve(&handle, reason.as_str()).await? {
76+
return Err(AdminError::HandleReserved);
77+
}
78+
let reserved = DomainEvent::HandleReserved { handle };
79+
Self::finish(tx, operator, &reason, None, vec![reserved]).await
7980
}
8081
```
8182

82-
`Self::context(operator, reason)` attributes an event to the operator with their
83-
`Reason`, which is stored as personal data. An event without a reason goes in
84-
`finish`'s list instead.
83+
Every change takes a `Reason`, which is stored with each of its events as
84+
personal data, and which the owner of an account it changes sees in their
85+
history. `operator.context(&reason)` is the only way to attribute a change's
86+
events, so none is recorded without one: use it for an event the change records
87+
before `finish`, such as through another service. A read records with
88+
`operator.reading()`, which has no reason. A change whose own content says why,
89+
such as an announcement's text, records `Reason::see_message()` instead of
90+
asking for one.
8591

8692
A refusal is an `AdminError` variant in `apps/auth-service/src/admin/error.rs`:
8793
its message says what to fix, its `reason()` names it, and its arm in
@@ -165,7 +171,9 @@ pub(super) async fn reserve(cx: &Cx, form: Option<Form<Reservation>>) -> Result<
165171
- `App::confirm` requires the dashboard's own recent passkey confirmation, or
166172
sends the browser to confirm and back.
167173
- `App::submit` makes the change, goes back to `back` once it is made, and
168-
otherwise returns the refusal, which the form shows with what was typed.
174+
otherwise returns the refusal, which the form shows with what was typed. A
175+
change whose form posts only its reason uses `App::submit_reason` and
176+
`ReasonForm`, which parse the reason first.
169177

170178
The page's words are a `Change` variant in `crates/ui/src/admin/mod.rs`: its
171179
title, description, button, label, whether it takes something away, and the

0 commit comments

Comments
 (0)