tablo-core 0.8.0

The core toolkit types for Tablo, a server-rendered admin toolkit on Topcoat and Toasty.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! The panel's tenant registration page: the [`TenantRegistrar`] trait that creates a tenant for
//! the signed-in user, the [`TenantRegistration`] a panel installs it with, and the
//! `{prefix}/register-tenant` routes.

use std::{any::Any, future::Future, marker::PhantomData};

use topcoat::{
    context::Cx,
    icon::icon,
    router::{
        Body, RouteFuture,
        error::{forbidden, not_found, see_other},
        response::{IntoResponse, Response},
    },
    view::{ViewExt, attributes, view},
};

use super::{
    PanelUser, SignedIn, infrastructure_failure, input,
    login::logout_url,
    page::{AuthPage, render, switch_link},
    panel_root,
    session::select_tenant,
    signed, unauthenticated_error,
};
use crate::{
    BoxFuture,
    auth::realm::{Realm, current_realm, hosted, panel_prefix},
    flash::set_notification,
    form::{FieldErrors, FormInput, FormValues},
    outcome::Notification,
    schema::{Schema, Source},
    tenancy::Membership,
};

/// How a panel lets the signed-in user create a tenant to act for, installed with a
/// [`TenantRegistration`].
///
/// It mirrors a [`Registrar`](super::Registrar): [`Input`](Self::Input) is the form the page asks
/// for, [`validate`](Self::validate) refuses a submission field by field, and
/// [`register`](Self::register) writes the tenant and the user's membership of it. Both run in
/// one transaction the framework opens. The next request reloads the user, whose
/// [`tenants`](PanelUser::tenants) must then list the new one.
///
/// ```rust
/// # use tablo_core::{Membership, PanelUser, auth::TenantRegistrar};
/// # use topcoat::context::Cx;
/// # #[derive(Debug, Clone, toasty::Model)]
/// # struct Team { #[key] #[auto] id: uuid::Uuid, name: String }
/// # #[derive(Debug, Clone, toasty::Model)]
/// # struct Seat { #[key] #[auto] id: uuid::Uuid, staff_id: uuid::Uuid, team_id: uuid::Uuid }
/// # struct Staff { id: uuid::Uuid, teams: Vec<Membership> }
/// # impl PanelUser for Staff {
/// #     fn user_id(&self) -> String { self.id.to_string() }
/// #     fn display_name(&self) -> &str { "" }
/// #     fn tenants(&self) -> &[Membership] { &self.teams }
/// # }
/// #[derive(tablo_core::FormInput)]
/// struct NewTeam {
///     name: String,
/// }
///
/// struct Teams;
///
/// impl TenantRegistrar for Teams {
///     type Input = NewTeam;
///     type User = Staff;
///
///     async fn register(
///         &self,
///         _cx: &Cx,
///         staff: &Staff,
///         team: NewTeam,
///         ex: &mut dyn toasty::Executor,
///     ) -> topcoat::Result<Membership> {
///         let team = toasty::create!(Team { name: team.name })
///             .exec(&mut *ex)
///             .await?;
///         toasty::create!(Seat {
///             staff_id: staff.id,
///             team_id: team.id,
///         })
///         .exec(ex)
///         .await?;
///         Ok(Membership::new(team.id.into(), team.name))
///     }
/// }
/// ```
pub trait TenantRegistrar: Send + Sync + 'static {
    /// What the page asks for: the app's own
    /// [`#[derive(FormInput)]`](derive@crate::FormInput) struct.
    type Input: FormInput;

    /// The user who registers the tenant: the type the panel's
    /// [`Authenticator`](super::Authenticator) loads, which mounting checks.
    type User: PanelUser;

    /// Refuse a submission [`register`](Self::register) should not receive, such as a name
    /// another tenant holds: each error names an input field's key and renders under its
    /// control. Defaults to none. A key no control renders is a declaration error: the page
    /// would show no message.
    ///
    /// It runs inside the registration's transaction `ex`, after the input parses and its
    /// controls' own rules pass.
    fn validate(
        &self,
        _cx: &Cx,
        _user: &Self::User,
        _input: &Self::Input,
        _ex: &mut dyn toasty::Executor,
    ) -> impl Future<Output = topcoat::Result<FieldErrors>> + Send {
        async { Ok(FieldErrors::new()) }
    }

    /// Create the tenant from a validated `input`, with `user` a member of it, through the
    /// transaction `ex`, which commits when it returns `Ok`; answer the membership the request's
    /// session then acts under.
    fn register(
        &self,
        cx: &Cx,
        user: &Self::User,
        input: Self::Input,
        ex: &mut dyn toasty::Executor,
    ) -> impl Future<Output = topcoat::Result<Membership>> + Send;
}

/// A panel's tenant registration page, installed with
/// [`Auth::tenant_registration`](super::Auth::tenant_registration): the [`TenantRegistrar`] that
/// creates tenants, and whether a user who acts for none must create one first.
///
/// ```rust
/// # use tablo_core::{Auth, Authenticator, Membership, PanelUser, TenantRegistration, auth::TenantRegistrar};
/// # use topcoat::context::Cx;
/// # struct Staff;
/// # impl PanelUser for Staff {
/// #     fn user_id(&self) -> String { String::new() }
/// #     fn display_name(&self) -> &str { "" }
/// # }
/// # struct StaffAuth;
/// # impl Authenticator for StaffAuth {
/// #     type User = Staff;
/// #     async fn verify(&self, _: &Cx, _: &str, _: &str) -> topcoat::Result<Option<Staff>> { Ok(None) }
/// #     async fn find_by_id(&self, _: &Cx, _: &str) -> topcoat::Result<Option<Staff>> { Ok(None) }
/// # }
/// # #[derive(tablo_core::FormInput)]
/// # struct NewTeam { name: String }
/// # impl TenantRegistrar for StaffAuth {
/// #     type Input = NewTeam;
/// #     type User = Staff;
/// #     async fn register(&self, _: &Cx, _: &Staff, _: NewTeam, _: &mut dyn toasty::Executor)
/// #         -> topcoat::Result<Membership> { unimplemented!() }
/// # }
/// let auth = Auth::custom(StaffAuth).tenant_registration(TenantRegistration::new(StaffAuth).required());
/// # let _ = auth;
/// ```
///
/// `U` is the user type the registrar loads; [`Auth`](crate::Auth) takes only one over the type its
/// authenticator loads.
pub struct TenantRegistration<U> {
    registrar: Box<dyn DynTenantRegistrar>,
    required: bool,
    user: PhantomData<fn() -> U>,
}

impl<U: PanelUser> TenantRegistration<U> {
    /// Registers tenants through `registrar`. A user who acts for no tenant still reaches the
    /// panel, and its resources that need a tenant answer 403; see [`required`](Self::required).
    #[must_use]
    pub fn new(registrar: impl TenantRegistrar<User = U>) -> Self {
        Self {
            registrar: Box::new(registrar),
            required: false,
            user: PhantomData,
        }
    }

    /// Sends a signed-in user who acts for no tenant to the registration page before anything
    /// else: a page answers with a redirect to it and any other request with 403, as for an
    /// unverified address, which comes first. Leave it off where some users, such as operators,
    /// act for no tenant on purpose.
    #[must_use]
    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }

    /// The same declaration, its user type checked by [`Auth`](crate::Auth).
    pub(crate) fn erase(self) -> TenantRegistration<()> {
        TenantRegistration {
            registrar: self.registrar,
            required: self.required,
            user: PhantomData,
        }
    }
}

impl TenantRegistration<()> {
    pub(crate) fn registrar(&self) -> &dyn DynTenantRegistrar {
        &*self.registrar
    }
}

impl<U> std::fmt::Debug for TenantRegistration<U> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TenantRegistration")
            .field("required", &self.required)
            .finish_non_exhaustive()
    }
}

/// What a tenant registration came to.
pub(crate) enum Outcome {
    /// The committed tenant, as the user's membership.
    Registered(Membership),
    /// The refused submission, its values, and why.
    Refused {
        values: FormValues,
        errors: FieldErrors,
    },
}

/// [`TenantRegistrar`] with its input and user types erased, as the panel keeps it.
pub(crate) trait DynTenantRegistrar: Send + Sync {
    /// The registration form.
    fn schema(&self) -> Schema;
    /// Parses, checks, validates and registers one submission for `signed`'s user.
    fn register<'a>(
        &'a self,
        cx: &'a Cx,
        signed: &'a SignedIn,
        values: FormValues,
    ) -> BoxFuture<'a, topcoat::Result<Outcome>>;
}

impl<R: TenantRegistrar> DynTenantRegistrar for R {
    fn schema(&self) -> Schema {
        R::Input::schema()
    }

    fn register<'a>(
        &'a self,
        cx: &'a Cx,
        signed: &'a SignedIn,
        values: FormValues,
    ) -> BoxFuture<'a, topcoat::Result<Outcome>> {
        Box::pin(async move {
            let any: &dyn Any = &*signed.user;
            let user = any
                .downcast_ref::<R::User>()
                .ok_or_else(|| topcoat::Error::from(forbidden()))?;
            let input =
                match input::parse::<R::Input>(cx, &values, &FormValues::new(), "tenant").await? {
                    Ok(input) => input,
                    Err(errors) => return Ok(Outcome::Refused { values, errors }),
                };
            let mut db = crate::db::db(cx)?;
            let mut tx = db.transaction().await.map_err(infrastructure_failure)?;
            let errors = self
                .validate(cx, user, &input, &mut tx)
                .await
                .map_err(infrastructure_failure)?;
            input::placed(
                &R::Input::schema(),
                &errors,
                "the tenant registrar",
                "tenant",
            )?;
            if !errors.is_empty() {
                return Ok(Outcome::Refused { values, errors });
            }
            let membership = TenantRegistrar::register(self, cx, user, input, &mut tx)
                .await
                .map_err(infrastructure_failure)?;
            tx.commit().await.map_err(infrastructure_failure)?;
            Ok(Outcome::Registered(membership))
        })
    }
}

/// The keys a tenant registration POST carries besides its input, which no input field may post.
pub(crate) const RESERVED_KEYS: [&str; 1] = [crate::csrf::FIELD_NAME];

/// Where the panel's tenant registration page lives: `{prefix}/register-tenant`.
pub(crate) fn register_tenant_url(cx: &Cx) -> String {
    format!("{}/register-tenant", panel_prefix(cx))
}

/// Whether the request's panel installs a [`TenantRegistration`].
pub(crate) fn offered(cx: &Cx) -> bool {
    current_realm(cx).is_some_and(|panel| panel.auth.tenant_registration_page().is_some())
}

/// Whether `user` must register a tenant before `panel` serves them anything else: the panel
/// requires one, and the user acts for none.
pub(super) fn pending(panel: &Realm, user: &dyn PanelUser) -> bool {
    panel
        .auth
        .tenant_registration_page()
        .is_some_and(|registration| registration.required)
        && user.tenants().is_empty()
}

/// The signed-in user and the panel's registrar, or the answer to a request without them.
fn registering(cx: &Cx) -> topcoat::Result<(&SignedIn, &dyn DynTenantRegistrar)> {
    let signed = signed(cx).ok_or_else(|| unauthenticated_error(cx))?;
    let registration = signed
        .realm
        .auth
        .tenant_registration_page()
        .ok_or_else(not_found)?;
    Ok((signed, registration.registrar()))
}

/// `GET {prefix}/register-tenant` — the standalone tenant registration page.
pub(super) fn register_tenant_page(cx: &Cx, _body: Body) -> RouteFuture<'_> {
    Box::pin(async move {
        let (signed, registrar) = registering(cx)?;
        page_response(
            cx,
            signed,
            registrar,
            &FormValues::new(),
            &FieldErrors::new(),
        )
        .await
    })
}

/// `POST {prefix}/register-tenant` — create the tenant, act for it, then land on the panel root.
pub(super) fn register_tenant_post(cx: &Cx, body: Body) -> RouteFuture<'_> {
    Box::pin(async move {
        let (signed, registrar) = registering(cx)?;
        let form = crate::auth::input::read_form(cx, body).await?;
        crate::csrf::verify(cx, form.last(crate::csrf::FIELD_NAME))?;
        let input_schema = registrar.schema();
        let values = input_schema.read_input(cx, &form.pairs, &RESERVED_KEYS)?;
        let membership = match registrar.register(cx, signed, values).await? {
            Outcome::Registered(membership) => membership,
            Outcome::Refused { values, errors } => {
                return page_response(cx, signed, registrar, &values, &errors).await;
            }
        };
        select_tenant(cx, membership.tenant.get()).await?;
        set_notification(
            cx,
            Notification::success(crate::t!(cx, "tablo-tenant-created")).description(crate::t!(
                cx,
                "tablo-tenant-acting",
                tenant = membership.name.clone()
            )),
        );
        see_other(panel_root(cx)).into_response(cx)
    })
}

/// The registration page: 422 for a refused submission, else 200. A user who acts for a tenant
/// already may go back to the panel; one who acts for none may only sign out.
async fn page_response(
    cx: &Cx,
    signed: &SignedIn,
    registrar: &dyn DynTenantRegistrar,
    values: &FormValues,
    errors: &FieldErrors,
) -> topcoat::Result<Response> {
    let csrf = crate::csrf::ensure_token(cx);
    let action = register_tenant_url(cx);
    let schema = registrar.schema();
    let fields = schema.render(cx, Source::form(values, errors)).await?;
    let first = signed.user.tenants().is_empty();
    let logout = logout_url(cx);
    let form = view! {
        cx =>
        <div class="flex flex-col gap-3">
            <form method="post" action=(action) class="flex flex-col gap-5">
                (crate::csrf::field(cx, &csrf))
                (fields)
                tablo_ui::button(
                    variant: tablo_ui::ButtonVariant::Primary,
                    attrs: attributes! { type="submit" class="w-full" },
                    (crate::t!(cx, "tablo-shell-create-tenant"))
                )
            </form>
            if first {
                <form method="post" action=(logout)>
                    (crate::csrf::field(cx, &csrf))
                    tablo_ui::button(
                        variant: tablo_ui::ButtonVariant::Outline,
                        attrs: attributes! { type="submit" class="w-full" },
                        icon(data: tablo_ui::icons::LOG_OUT)
                        (crate::t!(cx, "tablo-shell-sign-out"))
                    )
                </form>
            }
        </div>
    }
    .boxed();
    let root = panel_root(cx);
    let brand = hosted(cx)?.brand_name(cx);
    let page = render(
        cx,
        AuthPage {
            title: crate::t!(cx, "tablo-tenant-create-title"),
            description: if first {
                crate::t!(cx, "tablo-tenant-create-first", brand = brand)
            } else {
                crate::t!(cx, "tablo-tenant-create-another", brand = brand)
            },
            status: (!errors.is_empty()).then_some(http::StatusCode::UNPROCESSABLE_ENTITY),
            form,
            switch: (!first).then(|| {
                switch_link(
                    cx,
                    "tablo-tenant-changed-mind",
                    &root,
                    "tablo-tenant-go-back",
                )
            }),
            note: None,
        },
    )
    .await?;
    page.single().await?.into_response(cx)
}

/// The mistakes in a panel's [`Auth::tenant_registration`](super::Auth::tenant_registration): a
/// registration without auth, a registrar whose user is not the authenticator's, and a form field
/// the page cannot serve.
pub(super) fn declaration_errors(auth: &super::AuthConfig) -> Vec<crate::DeclarationErrorKind> {
    use super::input::FieldFault;
    use crate::{DeclarationErrorKind, declaration::TenantRegistrationFault as Fault};

    let Some(registrar) = auth
        .tenant_registration
        .as_ref()
        .map(TenantRegistration::registrar)
    else {
        return Vec::new();
    };
    if auth.authenticator().is_none() {
        return vec![DeclarationErrorKind::TenantRegistration(
            Fault::AuthDisabled,
        )];
    }
    let mut kinds = Vec::new();
    let schema = registrar.schema();
    kinds.extend(schema.declaration_errors());
    kinds.extend(schema.empty_choices());
    kinds.extend(
        input::field_faults(&schema, &RESERVED_KEYS).map(|(fault, name)| {
            DeclarationErrorKind::TenantRegistration(match fault {
                FieldFault::Reserved => Fault::ReservedField(name),
                FieldFault::File => Fault::FileField(name),
                FieldFault::Relationship => Fault::RelationshipField(name),
            })
        }),
    );
    kinds
}