tablo_core/models.rs
1//! The models Tablo ships, for auth and the bell: [`models!`](crate::models!), which registers
2//! them beside the app's, and [`erase_user`], which deletes a user's rows from them.
3
4#[cfg(feature = "two-factor")]
5use crate::auth::AuthFactor;
6use crate::{
7 auth::{AdminUser, AuthSession, AuthThrottle, AuthToken, ColumnPreference},
8 notification::StoredNotification,
9 toasty_compat::model::AppSchema,
10};
11
12/// Register every model Tablo ships in `models`: [`AdminUser`], [`AuthSession`],
13/// [`ColumnPreference`], [`AuthToken`], [`AuthThrottle`] and [`StoredNotification`], and
14/// `AuthFactor` with the `two-factor` feature.
15///
16/// [`models!`](crate::models!) calls it; call it directly when the app builds its
17/// [`ModelSet`](toasty::schema::ModelSet) by hand. A model the panel never reads costs an empty
18/// table and nothing else.
19///
20/// Toasty names each table after its model, so an app with a model of one of these names, such
21/// as its own `AdminUser` beside a custom authenticator, cannot register both: it lists the models
22/// its panel reads in [`toasty::models!`] instead.
23pub fn register_models(models: &mut toasty::schema::ModelSet) {
24 <AdminUser as toasty::schema::Model>::register(models);
25 <AuthSession as toasty::schema::Model>::register(models);
26 <ColumnPreference as toasty::schema::Model>::register(models);
27 <AuthToken as toasty::schema::Model>::register(models);
28 #[cfg(feature = "two-factor")]
29 <AuthFactor as toasty::schema::Model>::register(models);
30 <AuthThrottle as toasty::schema::Model>::register(models);
31 <StoredNotification as toasty::schema::Model>::register(models);
32}
33
34/// Deletes every row the tables Tablo ships hold for `user_id`, its
35/// [`PanelUser::user_id`](crate::PanelUser::user_id), on every panel, through `ex`: its sessions,
36/// its mailed links and pending sign-ins, its second factors and recovery codes, its column
37/// choices and its notifications. Call it where the app deletes an account, inside the same
38/// transaction, rather than deleting those tables by name: a table a later release ships joins it.
39///
40/// It reads a model only when `ex`'s schema registers it, so an app that registers the few its
41/// panel reads calls it as is. It leaves three things to the app:
42///
43/// - the user record, an [`AdminUser`](crate::auth::AdminUser) row included, which is the app's to
44/// delete;
45/// - the [`AuthThrottle`](crate::auth::AuthThrottle) counts, which hold a hash of the login and the
46/// address, never either in the clear, and end with their window;
47/// - what the app's own tables and the audit log record of the user.
48///
49/// A page the user has open keeps what it shows; its next request is anonymous.
50///
51/// ```rust
52/// # async fn delete_account(db: &mut toasty::Db, user_id: &str) -> toasty::Result<()> {
53/// use toasty::Executor as _;
54///
55/// let mut tx = db.transaction().await?;
56/// tablo_core::erase_user(&mut tx, user_id).await?;
57/// // ... delete the app's own rows for the user, then the user.
58/// tx.commit().await
59/// # }
60/// ```
61///
62/// # Errors
63///
64/// The driver's error when a delete fails; the transaction then holds whatever ran before it.
65pub async fn erase_user(ex: &mut dyn toasty::Executor, user_id: &str) -> toasty::Result<()> {
66 let schema = AppSchema::of_executor(ex);
67 let id = user_id.to_owned();
68 if schema.registers("AuthSession") {
69 AuthSession::filter(AuthSession::fields().user_id().eq(id.clone()))
70 .delete()
71 .exec(&mut *ex)
72 .await?;
73 }
74 if schema.registers("AuthToken") {
75 AuthToken::filter(AuthToken::fields().user_id().eq(id.clone()))
76 .delete()
77 .exec(&mut *ex)
78 .await?;
79 }
80 #[cfg(feature = "two-factor")]
81 if schema.registers("AuthFactor") {
82 AuthFactor::filter(AuthFactor::fields().user_id().eq(id.clone()))
83 .delete()
84 .exec(&mut *ex)
85 .await?;
86 }
87 if schema.registers("ColumnPreference") {
88 ColumnPreference::filter(ColumnPreference::fields().user_id().eq(id.clone()))
89 .delete()
90 .exec(&mut *ex)
91 .await?;
92 }
93 if schema.registers("StoredNotification") {
94 StoredNotification::filter(StoredNotification::fields().user_id().eq(id))
95 .delete()
96 .exec(&mut *ex)
97 .await?;
98 }
99 Ok(())
100}
101
102/// [`toasty::models!`] with the models Tablo ships appended, so a panel's `Db` holds every table
103/// auth reads.
104///
105/// It takes what `toasty::models!` takes: the app's models, and `crate::*` or `some_crate::*` for
106/// every model a crate declares. A `Db` built from it never fails the mount with
107/// `MissingAuthModels`. An app with a model named as one of Tablo's lists them in
108/// `toasty::models!` instead: see [`register_models`](crate::register_models).
109///
110/// ```rust,no_run
111/// # #[derive(Debug, Clone, toasty::Model)]
112/// # struct Post {
113/// # #[key]
114/// # #[auto]
115/// # id: uuid::Uuid,
116/// # }
117/// # async fn connect() -> toasty::Result<toasty::Db> {
118/// toasty::Db::builder()
119/// .models(tablo_core::models!(Post))
120/// .connect("sqlite::memory:")
121/// .await
122/// # }
123/// ```
124#[macro_export]
125macro_rules! models {
126 ($($models:tt)*) => {{
127 let mut models = ::toasty::models!($($models)*);
128 $crate::register_models(&mut models);
129 models
130 }};
131}
132
133#[cfg(test)]
134mod tests;