Skip to content

Commit 78b06eb

Browse files
committed
refactor(admin): every change that asks only why shares one page
The dashboard built the same "change with a reason" page in ten handlers: each parsed the reason, made the change, then rebuilt the same `dashboard_change` view and rendered it. `AdminApplication::reason_change` now serves such a page from a `ReasonPage` (its change, crumb, target and Cancel link), and `reason_page` renders it, which the account changes use to add the recent recoveries. `AdminApplication::confirmed` replaces the `admin` then `confirm` pair every change page started with.
1 parent e43cdb7 commit 78b06eb

12 files changed

Lines changed: 197 additions & 210 deletions

File tree

‎apps/auth-service/src/web/admin/administrators.rs‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Who administers Origin Auth: the list, granting rights and revoking them.
22
3-
use super::{AdminApplication as App, ReasonForm, Refused};
3+
use super::{AdminApplication as App, ReasonForm, ReasonPage};
44
use crate::{AdminError, Reason};
55
use serde::Deserialize;
66
use sourceorigin_common::{UserId, admin::UserSearch};
@@ -69,8 +69,7 @@ pub(super) struct GrantForm {
6969
/// makes it an administrator.
7070
#[route([GET, POST] "/administrators/grant")]
7171
pub(super) async fn grant(cx: &Cx, form: Option<Form<GrantForm>>) -> Result<Response> {
72-
let admin = App::admin(cx).await?;
73-
App::confirm(cx, admin)?;
72+
let admin = App::confirmed(cx).await?;
7473
let service = &App::of(cx).service;
7574
let back = href!(list).resolve(cx);
7675
let (q, page) = form
@@ -117,28 +116,18 @@ pub(super) async fn grant(cx: &Cx, form: Option<Form<GrantForm>>) -> Result<Resp
117116
/// rights.
118117
#[route([GET, POST] "/administrators/{administrator}/revoke")]
119118
pub(super) async fn revoke(cx: &Cx, form: Option<Form<ReasonForm>>) -> Result<Response> {
120-
let admin = App::admin(cx).await?;
121-
App::confirm(cx, admin)?;
119+
let admin = App::confirmed(cx).await?;
122120
let service = &App::of(cx).service;
123121
let id = *path_param::<Administrator>(cx)?;
124122
let back = href!(list).resolve(cx);
125-
let refusal = App::submit_reason(cx, form, back.clone(), async |reason| {
123+
let page = ReasonPage {
124+
crumb: "Revoke rights",
125+
..ReasonPage::new(Change::RevokeAdministrator, &back)
126+
};
127+
App::reason_change(cx, form, page, back.clone(), async |reason| {
126128
service
127129
.revoke_administrator(&admin.operator, id, reason)
128130
.await
129131
})
130-
.await?;
131-
let target = App::target(cx).await;
132-
let reason = refusal.as_ref().map(Refused::reason);
133-
let refusal = refusal.as_deref();
134-
let page = view! { cx =>
135-
sourceorigin_ui::dashboard_change(
136-
change: Change::RevokeAdministrator,
137-
target: target,
138-
back: back.as_str(),
139-
refusal: refusal,
140-
reason: reason,
141-
)
142-
};
143-
App::render(cx, StatusCode::OK, "Revoke rights", page).await
132+
.await
144133
}

‎apps/auth-service/src/web/admin/alerts.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ pub(super) async fn previous(cx: &Cx) -> Result<Response> {
8484
/// not found.
8585
#[route([GET, POST] "/alerts/{alert}/update")]
8686
pub(super) async fn update(cx: &Cx, form: Option<Form<UpdateForm>>) -> Result<Response> {
87-
let admin = App::admin(cx).await?;
88-
App::confirm(cx, admin)?;
87+
let admin = App::confirmed(cx).await?;
8988
let service = &App::of(cx).service;
9089
let id = *path_param::<Alert>(cx)?;
9190
let alerts = service

‎apps/auth-service/src/web/admin/announcements.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ enum Saved {
136136
/// page was read is refused, never sent unseen.
137137
#[route([GET, POST] "/announcements/{announcement}/send")]
138138
pub(super) async fn send(cx: &Cx, form: Option<Form<SendAnnouncement>>) -> Result<Response> {
139-
let admin = App::admin(cx).await?;
140-
App::confirm(cx, admin)?;
139+
let admin = App::confirmed(cx).await?;
141140
let service = &App::of(cx).service;
142141
let id = *path_param::<Announcement>(cx)?;
143142
let checked: SendAnnouncement = parse_query_params(cx)

‎apps/auth-service/src/web/admin/clients.rs‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! page at `/clients/new`, whatever the client's ID.
1616
1717
use super::{
18-
AdminApplication as App, ReasonForm, Refusal, Refused,
18+
AdminApplication as App, ReasonForm, ReasonPage, Refusal,
1919
origins::{self, OriginQuery},
2020
};
2121
use crate::{
@@ -163,8 +163,7 @@ pub(super) async fn detail(cx: &Cx) -> Result<Response> {
163163
/// which shows a confidential one's first secret, once.
164164
#[route([GET, POST] "/clients/new")]
165165
pub(super) async fn create(cx: &Cx, form: Option<Form<NewClientForm>>) -> Result<Response> {
166-
let admin = App::admin(cx).await?;
167-
App::confirm(cx, admin)?;
166+
let admin = App::confirmed(cx).await?;
168167
let service = &App::of(cx).service;
169168
let created = App::change(cx, form, async |new: NewClientForm| {
170169
let reason = Reason::parse(new.settings.reason.clone())?;
@@ -203,8 +202,7 @@ pub(super) async fn create(cx: &Cx, form: Option<Form<NewClientForm>>) -> Result
203202
/// but its ID and kind, then goes back to its page.
204203
#[route([GET, POST] "/clients/{client}/settings")]
205204
pub(super) async fn edit(cx: &Cx, form: Option<Form<OperatorDraft>>) -> Result<Response> {
206-
let admin = App::admin(cx).await?;
207-
App::confirm(cx, admin)?;
205+
let admin = App::confirmed(cx).await?;
208206
let service = &App::of(cx).service;
209207
let stored = App::changing(cx, admin).await?.client;
210208
let id = stored.id.clone();
@@ -393,33 +391,20 @@ impl Toggle {
393391
/// `POST` makes it, then goes back to the client's page, or to the clients
394392
/// once the client is deleted.
395393
async fn page(self, cx: &Cx, form: Option<Form<ReasonForm>>) -> Result<Response> {
396-
let admin = App::admin(cx).await?;
397-
App::confirm(cx, admin)?;
394+
let admin = App::confirmed(cx).await?;
398395
let service = &App::of(cx).service;
399396
let id = App::changing(cx, admin).await?.client.id;
400397
let back = href!(detail, Client(id.clone())).resolve(cx);
401398
let done = match self {
402399
Self::Delete => href!(list).resolve(cx),
403400
Self::Enable | Self::Disable | Self::Suspend | Self::Unsuspend => back.clone(),
404401
};
405-
let refusal = App::submit_reason(cx, form, done, async |reason| {
402+
let change = self.change();
403+
let page = ReasonPage::new(change, &back);
404+
App::reason_change(cx, form, page, done, async |reason| {
406405
self.make(service, &admin.operator, &id, reason).await
407406
})
408-
.await?;
409-
let change = self.change();
410-
let target = App::target(cx).await;
411-
let reason = refusal.as_ref().map(Refused::reason);
412-
let refusal = refusal.as_deref();
413-
let page = view! { cx =>
414-
sourceorigin_ui::dashboard_change(
415-
change: change,
416-
target: target,
417-
back: back.as_str(),
418-
refusal: refusal,
419-
reason: reason,
420-
)
421-
};
422-
App::render(cx, StatusCode::OK, change.title(), page).await
407+
.await
423408
}
424409
}
425410

‎apps/auth-service/src/web/admin/deliveries.rs‎

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The outbound delivery queue: listing deliveries, retrying and cancelling one.
22
3-
use super::{AdminApplication as App, ReasonForm, Refused};
3+
use super::{AdminApplication as App, ReasonForm, ReasonPage};
44
use sourceorigin_common::{DeliveryId, admin::DeliveryStatus};
55
use sourceorigin_ui::{Action, Change, DeliveryRow, Target};
66
use topcoat::{
@@ -99,35 +99,25 @@ impl App {
9999
form: Option<Form<ReasonForm>>,
100100
change: Change,
101101
) -> Result<Response> {
102-
let admin = Self::admin(cx).await?;
103-
Self::confirm(cx, admin)?;
102+
let admin = Self::confirmed(cx).await?;
104103
let service = &Self::of(cx).service;
105104
let id = *path_param::<Delivery>(cx)?;
106105
let back = href!(list).resolve(cx);
107-
let refusal = Self::submit_reason(cx, form, back.clone(), async |reason| {
106+
let shown = id.to_string();
107+
let page = ReasonPage {
108+
target: Some(Target {
109+
name: "Delivery",
110+
id: Some(&shown),
111+
}),
112+
..ReasonPage::new(change, &back)
113+
};
114+
Self::reason_change(cx, form, page, back.clone(), async |reason| {
108115
if change == Change::CancelDelivery {
109116
service.cancel_delivery(&admin.operator, id, reason).await
110117
} else {
111118
service.retry_delivery(&admin.operator, id, reason).await
112119
}
113120
})
114-
.await?;
115-
let id = id.to_string();
116-
let target = Target {
117-
name: "Delivery",
118-
id: Some(&id),
119-
};
120-
let reason = refusal.as_ref().map(Refused::reason);
121-
let refusal = refusal.as_deref();
122-
let page = view! { cx =>
123-
sourceorigin_ui::dashboard_change(
124-
change: change,
125-
target: target,
126-
back: back.as_str(),
127-
refusal: refusal,
128-
reason: reason,
129-
)
130-
};
131-
Self::render(cx, StatusCode::OK, change.title(), page).await
121+
.await
132122
}
133123
}

‎apps/auth-service/src/web/admin/handles.rs‎

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Handles: the reserved list, the accounts holding a reserved handle, and the
22
//! released handles, with reserving, unreserving and releasing one.
33
4-
use super::{AdminApplication as App, ReasonForm, Refused, settings, users::Account};
4+
use super::{AdminApplication as App, ReasonForm, ReasonPage, settings, users::Account};
55
use crate::{Reason, handles::HandlesError};
66
use serde::{Deserialize, Serialize};
77
use sourceorigin_common::{Handle, handle::RESERVED};
@@ -98,8 +98,7 @@ pub(super) async fn check(cx: &Cx) -> Result<Response> {
9898
/// `/handles/reserve`: reserves a handle, saying who or what it is kept for.
9999
#[route([GET, POST] "/handles/reserve")]
100100
pub(super) async fn reserve(cx: &Cx, form: Option<Form<Reservation>>) -> Result<Response> {
101-
let admin = App::admin(cx).await?;
102-
App::confirm(cx, admin)?;
101+
let admin = App::confirmed(cx).await?;
103102
let service = &App::of(cx).service;
104103
let back = href!(check).resolve(cx);
105104
let refused = App::submit(cx, form, back.clone(), |input: Reservation| async move {
@@ -150,17 +149,19 @@ impl App {
150149
form: Option<Form<ReasonForm>>,
151150
change: Change,
152151
) -> Result<Response> {
153-
let admin = Self::admin(cx).await?;
154-
Self::confirm(cx, admin)?;
152+
let admin = Self::confirmed(cx).await?;
155153
let service = &Self::of(cx).service;
156154
let handle = query_params::<HandleQuery>(cx)?.handle.clone();
157155
let back = href!(check).resolve(cx);
158156
let shown = format!("@{handle}");
159-
let target = Target {
160-
name: &shown,
161-
id: None,
157+
let page = ReasonPage {
158+
target: Some(Target {
159+
name: &shown,
160+
id: None,
161+
}),
162+
..ReasonPage::new(change, &back)
162163
};
163-
let refusal = Self::submit_reason(cx, form, back.clone(), async |reason| {
164+
Self::reason_change(cx, form, page, back.clone(), async |reason| {
164165
if change == Change::ReleaseHandle {
165166
service
166167
.release_handle(&admin.operator, handle, reason)
@@ -171,18 +172,6 @@ impl App {
171172
.await
172173
}
173174
})
174-
.await?;
175-
let reason = refusal.as_ref().map(Refused::reason);
176-
let refusal = refusal.as_deref();
177-
let page = view! { cx =>
178-
sourceorigin_ui::dashboard_change(
179-
change: change,
180-
target: target,
181-
back: back.as_str(),
182-
refusal: refusal,
183-
reason: reason,
184-
)
185-
};
186-
Self::render(cx, StatusCode::OK, change.title(), page).await
175+
.await
187176
}
188177
}

‎apps/auth-service/src/web/admin/invitations.rs‎

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Signup invitations, whatever the signup setting: mailed to an address, or
22
//! links to pass on.
33
4-
use super::{AdminApplication as App, ReasonForm, Refused};
4+
use super::{AdminApplication as App, ReasonForm, ReasonPage};
55
use crate::{AdminError, InviteExpiry, Reason};
66
use chrono::Utc;
77
use sourceorigin_common::{
@@ -140,59 +140,50 @@ pub(super) async fn create(cx: &Cx, form: Option<Form<CreateInvitation>>) -> Res
140140
/// `/invitations/{invitation}/resend`: mails an open invitation's link again.
141141
#[route([GET, POST] "/invitations/{invitation}/resend")]
142142
pub(super) async fn resend(cx: &Cx, form: Option<Form<ReasonForm>>) -> Result<Response> {
143-
let admin = App::admin(cx).await?;
144-
App::confirm(cx, admin)?;
143+
let admin = App::confirmed(cx).await?;
145144
let service = &App::of(cx).service;
146145
let id = *path_param::<Invitation>(cx)?;
147-
let back = href!(list).resolve(cx);
148-
let refusal = App::submit_reason(cx, form, back.clone(), async |reason| {
146+
App::invitation_change(cx, form, Change::ResendInvitation, id, async |reason| {
149147
service.resend_invitation(&admin.operator, id, reason).await
150148
})
151-
.await?;
152-
App::invitation_change(cx, Change::ResendInvitation, id, &back, refusal).await
149+
.await
153150
}
154151

155152
/// `/invitations/{invitation}/revoke`: revokes an unredeemed invitation.
156153
#[route([GET, POST] "/invitations/{invitation}/revoke")]
157154
pub(super) async fn revoke(cx: &Cx, form: Option<Form<ReasonForm>>) -> Result<Response> {
158-
let admin = App::admin(cx).await?;
159-
App::confirm(cx, admin)?;
155+
let admin = App::confirmed(cx).await?;
160156
let service = &App::of(cx).service;
161157
let id = *path_param::<Invitation>(cx)?;
162-
let back = href!(list).resolve(cx);
163-
let refusal = App::submit_reason(cx, form, back.clone(), async |reason| {
158+
App::invitation_change(cx, form, Change::RevokeInvitation, id, async |reason| {
164159
service.revoke_invitation(&admin.operator, id, reason).await
165160
})
166-
.await?;
167-
App::invitation_change(cx, Change::RevokeInvitation, id, &back, refusal).await
161+
.await
168162
}
169163

170164
impl App {
171-
/// The page of `change` to the invitation `id`, going back to `back`, with
172-
/// why its last attempt was refused, if it was.
173-
async fn invitation_change(
165+
/// The page of `change` to the invitation `id`, made with `call`, which goes
166+
/// back to the invitations once made.
167+
async fn invitation_change<F, Fut>(
174168
cx: &Cx,
169+
form: Option<Form<ReasonForm>>,
175170
change: Change,
176171
id: TokenId,
177-
back: &str,
178-
refusal: Option<Refused<ReasonForm>>,
179-
) -> Result<Response> {
172+
call: F,
173+
) -> Result<Response>
174+
where
175+
F: FnOnce(Reason) -> Fut,
176+
Fut: Future<Output = Result<(), AdminError>>,
177+
{
178+
let back = href!(list).resolve(cx);
180179
let shown = id.to_string();
181-
let target = Target {
182-
name: "Invitation",
183-
id: Some(&shown),
184-
};
185-
let reason = refusal.as_ref().map(Refused::reason);
186-
let refusal = refusal.as_deref();
187-
let page = view! { cx =>
188-
sourceorigin_ui::dashboard_change(
189-
change: change,
190-
target: target,
191-
back: back,
192-
refusal: refusal,
193-
reason: reason,
194-
)
180+
let page = ReasonPage {
181+
target: Some(Target {
182+
name: "Invitation",
183+
id: Some(&shown),
184+
}),
185+
..ReasonPage::new(change, &back)
195186
};
196-
App::render(cx, StatusCode::OK, change.title(), page).await
187+
Self::reason_change(cx, form, page, back.clone(), call).await
197188
}
198189
}

0 commit comments

Comments
 (0)